<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';
import VnInputDate from 'src/components/common/VnInputDate.vue';

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

const buyersOptions = ref([]);
const itemTypesOptions = ref([]);
const itemCategoriesOptions = ref([]);
</script>

<template>
    <FetchData
        url="TicketRequests/getItemTypeWorker"
        :filter="{
            fields: ['id', 'nickname'],
            order: 'nickname ASC',
        }"
        @on-fetch="(data) => (buyersOptions = data)"
        auto-load
    />
    <FetchData
        url="ItemTypes"
        :filter="{
            fields: ['id', 'name', 'categoryFk'],
            include: 'category',
            order: 'name ASC',
        }"
        @on-fetch="(data) => (itemTypesOptions = data)"
        auto-load
    />
    <FetchData
        url="ItemCategories"
        :filter="{
            fields: ['id', 'name'],
            order: 'name ASC',
        }"
        @on-fetch="(data) => (itemCategoriesOptions = data)"
        auto-load
    />
    <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>
                <QItemSection>
                    <VnInput
                        v-model="params.search"
                        :label="t('params.search')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        v-model="params.itemId"
                        :label="t('params.itemId')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelectFilter
                        :label="t('params.buyerId')"
                        v-model="params.buyerId"
                        @update:model-value="searchFn()"
                        :options="buyersOptions"
                        option-value="id"
                        option-label="nickname"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelectFilter
                        :label="t('params.typeId')"
                        v-model="params.typeId"
                        @update:model-value="searchFn()"
                        :options="itemTypesOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    >
                        <template #option="scope">
                            <QItem v-bind="scope.itemProps">
                                <QItemSection>
                                    <QItemLabel>{{ scope.opt?.name }}</QItemLabel>
                                    <QItemLabel caption>{{
                                        scope.opt?.category?.name
                                    }}</QItemLabel>
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelectFilter>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelectFilter
                        :label="t('params.categoryId')"
                        v-model="params.categoryId"
                        @update:model-value="searchFn()"
                        :options="itemCategoriesOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInputDate
                        :label="t('params.from')"
                        v-model="params.from"
                        @update:model-value="searchFn()"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInputDate
                        :label="t('params.to')"
                        v-model="params.to"
                        @update:model-value="searchFn()"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        search: General search
        itemId: Item id
        buyerId: Buyer
        typeId: Type
        categoryId: Category
        from: From
        to: To
es:
    params:
        search: Búsqueda general
        itemId: Id Artículo
        buyerId: Comprador
        typeId: Tipo
        categoryId: Reino
        from: Desde
        to: Hasta
</i18n>
