<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 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 class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnInput
                        :label="t('Name')"
                        v-model="data.name"
                        :rules="validate('city.name')"
                    />
                </div>
                <div class="col">
                    <VnSelectFilter
                        :label="t('Province')"
                        :options="provincesOptions"
                        hide-selected
                        option-label="name"
                        option-value="id"
                        v-model="data.provinceFk"
                        :rules="validate('city.provinceFk')"
                    />
                </div>
            </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>
