<script setup>
import { 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 VnInputDate from 'src/components/common/VnInputDate.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 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),
                },
            };
    }
};

const add = (paramsObj, key) => {
    if (paramsObj[key] === undefined) {
        paramsObj[key] = 1;
    } else {
        paramsObj[key]++;
    }
};

const decrement = (paramsObj, key) => {
    if (paramsObj[key] === 0) return;

    paramsObj[key]--;
};
</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 v-if="tag.label !== 'state'">{{ formatFn(tag.value) }}</span>
                <span v-else>{{ t(`${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>
                    <VnSelect
                        :label="t('params.requesterFk')"
                        v-model="params.requesterFk"
                        @update:model-value="searchFn()"
                        url="Workers/search"
                        :fields="['id', 'name']"
                        order="name ASC"
                        :params="{ departmentCodes: ['VT'] }"
                        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?.nickname }},
                                        {{ scope.opt?.code }}</QItemLabel
                                    >
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelect>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <QCheckbox
                        v-model="params.myTeam"
                        :label="t('params.myTeam')"
                        @update:model-value="searchFn()"
                        toggle-indeterminate
                    />
                </QItemSection>
            </QItem>
            <QCard bordered>
                <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>
                <QItem>
                    <QItemSection>
                        <VnInput
                            v-model="params.scopeDays"
                            :label="t('globals.daysOnward')"
                            type="number"
                            dense
                            outlined
                            rounded
                            :min="0"
                        >
                            <template #append>
                                <QBtn
                                    icon="add"
                                    flat
                                    dense
                                    size="12px"
                                    shortcut="+"
                                    @click="add(params, 'scopeDays')"
                                />
                                <QBtn
                                    icon="remove"
                                    flat
                                    dense
                                    size="12px"
                                    @click="decrement(params, 'scopeDays')"
                                />
                            </template>
                        </VnInput>
                    </QItemSection>
                </QItem>
                <QIcon name="info" style="position: absolute; top: 4px; right: 4px">
                    <QTooltip max-width="300px">
                        {{ t('dateFiltersTooltip') }}
                    </QTooltip>
                </QIcon>
            </QCard>
            <QItem>
                <QItemSection>
                    <QCheckbox
                        :label="t('params.mine')"
                        v-model="params.mine"
                        toggle-indeterminate
                    />
                </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>
        </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>
