<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 VnSelect from 'src/components/common/VnSelect.vue';
import VnInput from 'src/components/common/VnInput.vue';
import FormModelPopup from './FormModelPopup.vue';

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

const { t } = useI18n();

const cityFormData = reactive({
    name: null,
    provinceFk: null,
});

const provincesOptions = ref([]);

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

<template>
    <FetchData
        @on-fetch="(data) => (provincesOptions = data)"
        auto-load
        url="Provinces"
    />
    <FormModelPopup
        :title="t('New city')"
        :subtitle="t('Please, ensure you put the correct data!')"
        :form-initial-data="cityFormData"
        url-create="towns"
        model="city"
        @on-data-saved="onDataSaved($event)"
    >
        <template #form-inputs="{ data, validate }">
            <VnRow>
                <VnInput
                    :label="t('Name')"
                    v-model="data.name"
                    :rules="validate('city.name')"
                />
                <VnSelect
                    :label="t('Province')"
                    :options="provincesOptions"
                    hide-selected
                    option-label="name"
                    option-value="id"
                    v-model="data.provinceFk"
                    :rules="validate('city.provinceFk')"
                />
            </VnRow>
        </template>
    </FormModelPopup>
</template>

<i18n>
es:
    New city: Nueva ciudad
    Please, ensure you put the correct data!: ¡Por favor, asegúrese de poner los datos correctos!
    Name: Nombre
    Province: Provincia
</i18n>
