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

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

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

const cities = ref();
const clients = ref();
</script>

<template>
    <FetchData
        :filter="{ where: { role: 'socialName' } }"
        @on-fetch="(data) => (clients = data)"
        auto-load
        url="Clients"
    />
    <FetchData @on-fetch="(data) => (cities = data)" auto-load url="Towns" />
    <VnFilterPanel :data-key="props.dataKey" :search-button="true">
        <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 class="q-mb-sm q-mt-sm">
                <QItemSection>
                    <VnInput
                        :label="t('Identifier')"
                        is-outlined
                        v-model="params.identifier"
                    />
                </QItemSection>
            </QItem>

            <QItem class="q-mb-sm">
                <QItemSection v-if="!clients">
                    <QSkeleton type="QInput" class="full-width" />
                </QItemSection>
                <QItemSection v-if="clients">
                    <VnSelectFilter
                        :input-debounce="0"
                        :label="t('Social name')"
                        :options="clients"
                        @update:model-value="searchFn()"
                        dense
                        emit-value
                        hide-selected
                        map-options
                        option-label="socialName"
                        option-value="socialName"
                        outlined
                        rounded
                        use-input
                        v-model="params.socialName"
                    />
                </QItemSection>
            </QItem>

            <QItem class="q-mb-sm">
                <QItemSection v-if="!cities">
                    <QSkeleton type="QInput" class="full-width" />
                </QItemSection>
                <QItemSection v-if="cities">
                    <VnSelectFilter
                        :input-debounce="0"
                        :label="t('City')"
                        :options="cities"
                        @update:model-value="searchFn()"
                        dense
                        emit-value
                        hide-selected
                        map-options
                        option-label="name"
                        option-value="name"
                        outlined
                        rounded
                        use-input
                        v-model="params.city"
                    />
                </QItemSection>
            </QItem>

            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnInput :label="t('Phone')" is-outlined v-model="params.phone" />
                </QItemSection>
            </QItem>

            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnInput :label="t('Email')" is-outlined v-model="params.email" />
                </QItemSection>
            </QItem>
            <QSeparator />
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        identifier: Identifier
        socialName: Social name
        city: City
        phone: Phone
        email: Email
es:
    params:
        identifier: Identificador
        socialName: Razón social
        city: Población
        phone: Teléfono
        email: Email
    Identifier: Identificador
    Social name: Razón social
    City: Población
    Phone: Teléfono
    Email: Email
</i18n>
