<script setup>
import { useI18n } from 'vue-i18n';
import VnFilterPanel from 'components/ui/VnFilterPanel.vue';
import VnSelect from 'components/common/VnSelect.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']);
</script>

<template>
    <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>
                    <VnSelect
                        :label="t('Worker')"
                        v-model="params.workerFk"
                        url="Workers/search"
                        sort-by="nickname ASC"
                        option-value="id"
                        option-label="nickname"
                        dense
                        outlined
                        rounded
                        :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>
                    </VnSelect>
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection>
                    <VnSelect
                        :label="t('Agency')"
                        v-model="params.agencyModeFk"
                        url="AgencyModes/isActive"
                        sort-by="name ASC"
                        option-value="id"
                        option-label="name"
                        dense
                        outlined
                        rounded
                        :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>
                    <VnSelect
                        :label="t('Vehicle')"
                        v-model="params.vehicleFk"
                        url="Vehicles"
                        sort-by="numberPlate ASC"
                        option-value="id"
                        option-label="numberPlate"
                        option-filter-value="numberPlate"
                        dense
                        outlined
                        rounded
                        :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>
                    <VnSelect
                        :label="t('Warehouse')"
                        v-model="params.warehouseFk"
                        url="Warehouses"
                        option-value="id"
                        option-label="name"
                        dense
                        outlined
                        rounded
                        :input-debounce="0"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection>
                    <VnInput
                        v-model="params.description"
                        :label="t('Description')"
                        is-outlined
                        clearable
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <QCheckbox
                        v-model="params.isOk"
                        :label="t('Served')"
                        toggle-indeterminate
                    />
                </QItemSection>
            </QItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        warehouseFk: Warehouse
        description: Description
        m3: m³
        vehicleFk: Vehicle
        agencyModeFk: Agency
        workerFk: Worker
        from: From
        to: To
        Served: Served
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
    Served: Servida
    Days Onward: Días en adelante
</i18n>
