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

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

const workers = ref();
const states = ref();
</script>

<template>
    <FetchData url="ClaimStates" @on-fetch="(data) => (states = data)" auto-load />
    <FetchData
        url="Workers/activeWithInheritedRole"
        :filter="{ where: { role: 'salesPerson' } }"
        @on-fetch="(data) => (workers = 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 class="q-my-sm">
                <QItemSection>
                    <VnInput
                        :label="t('Customer ID')"
                        v-model="params.clientFk"
                        lazy-rules
                        is-outlined
                    >
                        <template #prepend>
                            <QIcon name="badge" size="xs"></QIcon> </template
                    ></VnInput>
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnInput
                        :label="t('Client Name')"
                        v-model="params.clientName"
                        lazy-rules
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection v-if="!workers">
                    <QSkeleton type="QInput" class="full-width" />
                </QItemSection>
                <QItemSection v-if="workers">
                    <VnSelectFilter
                        :label="t('Salesperson')"
                        v-model="params.salesPersonFk"
                        @update:model-value="searchFn()"
                        :options="workers"
                        option-value="id"
                        option-label="name"
                        emit-value
                        map-options
                        use-input
                        hide-selected
                        dense
                        outlined
                        rounded
                        :input-debounce="0"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection v-if="!workers">
                    <QSkeleton type="QInput" class="full-width" />
                </QItemSection>
                <QItemSection v-if="workers">
                    <VnSelectFilter
                        :label="t('Attender')"
                        v-model="params.attenderFk"
                        @update:model-value="searchFn()"
                        :options="workers"
                        option-value="id"
                        option-label="name"
                        emit-value
                        map-options
                        use-input
                        hide-selected
                        dense
                        outlined
                        rounded
                        :input-debounce="0"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection v-if="!workers">
                    <QSkeleton type="QInput" class="full-width" />
                </QItemSection>
                <QItemSection v-if="workers">
                    <VnSelectFilter
                        :label="t('Responsible')"
                        v-model="params.claimResponsibleFk"
                        @update:model-value="searchFn()"
                        :options="workers"
                        option-value="id"
                        option-label="name"
                        emit-value
                        map-options
                        use-input
                        hide-selected
                        dense
                        outlined
                        rounded
                        :input-debounce="0"
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection v-if="!states">
                    <QSkeleton type="QInput" class="full-width" />
                </QItemSection>
                <QItemSection v-if="states">
                    <VnSelectFilter
                        :label="t('State')"
                        v-model="params.claimStateFk"
                        @update:model-value="searchFn()"
                        :options="states"
                        option-value="id"
                        option-label="description"
                        emit-value
                        map-options
                        hide-selected
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QSeparator />
            <QExpansionItem :label="t('More options')" expand-separator>
                <!-- <QItem>
                        <QItemSection>
                            <qSelect
                                :label="t('Item')"
                                v-model="params.itemFk"
                                :options="items"
                                :loading="loading"
                                @filter="filterFn"
                                @virtual-scroll="onScroll"
                                option-value="id"
                                option-label="name"
                                emit-value
                                map-options
                            />
                        </QItemSection>
                    </QItem> -->
                <QItem>
                    <QItemSection>
                        <VnInputDate
                            v-model="params.created"
                            :label="t('Created')"
                            is-outlined
                        />
                    </QItemSection>
                </QItem>
            </QExpansionItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        search: Contains
        clientFk: Customer
        clientName: Customer
        salesPersonFk: Salesperson
        attenderFk: Attender
        claimResponsibleFk: Responsible
        claimStateFk: State
        created: Created
es:
    params:
        search: Contiene
        clientFk: Cliente
        clientName: Cliente
        salesPersonFk: Comercial
        attenderFk: Asistente
        claimResponsibleFk: Responsable
        claimStateFk: Estado
        created: Creada
    Customer ID: ID cliente
    Client Name: Nombre del cliente
    Salesperson: Comercial
    Attender: Asistente
    Responsible: Responsable
    State: Estado
    Item: Artículo
    Created: Creada
    More options: Más opciones
</i18n>
