<script setup>
import { reactive, ref, onMounted, nextTick } from 'vue';
import { useI18n } from 'vue-i18n';
import VnInput from 'src/components/common/VnInput.vue';

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

const props = defineProps({
    showEntityField: {
        type: Boolean,
        default: true,
    },
});

const emit = defineEmits(['onDataSaved']);
const { t } = useI18n();
const bicInputRef = ref(null);
const bankEntityFormData = reactive({
    name: null,
    bic: null,
    countryFk: null,
    id: null,
});

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

const countriesOptions = ref([]);

const onDataSaved = (formData, requestResponse) => {
    emit('onDataSaved', formData, requestResponse);
};

onMounted(async () => {
    await nextTick();
    bicInputRef.value.focus();
});
</script>

<template>
    <FetchData
        url="Countries"
        :filter="countriesFilter"
        auto-load
        @on-fetch="(data) => (countriesOptions = data)"
    />
    <FormModelPopup
        url-create="bankEntities"
        model="bankEntity"
        :title="t('title')"
        :subtitle="t('subtitle')"
        :form-initial-data="bankEntityFormData"
        @on-data-saved="onDataSaved"
    >
        <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"
                        :required="true"
                        :rules="validate('bankEntity.name')"
                    />
                </div>
                <div class="col">
                    <VnInput
                        ref="bicInputRef"
                        :label="t('swift')"
                        v-model="data.bic"
                        :required="true"
                        :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
                        :required="true"
                        :rules="validate('bankEntity.countryFk')"
                    />
                </div>
                <div v-if="showEntityField" class="col">
                    <VnInput
                        :label="t('id')"
                        v-model="data.id"
                        :required="true"
                        :rules="validate('city.name')"
                    />
                </div>
            </VnRow>
        </template>
    </FormModelPopup>
</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>
