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

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

const warehousesOptions = ref([]);
const continentsOptions = ref([]);
const agenciesOptions = ref([]);
const warehousesByContinent = 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]--;
};

const warehouses = async () => {
    const warehousesResponse = await axios.get('Warehouses');
    const countriesResponse = await axios.get('Countries');
    const continentsResponse = await axios.get('Continents');

    const countryContinentMap = countriesResponse.data.reduce((acc, country) => {
        acc[country.id] = country.continentFk;
        return acc;
    }, {});

    continentsResponse.data.forEach((continent) => {
        const countriesInContinent = Object.keys(countryContinentMap).filter(
            (countryId) => countryContinentMap[countryId] === continent.id.toString()
        );

        warehousesByContinent.value[continent.code] = warehousesResponse.data.filter(
            (warehouse) => countriesInContinent.includes(warehouse.countryFk.toString())
        );
    });
};
warehouses();
</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 label="id" v-model="params.id" is-outlined />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        :label="t('params.reference')"
                        v-model="params.reference"
                        is-outlined
                    />
                </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"
                                shortcut="+"
                                flat
                                dense
                                size="12px"
                                @click="add(params, 'totalEntries')"
                            />
                            <QBtn
                                icon="remove"
                                flat
                                dense
                                size="12px"
                                @click="decrement(params, 'totalEntries')"
                            />
                        </template>
                    </QInput>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('params.agencyModeFk')"
                        v-model="params.agencyModeFk"
                        :options="agenciesOptions"
                        option-value="agencyFk"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInputDate
                        :label="t('params.shippedFrom')"
                        v-model="params.shippedFrom"
                        @update:model-value="searchFn()"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInputDate
                        :label="t('params.landedTo')"
                        v-model="params.landedTo"
                        @update:model-value="searchFn()"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem v-if="warehousesByContinent[params.continent]">
                <QItemSection>
                    <VnSelect
                        :label="t('params.warehouseOutFk')"
                        v-model="params.warehouseOutFk"
                        :options="warehousesByContinent[params.continent]"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem v-else>
                <QItemSection>
                    <VnSelect
                        :label="t('params.warehouseOutFk')"
                        v-model="params.warehouseOutFk"
                        :options="warehousesOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('params.warehouseInFk')"
                        v-model="params.warehouseInFk"
                        :options="warehousesOptions"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('globals.pageTitles.supplier')"
                        v-model="params.cargoSupplierFk"
                        url="Suppliers"
                        option-value="id"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('params.continent')"
                        v-model="params.continent"
                        :options="continentsOptions"
                        option-value="code"
                        option-label="name"
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        id: Id
        reference: Reference
        totalEntries: Total entries
        agencyModeFk: Agency
        warehouseInFk: Warehouse In
        warehouseOutFk: Warehouse Out
        shippedFrom: Shipped from
        landedTo: Landed to
        cargoSupplierFk: Supplier
        continent: Continent out
es:
    params:
        id: Id
        reference: Referencia
        totalEntries: Ent. totales
        agencyModeFk: Agencia
        warehouseInFk: Alm. entrada
        warehouseOutFk: Alm. salida
        shippedFrom: Llegada desde
        landedTo: Llegada hasta
        cargoSupplierFk: Proveedor
        continent: Cont. Salida
</i18n>
