<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 FormModel from 'components/FormModel.vue';

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

const { t } = useI18n();

const provinceFormData = reactive({
    name: null,
    autonomyFk: null,
});

const closeButton = ref(null);
const isLoading = ref(false);
const autonomiesOptions = ref([]);

const onDataSaved = () => {
    emit('onDataSaved');
    closeForm();
};

const closeForm = () => {
    if (closeButton.value) closeButton.value.click();
};
</script>

<template>
    <FetchData
        @on-fetch="(data) => (autonomiesOptions = data)"
        auto-load
        url="Autonomies"
    />
    <FormModel
        :form-initial-data="provinceFormData"
        :observe-form-changes="false"
        :default-actions="false"
        url-create="provinces"
        model="province"
        @on-data-saved="onDataSaved()"
    >
        <template #form="{ data, validate }">
            <span ref="closeButton" class="close-icon" v-close-popup>
                <QIcon name="close" size="sm" />
            </span>
            <h1 class="title">{{ t('New province') }}</h1>
            <p>{{ t('Please, ensure you put the correct data!') }}</p>
            <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>

            <div class="q-mt-lg row justify-end">
                <QBtn
                    :label="t('globals.save')"
                    type="submit"
                    color="primary"
                    :disabled="isLoading"
                    :loading="isLoading"
                />
                <QBtn
                    :label="t('globals.cancel')"
                    type="reset"
                    color="primary"
                    flat
                    class="q-ml-sm"
                    :disabled="isLoading"
                    :loading="isLoading"
                    v-close-popup
                />
            </div>
        </template>
    </FormModel>
</template>

<style lang="scss" scoped>
.close-icon {
    position: absolute;
    top: 20px;
    right: 20px;
    cursor: pointer;
}
.title {
    font-size: 17px;
    font-weight: bold;
    line-height: 20px;
}
</style>

<i18n>
es:
    New postcode: Nuevo código postal
    Please, ensure you put the correct data!: ¡Por favor, asegúrese de poner los datos correctos!
    Name: Nombre
    Autonomy: Autonomía
</i18n>
