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

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

const warehousesOptions = ref([]);
const continentsOptions = ref([]);
const agenciesOptions = ref([]);

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="Warehouses"
        @on-fetch="(data) => (warehousesOptions = data)"
        auto-load
    />
    <FetchData
        url="Continents"
        @on-fetch="(data) => (continentsOptions = data)"
        auto-load
    />
    <FetchData
        url="AgencyModes"
        @on-fetch="(data) => (agenciesOptions = 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>
                    <VnSelectFilter
                        :label="t('params.agencyModeFk')"
                        v-model="params.agencyModeFk"
                        @update:model-value="searchFn()"
                        :options="agenciesOptions"
                        option-value="agencyFk"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelectFilter
                        :label="t('params.warehouseOutFk')"
                        v-model="params.warehouseOutFk"
                        @update:model-value="searchFn()"
                        :options="warehousesOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelectFilter
                        :label="t('params.warehouseInFk')"
                        v-model="params.warehouseInFk"
                        @update:model-value="searchFn()"
                        :options="warehousesOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <QInput
                        v-model="params.scopeDays"
                        type="number"
                        :label="t('params.scopeDays')"
                        dense
                        outlined
                        rounded
                        class="input-number"
                    >
                        <template #append>
                            <QBtn
                                icon="add"
                                flat
                                dense
                                size="12px"
                                @click="add(params, 'scopeDays')"
                            />
                            <QBtn
                                icon="remove"
                                flat
                                dense
                                size="12px"
                                @click="decrement(params, 'scopeDays')"
                            />
                        </template>
                    </QInput>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInputDate
                        v-model="params.landedFrom"
                        :label="t('params.landedFrom')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInputDate
                        :label="t('params.landedTo')"
                        v-model="params.landedTo"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelectFilter
                        :label="t('params.continent')"
                        v-model="params.continent"
                        @update:model-value="searchFn()"
                        :options="continentsOptions"
                        option-value="code"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <QInput
                        v-model="params.totalEntries"
                        type="number"
                        :label="t('params.totalEntries')"
                        dense
                        outlined
                        rounded
                        min="0"
                        class="input-number"
                    >
                        <template #append>
                            <QBtn
                                icon="add"
                                flat
                                dense
                                size="12px"
                                @click="add(params, 'totalEntries')"
                            />
                            <QBtn
                                icon="remove"
                                flat
                                dense
                                size="12px"
                                @click="decrement(params, 'totalEntries')"
                            />
                        </template>
                    </QInput>
                </QItemSection>
            </QItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        search: Id/Reference
        agencyModeFk: Agency
        warehouseInFk: Warehouse In
        warehouseOutFk: Warehouse Out
        scopeDays: Days onward
        landedFrom: Landed from
        landedTo: Landed to
        continent: Continent out
        totalEntries: Total entries
es:
    params:
        search: Id/Referencia
        agencyModeFk: Agencia
        warehouseInFk: Alm. entrada
        warehouseOutFk: Alm. salida
        scopeDays: Días adelante
        landedFrom: Llegada desde
        landedTo: Llegada hasta
        continent: Cont. Salida
        totalEntries: Ent. totales

</i18n>
