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

import VnFilterPanel from 'src/components/ui/VnFilterPanel.vue';
import VnSelectFilter from 'src/components/common/VnSelectFilter.vue';
import VnInput from 'src/components/common/VnInput.vue';
import FetchData from 'components/FetchData.vue';

const props = defineProps({
    dataKey: {
        type: String,
        required: true,
    },
});

const { t } = useI18n();

const provincesOptions = ref([]);
const countriesOptions = ref([]);
</script>

<template>
    <FetchData
        url="Provinces"
        :filter="{ fields: ['id', 'name'], order: 'name ASC', limit: 30 }"
        @on-fetch="(data) => (provincesOptions = data)"
        auto-load
    />
    <FetchData
        url="countries"
        :filter="{ fields: ['id', 'country'], order: 'country ASC', limit: 30 }"
        @on-fetch="(data) => (countriesOptions = data)"
        auto-load
    />
    <VnFilterPanel
        :data-key="props.dataKey"
        :search-button="true"
        :unremovable-params="['supplierFk']"
    >
        <template #tags="{ tag, formatFn }">
            <div class="q-gutter-x-xs">
                <strong>{{ t(`params.${tag.label}`) }}: </strong>
                <span>{{ formatFn(tag.value) }}</span>
            </div>
        </template>
        <template #body="{ params, searchFn }">
            <QItem>
                <QItemSection>
                    <VnInput
                        v-model="params.search"
                        :label="t('params.search')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        v-model="params.nickname"
                        :label="t('params.nickname')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput v-model="params.nif" :label="t('params.nif')" is-outlined />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelectFilter
                        :label="t('params.provinceFk')"
                        v-model="params.provinceFk"
                        @update:model-value="searchFn()"
                        :options="provincesOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelectFilter
                        :label="t('params.countryFk')"
                        v-model="params.countryFk"
                        @update:model-value="searchFn()"
                        :options="countriesOptions"
                        option-value="id"
                        option-label="country"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        search: General search
        nickname: Alias
        nif: Tax number
        provinceFk: Province
        countryFk: Country
es:
    params:
        search: Búsqueda general
        nickname: Alias
        nif: NIF/CIF
        provinceFk: Provincia
        countryFk: País
</i18n>
