<script setup>
import { onMounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { dateRange } from 'src/filters';
import VnFilterPanel from 'src/components/ui/VnFilterPanel.vue';
import VnSelect from 'src/components/common/VnSelect.vue';
import VnInput from 'src/components/common/VnInput.vue';
import FetchData from 'components/FetchData.vue';
import { useArrayData } from 'src/composables/useArrayData';
import VnSelectWorker from 'src/components/common/VnSelectWorker.vue';

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

const stateOptions = [
    { code: 'pending', name: t('pending') },
    { code: 'accepted', name: t('accepted') },
    { code: 'denied', name: t('denied') },
];
const arrayData = useArrayData(props.dataKey);
const fieldFiltersValues = ref([]);
const itemTypesOptions = ref([]);
const warehousesOptions = ref([]);

const exprBuilder = (param, value) => {
    switch (param) {
        case 'ticketFk':
        case 'quantity':
        case 'price':
        case 'isOk':
            return { [`tr.${param}`]: value };
        case 'attenderName':
            return { [`ua.name`]: value };
        case 'shipped':
            return {
                't.shipped': {
                    between: dateRange(value),
                },
            };
    }
};

onMounted(async () => {
    if (arrayData.store?.userParams) {
        fieldFiltersValues.value = Object.entries(arrayData.store.userParams).map(
            ([key, value]) => ({
                name: key,
                value,
                selectedField: { name: key, label: t(`params.${key}`) },
            })
        );
    }
    exprBuilder('state', arrayData.store?.userParams?.state);
});
</script>

<template>
    <FetchData
        url="TicketRequests/getItemTypeWorker"
        :filter="{ fields: ['id', 'nickname'], order: 'nickname ASC' }"
        @on-fetch="(data) => (itemTypesOptions = data)"
        auto-load
    />
    <FetchData
        url="warehouses"
        :filter="{ order: 'name ASC' }"
        @on-fetch="(data) => (warehousesOptions = data)"
        auto-load
    />
    <VnFilterPanel
        :data-key="props.dataKey"
        :search-button="true"
        :expr-builder="exprBuilder"
    >
        <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.ticketFk"
                        :label="t('params.ticketFk')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        v-model="params.attenderFk"
                        :label="t('params.attenderFk')"
                        @update:model-value="searchFn()"
                        :options="itemTypesOptions"
                        option-value="id"
                        option-label="nickname"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        v-model="params.clientFk"
                        :label="t('params.clientFk')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('params.warehouseFk')"
                        v-model="params.warehouseFk"
                        @update:model-value="searchFn()"
                        :options="warehousesOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelectWorker
                        :label="t('params.requesterFk')"
                        v-model="params.requesterFk"
                        @update:model-value="searchFn()"
                        :fields="['id', 'name']"
                        :params="{ departmentCodes: ['VT'] }"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('params.state')"
                        v-model="params.state"
                        @update:model-value="searchFn()"
                        :options="stateOptions"
                        option-value="code"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        :label="t('params.daysOnward')"
                        v-model="params.daysOnward"
                        lazy-rules
                        is-outlined
                    />
                </QItemSection>
            </QItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        search: General search
        ticketFk: Ticket id
        attenderFk: Atender
        clientFk: Client id
        warehouseFk: Warehouse
        requesterFk: Salesperson
        from: From
        to: To
        mine: For me
        state: State
        myTeam: My team
    dateFiltersTooltip: Cannot choose a range of dates and days onward at the same time
    denied: Denied
    accepted: Accepted
    pending: Pending
es:
    params:
        search: Búsqueda general
        ticketFk: Id ticket
        attenderFk: Comprador
        clientFk: Id cliente
        warehouseFk: Almacén
        requesterFk: Comercial
        from: Desde
        to: Hasta
        mine: Para mi
        state: Estado
        myTeam: Mi equipo
    dateFiltersTooltip: No se puede seleccionar un rango de fechas y días en adelante a la vez
    denied: Denegada
    accepted: Aceptada
    pending: Pendiente
</i18n>
