<script setup>
import { reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRoute, useRouter } from 'vue-router';
import FormModel from 'components/FormModel.vue';
import VnRow from 'components/ui/VnRow.vue';
import VnSelect from 'src/components/common/VnSelect.vue';
import FetchData from 'components/FetchData.vue';
import VnSubToolbar from 'src/components/ui/VnSubToolbar.vue';
import VnSearchbar from 'src/components/ui/VnSearchbar.vue';
import { useStateStore } from 'stores/useStateStore';
import { useState } from 'src/composables/useState';
import VnInputDate from 'src/components/common/VnInputDate.vue';
import VnInput from 'src/components/common/VnInput.vue';

const state = useState();
const { t } = useI18n();
const route = useRoute();
const router = useRouter();
const stateStore = useStateStore();

const user = state.getUser();
const newInvoiceIn = reactive({
    supplierFk: +route.query?.supplierFk || null,
    supplierRef: null,
    companyFk: user.value.companyFk || null,
    issued: Date.vnNew(),
});
const companies = ref([]);

const redirectToInvoiceInBasicData = (__, { id }) => {
    router.push({ name: 'InvoiceInBasicData', params: { id } });
};
</script>

<template>
    <FetchData
        ref="companiesRef"
        url="Companies"
        :filter="{ fields: ['id', 'code'] }"
        order="code"
        @on-fetch="(data) => (companies = data)"
        auto-load
    />
    <template v-if="stateStore.isHeaderMounted()">
        <Teleport to="#searchbar">
            <VnSearchbar
                custom-route-redirect-name="InvoiceInSummary"
                data-key="InvoiceInSummary"
            />
        </Teleport>
    </template>
    <QPage>
        <VnSubToolbar />
        <FormModel
            url-create="InvoiceIns"
            model="InvoiceIn"
            :form-initial-data="newInvoiceIn"
            @on-data-saved="redirectToInvoiceInBasicData"
        >
            <template #form="{ data, validate }">
                <VnRow>
                    <VnSelect
                        url="Suppliers"
                        :fields="['id', 'nickname']"
                        :label="t('Supplier')"
                        v-model="data.supplierFk"
                        option-value="id"
                        option-label="nickname"
                        hide-selected
                        :required="true"
                        :rules="validate('entry.supplierFk')"
                    >
                        <template #option="scope">
                            <QItem v-bind="scope.itemProps">
                                <QItemSection>
                                    <QItemLabel>{{ scope.opt?.nickname }}</QItemLabel>
                                    <QItemLabel caption>
                                        #{{ scope.opt?.id }}
                                    </QItemLabel>
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelect>
                    <VnInput
                        :label="t('invoiceIn.summary.supplierRef')"
                        v-model="data.supplierRef"
                    />
                </VnRow>
                <VnRow>
                    <VnSelect
                        :label="t('Company')"
                        v-model="data.companyFk"
                        :options="companies"
                        option-value="id"
                        option-label="code"
                        map-options
                        hide-selected
                        :required="true"
                        :rules="validate('invoiceIn.companyFk')"
                    />
                    <VnInputDate
                        :label="t('invoiceIn.summary.issued')"
                        v-model="data.issued"
                    />
                </VnRow>
            </template>
        </FormModel>
    </QPage>
</template>

<i18n>
    es:
        Supplier: Proveedor
        Travel: Envío
        Company: Empresa
</i18n>
