<script setup>
import { ref } from 'vue';
import { useRoute } from 'vue-router';
import { useI18n } from 'vue-i18n';

import FetchData from 'components/FetchData.vue';
import FormModel from 'components/FormModel.vue';
import VnRow from 'components/ui/VnRow.vue';
import VnInput from 'src/components/common/VnInput.vue';
import VnSelectFilter from 'src/components/common/VnSelectFilter.vue';
import VnLocation from 'src/components/common/VnLocation.vue';

const route = useRoute();
const { t } = useI18n();

const sageTaxTypesOptions = ref([]);
const sageWithholdingsOptions = ref([]);
const sageTransactionTypesOptions = ref([]);
const supplierActivitiesOptions = ref([]);
const postcodesOptions = ref([]);

function handleLocation(data, location) {
    const { town, code, provinceFk, countryFk } = location ?? {};
    data.postCode = code;
    data.city = town;
    data.provinceFk = provinceFk;
    data.countryFk = countryFk;
}
</script>
<template>
    <FetchData
        url="SageTaxTypes"
        auto-load
        @on-fetch="(data) => (sageTaxTypesOptions = data)"
    />
    <FetchData
        url="SageWithholdings"
        auto-load
        @on-fetch="(data) => (sageWithholdingsOptions = data)"
    />
    <FetchData
        url="sageTransactionTypes"
        auto-load
        @on-fetch="(data) => (sageTransactionTypesOptions = data)"
    />
    <FetchData
        url="SupplierActivities"
        auto-load
        @on-fetch="(data) => (supplierActivitiesOptions = data)"
    />
    <FormModel
        :url="`Suppliers/${route.params.id}`"
        :url-update="`Suppliers/${route.params.id}/updateFiscalData`"
        model="supplier"
        auto-load
        :clear-store-on-unmount="false"
    >
        <template #form="{ data, validate }">
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        v-model="data.name"
                        :label="t('supplier.fiscalData.name')"
                        clearable
                    />
                </div>
                <div class="col">
                    <VnInput
                        v-model="data.nif"
                        :label="t('supplier.fiscalData.nif')"
                        clearable
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        v-model="data.account"
                        :label="t('supplier.fiscalData.account')"
                        clearable
                    />
                </div>
                <div class="col">
                    <VnSelectFilter
                        :label="t('supplier.fiscalData.sageTaxTypeFk')"
                        v-model="data.sageTaxTypeFk"
                        :options="sageTaxTypesOptions"
                        option-value="id"
                        option-label="vat"
                        hide-selected
                        map-options
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnSelectFilter
                        :label="t('supplier.fiscalData.sageWithholdingFk')"
                        v-model="data.sageWithholdingFk"
                        :options="sageWithholdingsOptions"
                        option-value="id"
                        option-label="withholding"
                        hide-selected
                        map-options
                    />
                </div>
                <div class="col">
                    <VnSelectFilter
                        :label="t('supplier.fiscalData.sageTransactionTypeFk')"
                        v-model="data.sageTransactionTypeFk"
                        :options="sageTransactionTypesOptions"
                        option-value="id"
                        option-label="transaction"
                        hide-selected
                        map-options
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnSelectFilter
                        :label="t('supplier.fiscalData.supplierActivityFk')"
                        v-model="data.supplierActivityFk"
                        :options="supplierActivitiesOptions"
                        option-value="code"
                        option-label="name"
                        hide-selected
                        map-options
                    />
                </div>
                <div class="col">
                    <VnInput
                        v-model="data.healthRegister"
                        :label="t('supplier.fiscalData.healthRegister')"
                        clearable
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        v-model="data.street"
                        :label="t('supplier.fiscalData.street')"
                        clearable
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnLocation
                        :rules="validate('Worker.postcode')"
                        :roles-allowed-to-create="['deliveryAssistant']"
                        :options="postcodesOptions"
                        v-model="data.postCode"
                        @update:model-value="(location) => handleLocation(data, location)"
                    >
                    </VnLocation>
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col flex justify-around">
                    <QCheckbox
                        v-model="data.isTrucker"
                        :label="t('supplier.fiscalData.isTrucker')"
                    />
                    <div class="row items-center">
                        <QCheckbox
                            v-model="data.isVies"
                            :label="t('supplier.fiscalData.isVies')"
                        />
                        <QIcon name="info" size="xs" class="cursor-pointer q-ml-sm">
                            <QTooltip>
                                {{
                                    t(
                                        'When activating it, do not enter the country code in the ID field.'
                                    )
                                }}
                            </QTooltip>
                        </QIcon>
                    </div>
                </div>
            </VnRow>
        </template>
    </FormModel>
</template>

<i18n>
es:
    When activating it, do not enter the country code in the ID field.: Al activarlo, no informar el código del país en el campo nif
</i18n>
