<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 axios from 'axios';
import { onMounted } from 'vue';
import VnInputNumber from 'src/components/common/VnInputNumber.vue';

const { t, te } = useI18n();
const props = defineProps({
    dataKey: {
        type: String,
        required: true,
    },
    exprBuilder: {
        type: Function,
        default: null,
    },
});

const warehousesOptions = ref([]);
const itemPackingTypes = 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 getLocale = (val) => {
    const param = `params.${val}`;
    return te(param) ? t(param) : t(`globals.${param}`);
};

onMounted(async () => await getItemPackingTypes());
</script>

<template>
    <FetchData
        url="Warehouses"
        @on-fetch="(data) => (warehousesOptions = data)"
        auto-load
    />
    <VnFilterPanel
        :data-key="props.dataKey"
        :search-button="true"
        :hidden-tags="['search']"
        :unremovable-params="['warehouseFk', 'dateFuture', 'dateToAdvance']"
    >
        <template #tags="{ tag, formatFn }">
            <div class="q-gutter-x-xs">
                <strong>{{ getLocale(tag.label) }}: </strong>
                <span>{{ formatFn(tag.value) }}</span>
            </div>
        </template>
        <template #body="{ params, searchFn }">
            <QItem class="q-my-sm">
                <QItemSection>
                    <VnInputDate
                        v-model="params.dateFuture"
                        :label="t('params.dateFuture')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection>
                    <VnInputDate
                        v-model="params.dateToAdvance"
                        :label="t('params.dateToAdvance')"
                        is-outlined
                    />
                </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.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>
                    <VnInputNumber
                        v-model="params.scopeDays"
                        :label="t('Days onward')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <QCheckbox
                        :label="t('params.isFullMovable')"
                        v-model="params.isFullMovable"
                        toggle-indeterminate
                        @update:model-value="searchFn()"
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('globals.params.departmentFk')"
                        v-model="params.departmentFk"
                        url="Departments"
                        :fields="['id', 'name']"
                        dense
                        outlined
                        rounded
                    />
                </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:
        dateFuture: Origin date
        dateToAdvance: Destination date
        futureIpt: Origin IPT
        ipt: Destination IPT
        isFullMovable: 100% movable
        warehouseFk: Warehouse
es:
    Horizontal: Horizontal
    Vertical: Vertical
    iptInfo: Encajado
    params:
        dateFuture: fecha origen
        dateToAdvance: Fecha destino
        futureIpt: IPT Origen
        ipt: IPT destino
        isFullMovable: 100% movible
        warehouseFk: Almacén
</i18n>
