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

import axios from 'axios';
import { onMounted } from 'vue';

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

const warehousesOptions = ref([]);
const itemPackingTypes = ref([]);
const stateOptions = ref([]);

const getItemPackingTypes = async () => {
    try {
        const filter = {
            where: { isActive: true },
        };
        const { data } = await axios.get('ItemPackingTypes', {
            params: { filter: JSON.stringify(filter) },
        });
        itemPackingTypes.value = data.map((ipt) => ({
            description: t(ipt.description),
            code: ipt.code,
        }));
    } catch (error) {
        console.error(error);
    }
};

const getGroupedStates = async () => {
    try {
        const { data } = await axios.get('AlertLevels');
        stateOptions.value = data.map((state) => ({
            id: state.id,
            name: t(`futureTickets.${state.code}`),
            code: state.code,
        }));
    } catch (error) {
        console.error(error);
    }
};

onMounted(async () => {
    getItemPackingTypes();
    getGroupedStates();
});
</script>

<template>
    <FetchData
        url="Warehouses"
        @on-fetch="(data) => (warehousesOptions = data)"
        auto-load
    />
    <VnFilterPanel
        :data-key="props.dataKey"
        :hidden-tags="['search']"
        :un-removable-params="['warehouseFk', 'originScopeDays ', 'futureScopeDays']"
    >
        <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-my-sm">
                <QItemSection>
                    <VnInputDate
                        v-model="params.originScopeDays"
                        :label="t('params.originScopeDays')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection>
                    <VnInputDate
                        v-model="params.futureScopeDays"
                        :label="t('params.futureScopeDays')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection>
                    <VnInput
                        :label="t('params.litersMax')"
                        v-model="params.litersMax"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection>
                    <VnInput
                        :label="t('params.linesMax')"
                        v-model="params.linesMax"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('params.ipt')"
                        v-model="params.ipt"
                        :options="itemPackingTypes"
                        option-value="code"
                        option-label="description"
                        :info="t('iptInfo')"
                        @update:model-value="searchFn()"
                        dense
                        outlined
                        rounded
                    >
                    </VnSelect>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('params.futureIpt')"
                        v-model="params.futureIpt"
                        :options="itemPackingTypes"
                        option-value="code"
                        option-label="description"
                        :info="t('iptInfo')"
                        @update:model-value="searchFn()"
                        dense
                        outlined
                        rounded
                    >
                    </VnSelect>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('params.state')"
                        v-model="params.state"
                        :options="stateOptions"
                        option-value="id"
                        option-label="name"
                        @update:model-value="searchFn()"
                        dense
                        outlined
                        rounded
                    >
                    </VnSelect>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('params.futureState')"
                        v-model="params.futureState"
                        :options="stateOptions"
                        option-value="id"
                        option-label="name"
                        @update:model-value="searchFn()"
                        dense
                        outlined
                        rounded
                    >
                    </VnSelect>
                </QItemSection>
            </QItem>

            <QItem>
                <QItemSection>
                    <QCheckbox
                        :label="t('params.problems')"
                        v-model="params.problems"
                        :toggle-indeterminate="false"
                        @update:model-value="searchFn()"
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('params.warehouseFk')"
                        v-model="params.warehouseFk"
                        :options="warehousesOptions"
                        option-value="id"
                        option-label="name"
                        @update:model-value="searchFn()"
                        dense
                        outlined
                        rounded
                    >
                    </VnSelect>
                </QItemSection>
            </QItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    iptInfo: IPT
    params:
        originScopeDays: Origin date
        futureScopeDays: Destination date
        futureIpt: Destination IPT
        ipt: Origin IPT
        warehouseFk: Warehouse
        litersMax: Max liters
        linesMax: Max lines
        state: Origin grouped state
        futureState: Destination grouped state
        problems: With problems
es:
    Horizontal: Horizontal
    Vertical: Vertical
    iptInfo: Encajado
    params:
        originScopeDays: Fecha origen
        futureScopeDays: Fecha destino
        futureIpt: IPT destino
        ipt: IPT Origen
        warehouseFk: Almacén
        litersMax: Litros máx.
        linesMax: Líneas máx.
        state: Estado agrupado origen
        futureState: Estado agrupado destino
        problems: Con problemas
</i18n>
