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

import VnRow from 'components/ui/VnRow.vue';
import VnSelectProvince from 'components/VnSelectProvince.vue';
import VnInput from 'components/common/VnInput.vue';
import FormModelPopup from './FormModelPopup.vue';

const emit = defineEmits(['onDataSaved']);
const $props = defineProps({
    countryFk: {
        type: Number,
        default: null,
    },
    provinceSelected: {
        type: Number,
        default: null,
    },
    provinces: {
        type: Array,
        default: () => [],
    },
});
const { t } = useI18n();

const cityFormData = ref({
    name: null,
    provinceFk: null,
});
onMounted(() => {
    cityFormData.value.provinceFk = $props.provinceSelected;
});
const onDataSaved = (...args) => {
    emit('onDataSaved', ...args);
};
</script>

<template>
    <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"
    >
        <template #form-inputs="{ data, validate }">
            <VnRow>
                <VnInput
                    :label="t('Names')"
                    v-model="data.name"
                    :rules="validate('city.name')"
                />
                <VnSelectProvince
                    :province-selected="$props.provinceSelected"
                    :country-fk="$props.countryFk"
                    v-model="data.provinceFk"
                    :provinces="$props.provinces"
                />
            </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>
