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

import VnSelectFilter from 'src/components/common/VnSelectFilter.vue';
import FetchData from 'components/FetchData.vue';
import FormModel from 'components/FormModel.vue';
import VnRow from 'components/ui/VnRow.vue';

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

const { t } = useI18n();

const bankEntityFormData = reactive({
    name: null,
    bic: null,
    countryFk: null,
    id: null,
});

const countriesFilter = {
    fields: ['id', 'country', 'code'],
};

const closeButton = ref(null);
const countriesOptions = ref([]);
const loading = ref(false);

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

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

<template>
    <FetchData
        url="Countries"
        :filter="countriesFilter"
        auto-load
        @on-fetch="(data) => (countriesOptions = data)"
    />
    <FormModel
        :form-initial-data="bankEntityFormData"
        :observe-form-changes="false"
        :default-actions="false"
        url-create="bankEntities"
        model="bankEntity"
        @on-data-saved="onDataSaved($event)"
    >
        <template #form="{ data, validate }">
            <span ref="closeButton" class="popup-form-close-button" v-close-popup>
                <QIcon name="close" size="sm" />
            </span>
            <h1 class="popup-form-title">{{ t('title') }}</h1>
            <p class="q-mb-md">{{ t('subtitle') }}</p>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <QInput
                        :label="t('name')"
                        v-model="data.name"
                        :rules="validate('bankEntity.name')"
                    />
                </div>
                <div class="col">
                    <QInput
                        :label="t('swift')"
                        v-model="data.bic"
                        :rules="validate('bankEntity.bic')"
                    />
                </div>
            </VnRow>
            <VnRow class="row q-gutter-md q-mb-md">
                <div class="col">
                    <VnSelectFilter
                        :label="t('country')"
                        v-model="data.countryFk"
                        :options="countriesOptions"
                        option-value="id"
                        option-label="country"
                        hide-selected
                        :rules="validate('bankEntity.countryFk')"
                    />
                </div>
                <div class="col">
                    <QInput :label="t('id')" v-model="data.id" />
                </div>
            </VnRow>
            <div class="q-mt-lg row justify-end">
                <QBtn
                    :label="t('globals.save')"
                    type="submit"
                    color="primary"
                    :disabled="loading"
                    :loading="loading"
                />
                <QBtn
                    :label="t('globals.cancel')"
                    type="reset"
                    color="primary"
                    flat
                    class="q-ml-sm"
                    :disabled="loading"
                    :loading="loading"
                    v-close-popup
                />
            </div>
        </template>
    </FormModel>
</template>

<i18n>
en:
    title: New bank entity
    subtitle: Please, ensure you put the correct data!
    name: Name *
    swift: Swift *
    country: Country
    id: Entity code
es:
    title: Nueva entidad bancaria
    subtitle: ¡Por favor, asegúrate de poner los datos correctos!
    name: Nombre *
    swift: Swift *
    country: País
    id: Código de la entidad
</i18n>
