<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 provinceFormData = reactive({
    name: null,
    autonomyFk: null,
});

const autonomiesOptions = ref([]);

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

<template>
    <FetchData
        @on-fetch="(data) => (autonomiesOptions = data)"
        auto-load
        url="Autonomies"
    />
    <FormModelPopup
        :title="t('New province')"
        :subtitle="t('Please, ensure you put the correct data!')"
        url-create="provinces"
        model="province"
        :form-initial-data="provinceFormData"
        @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('province.name')"
                    />
                </div>
                <div class="col">
                    <VnSelectFilter
                        :label="t('Autonomy')"
                        :options="autonomiesOptions"
                        hide-selected
                        option-label="name"
                        option-value="id"
                        v-model="data.autonomyFk"
                        :rules="validate('province.autonomyFk')"
                    />
                </div>
            </VnRow>
        </template>
    </FormModelPopup>
</template>

<i18n>
es:
    New province: Nueva provincia
    Please, ensure you put the correct data!: ¡Por favor, asegúrese de poner los datos correctos!
    Name: Nombre
    Autonomy: Autonomía
</i18n>
