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

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

const provinces = ref([]);
const states = ref([]);
const agencies = ref([]);
const warehouses = ref([]);
const groupedStates = ref([]);

const getGroupedStates = (data) => {
    for (const state of data) {
        groupedStates.value.push({
            id: state.id,
            name: t(`${state.code}`),
            code: state.code,
        });
    }
};
</script>

<template>
    <FetchData url="Provinces" @on-fetch="(data) => (provinces = data)" auto-load />
    <FetchData url="States" @on-fetch="(data) => (states = data)" auto-load />
    <FetchData
        url="AlertLevels"
        @on-fetch="
            (data) => {
                getGroupedStates(data);
            }
        "
        auto-load
    />
    <FetchData url="AgencyModes" @on-fetch="(data) => (agencies = data)" auto-load />
    <FetchData url="Warehouses" @on-fetch="(data) => (warehouses = data)" auto-load />
    <VnFilterPanel :data-key="props.dataKey" :search-button="true" search-url="table">
        <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.clientFk"
                        :label="t('Customer ID')"
                        is-outlined
                    />
                </QItemSection>
                <QItemSection>
                    <VnInput
                        v-model="params.orderFk"
                        :label="t('Order ID')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInputDate v-model="params.from" :label="t('From')" is-outlined />
                </QItemSection>
                <QItemSection>
                    <VnInputDate v-model="params.to" :label="t('To')" is-outlined />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnSelect
                        :label="t('Salesperson')"
                        v-model="params.salesPersonFk"
                        url="Workers/activeWithInheritedRole"
                        :where="{ role: 'salesPerson' }"
                        option-value="id"
                        option-label="firstName"
                        :use-like="false"
                        sort-by="firstName ASC"
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection v-if="!states">
                    <QSkeleton type="QInput" class="full-width" />
                </QItemSection>
                <QItemSection v-if="states">
                    <QSelect
                        :label="t('State')"
                        v-model="params.stateFk"
                        @update:model-value="searchFn()"
                        :options="states"
                        option-value="id"
                        option-label="name"
                        emit-value
                        map-options
                        use-input
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection v-if="!groupedStates">
                    <QSkeleton type="QInput" class="full-width" />
                </QItemSection>
                <QItemSection v-if="groupedStates">
                    <QSelect
                        :label="t('Grouped state')"
                        v-model="params.groupedStates"
                        @update:model-value="searchFn()"
                        :options="groupedStates"
                        option-value="id"
                        option-label="name"
                        emit-value
                        map-options
                        use-input
                        dense
                        outlined
                        rounded
                        sort-by="name ASC"
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        v-model="params.refFk"
                        :label="t('Invoice Ref.')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        v-model="params.scopeDays"
                        :label="t('Days onward')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        v-model="params.nickname"
                        :label="t('Nickname')"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <QCheckbox
                        v-model="params.myTeam"
                        @update:model-value="searchFn()"
                        :label="t('My team')"
                        toggle-indeterminate
                    />
                </QItemSection>
                <QItemSection>
                    <QCheckbox
                        v-model="params.pending"
                        @update:model-value="searchFn()"
                        :label="t('Pending')"
                        toggle-indeterminate
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <QCheckbox
                        v-model="params.hasInvoice"
                        @update:model-value="searchFn()"
                        :label="t('Invoiced')"
                        toggle-indeterminate
                    />
                </QItemSection>
                <QItemSection>
                    <QCheckbox
                        v-model="params.hasRoute"
                        @update:model-value="searchFn()"
                        :label="t('Routed')"
                        toggle-indeterminate
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <QCheckbox
                        v-model="params.problems"
                        @update:model-value="searchFn()"
                        :label="t('With problems')"
                        toggle-indeterminate
                    />
                </QItemSection>
            </QItem>
            <QSeparator />
            <QExpansionItem :label="t('More options')" expand-separator>
                <QItem>
                    <QItemSection v-if="!provinces">
                        <QSkeleton type="QInput" class="full-width" />
                    </QItemSection>
                    <QItemSection v-if="provinces">
                        <QSelect
                            :label="t('Province')"
                            v-model="params.provinceFk"
                            @update:model-value="searchFn()"
                            :options="provinces"
                            option-value="id"
                            option-label="name"
                            emit-value
                            map-options
                            use-input
                            dense
                            outlined
                            rounded
                        />
                    </QItemSection>
                </QItem>
                <QItem>
                    <QItemSection v-if="!agencies">
                        <QSkeleton type="QInput" class="full-width" />
                    </QItemSection>
                    <QItemSection v-if="agencies">
                        <QSelect
                            :label="t('Agency')"
                            v-model="params.agencyModeFk"
                            @update:model-value="searchFn()"
                            :options="agencies"
                            option-value="id"
                            option-label="name"
                            emit-value
                            map-options
                            use-input
                            dense
                            outlined
                            rounded
                        />
                    </QItemSection>
                </QItem>
                <QItem>
                    <QItemSection v-if="!warehouses">
                        <QSkeleton type="QInput" class="full-width" />
                    </QItemSection>
                    <QItemSection v-if="warehouses">
                        <QSelect
                            :label="t('Warehouse')"
                            v-model="params.warehouseFk"
                            @update:model-value="searchFn()"
                            :options="warehouses"
                            option-value="id"
                            option-label="name"
                            emit-value
                            map-options
                            use-input
                            dense
                            outlined
                            rounded
                        />
                    </QItemSection>
                </QItem>
                <QItem>
                    <QItemSection>
                        <VnInput
                            v-model="params.collectionFk"
                            :label="t('Collection')"
                            is-outlined
                        />
                    </QItemSection>
                </QItem>
            </QExpansionItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        search: Contains
        clientFk: Customer
        orderFk: Order
        from: From
        to: To
        salesPersonFk: Salesperson
        stateFk: State
        refFk: Invoice Ref.
        myTeam: My team
        pending: Pending
        hasInvoice: Invoiced
        hasRoute: Routed
        provinceFk: Province
        agencyModeFk: Agency
        warehouseFk: Warehouse
    FREE: Free
    ON_PREPARATION: On preparation
    PACKED: Packed
    DELIVERED: Delivered
    ON_PREVIOUS: ON_PREVIOUS
es:
    params:
        search: Contiene
        clientFk: Cliente
        orderFk: Pedido
        from: Desde
        to: Hasta
        salesPersonFk: Comercial
        stateFk: Estado
        refFk: Ref. Factura
        myTeam: Mi equipo
        pending: Pendiente
        hasInvoice: Facturado
        hasRoute: Enrutado
    Customer ID: ID Cliente
    Order ID: ID Pedido
    From: Desde
    To: Hasta
    Salesperson: Comercial
    State: Estado
    Invoice Ref.: Ref. Factura
    My team: Mi equipo
    Pending: Pendiente
    With problems: Con problemas
    Invoiced: Facturado
    Routed: Enrutado
    More options: Más opciones
    Province: Provincia
    Agency: Agencia
    Warehouse: Almacén
    Yes: Si
    No: No
    Days onward: Días adelante
    Grouped state: Estado agrupado
    FREE: Libre
    ON_PREPARATION: En preparación
    PACKED: Encajado
    DELIVERED: Servido
    ON_PREVIOUS: ON_PREVIOUS
    Collection: Colección
    Nickname: Nombre mostrado
</i18n>
