<script setup>
import { reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';

import FetchData from 'components/FetchData.vue';
import VnRow from 'components/ui/VnRow.vue';
import VnSelectFilter from 'src/components/common/VnSelectFilter.vue';
import VnInput from 'src/components/common/VnInput.vue';
import CreateNewCityForm from './CreateNewCityForm.vue';
import CreateNewProvinceForm from './CreateNewProvinceForm.vue';
import VnSelectCreate from 'components/common/VnSelectCreate.vue';
import FormModelPopup from './FormModelPopup.vue';

const emit = defineEmits(['onDataSaved']);

const { t } = useI18n();

const postcodeFormData = reactive({
    code: null,
    countryFk: null,
    provinceFk: null,
    townFk: null,
});

const townsFetchDataRef = ref(null);
const provincesFetchDataRef = ref(null);
const countriesOptions = ref([]);
const provincesOptions = ref([]);
const townsLocationOptions = ref([]);

const onDataSaved = (dataSaved) => {
    emit('onDataSaved', dataSaved);
};

const onCityCreated = async ({ name, provinceFk }, formData) => {
    await townsFetchDataRef.value.fetch();
    formData.townFk = townsLocationOptions.value.find((town) => town.name === name).id;
    formData.provinceFk = provinceFk;
    formData.countryFk = provincesOptions.value.find(
        (province) => province.id === provinceFk
    ).countryFk;
};

const onProvinceCreated = async ({ name }, formData) => {
    await provincesFetchDataRef.value.fetch();
    formData.provinceFk = provincesOptions.value.find(
        (province) => province.name === name
    ).id;
};
</script>

<template>
    <FetchData
        ref="townsFetchDataRef"
        @on-fetch="(data) => (townsLocationOptions = data)"
        auto-load
        url="Towns/location"
    />
    <FetchData
        ref="provincesFetchDataRef"
        @on-fetch="(data) => (provincesOptions = data)"
        auto-load
        url="Provinces"
    />
    <FetchData
        @on-fetch="(data) => (countriesOptions = data)"
        auto-load
        url="Countries"
    />
    <FormModelPopup
        url-create="postcodes"
        model="postcode"
        :title="t('New postcode')"
        :subtitle="t('Please, ensure you put the correct data!')"
        :form-initial-data="postcodeFormData"
        @on-data-saved="onDataSaved($event)"
    >
        <template #form-inputs="{ data, validate }">
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        :label="t('Postcode')"
                        v-model="data.code"
                        :rules="validate('postcode.code')"
                    />
                </div>
                <div class="col">
                    <VnSelectCreate
                        :label="t('City')"
                        :options="townsLocationOptions"
                        v-model="data.townFk"
                        hide-selected
                        option-label="name"
                        option-value="id"
                        :rules="validate('postcode.city')"
                        :roles-allowed-to-create="['deliveryAssistant']"
                    >
                        <template #form>
                            <CreateNewCityForm
                                @on-data-saved="onCityCreated($event, data)"
                            />
                        </template>
                    </VnSelectCreate>
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-xl">
                <div class="col">
                    <VnSelectCreate
                        :label="t('Province')"
                        :options="provincesOptions"
                        hide-selected
                        option-label="name"
                        option-value="id"
                        v-model="data.provinceFk"
                        :rules="validate('postcode.provinceFk')"
                        :roles-allowed-to-create="['deliveryAssistant']"
                    >
                        <template #form>
                            <CreateNewProvinceForm
                                @on-data-saved="onProvinceCreated($event, data)"
                            />
                        </template>
                    </VnSelectCreate>
                </div>
                <div class="col">
                    <VnSelectFilter
                        :label="t('Country')"
                        :options="countriesOptions"
                        hide-selected
                        option-label="country"
                        option-value="id"
                        v-model="data.countryFk"
                        :rules="validate('postcode.countryFk')"
                    />
                </div> </VnRow
        ></template>
    </FormModelPopup>
</template>

<i18n>
es:
    New postcode: Nuevo código postal
    Please, ensure you put the correct data!: ¡Por favor, asegúrese de poner los datos correctos!
    City: Población
    Province: Provincia
    Country: País
    Postcode: Código postal
</i18n>
