<script setup>
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';
import FetchData from 'components/FetchData.vue';
import VnFilterPanel from 'components/ui/VnFilterPanel.vue';
import VnSelectFilter from 'components/common/VnSelectFilter.vue';
import VnInputDate from 'components/common/VnInputDate.vue';
import VnInput from 'components/common/VnInput.vue';

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

const emit = defineEmits(['search']);

const workerList = ref([]);
const agencyList = ref([]);
const vehicleList = ref([]);
const warehouseList = ref([]);
</script>

<template>
    <FetchData
        url="Workers/search"
        :filter="{ fields: ['id', 'nickname'] }"
        sort-by="nickname ASC"
        limit="30"
        @on-fetch="(data) => (workerList = data)"
        auto-load
    />
    <FetchData
        url="AgencyModes/isActive"
        :filter="{ fields: ['id', 'name'] }"
        sort-by="name ASC"
        limit="30"
        @on-fetch="(data) => (agencyList = data)"
        auto-load
    />
    <FetchData
        url="Vehicles"
        :filter="{ fields: ['id', 'numberPlate'] }"
        sort-by="numberPlate ASC"
        limit="30"
        @on-fetch="(data) => (vehicleList = data)"
        auto-load
    />
    <FetchData url="Warehouses" @on-fetch="(data) => (warehouseList = data)" auto-load />
    <VnFilterPanel
        :data-key="props.dataKey"
        :search-button="true"
        @search="emit('search')"
    >
        <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 }">
            <QItem class="q-my-sm">
                <QItemSection v-if="workerList">
                    <VnSelectFilter
                        :label="t('Worker')"
                        v-model="params.workerFk"
                        :options="workerList"
                        option-value="id"
                        option-label="nickname"
                        dense
                        outlined
                        rounded
                        emit-value
                        map-options
                        use-input
                        :input-debounce="0"
                    >
                        <template #option="{ itemProps, opt }">
                            <QItem v-bind="itemProps">
                                <QItemSection>
                                    <QItemLabel>{{ opt.name }}</QItemLabel>
                                    <QItemLabel caption>
                                        {{ opt.nickname }},{{ opt.code }}
                                    </QItemLabel>
                                </QItemSection>
                            </QItem>
                        </template>
                    </VnSelectFilter>
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection v-if="agencyList">
                    <VnSelectFilter
                        :label="t('Agency')"
                        v-model="params.agencyModeFk"
                        :options="agencyList"
                        option-value="id"
                        option-label="name"
                        dense
                        outlined
                        rounded
                        emit-value
                        map-options
                        use-input
                        :input-debounce="0"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection>
                    <VnInputDate
                        v-model="params.from"
                        :label="t('From')"
                        is-outlined
                        :disable="Boolean(params.scopeDays)"
                        @update:model-value="params.scopeDays = null"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection>
                    <VnInputDate
                        v-model="params.to"
                        :label="t('To')"
                        is-outlined
                        :disable="Boolean(params.scopeDays)"
                        @update:model-value="params.scopeDays = null"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection>
                    <VnInput
                        v-model="params.scopeDays"
                        type="number"
                        :label="t('Days Onward')"
                        is-outlined
                        clearable
                        :disable="Boolean(params.from || params.to)"
                        @update:model-value="
                            params.to = null;
                            params.from = null;
                        "
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection v-if="vehicleList">
                    <VnSelectFilter
                        :label="t('Vehicle')"
                        v-model="params.vehicleFk"
                        :options="vehicleList"
                        option-value="id"
                        option-label="numberPlate"
                        dense
                        outlined
                        rounded
                        emit-value
                        map-options
                        use-input
                        :input-debounce="0"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection>
                    <VnInput v-model="params.m3" label="m³" is-outlined clearable />
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection v-if="vehicleList">
                    <VnSelectFilter
                        :label="t('Warehouse')"
                        v-model="params.warehouseFk"
                        :options="warehouseList"
                        option-value="id"
                        option-label="name"
                        dense
                        outlined
                        rounded
                        emit-value
                        map-options
                        use-input
                        :input-debounce="0"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection>
                    <VnInput
                        v-model="params.description"
                        :label="t('Description')"
                        is-outlined
                        clearable
                    />
                </QItemSection>
            </QItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        warehouseFk: Warehouse
        description: Description
        m3: m³
        vehicleFk: Vehicle
        agencyModeFk: Agency
        workerFk: Worker
        from: From
        to: To
es:
    params:
        warehouseFk: Almacén
        description: Descripción
        m3: m³
        vehicleFk: Vehículo
        agencyModeFk: Agencia
        workerFk: Trabajador
        from: Desde
        to: Hasta
    Warehouse: Almacén
    Description: Descripción
    Vehicle: Vehículo
    Agency: Agencia
    Worker: Trabajador
    From: Desde
    To: Hasta
</i18n>
