<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 'components/common/VnSelect.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 states = ref([]);

defineExpose({ states });
</script>

<template>
    <FetchData url="ClaimStates" @on-fetch="(data) => (states = 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 }">
            <div class="q-pa-sm q-gutter-y-sm">
                <VnInput
                    :label="t('claim.customerId')"
                    v-model="params.clientFk"
                    lazy-rules
                    is-outlined
                >
                    <template #prepend> <QIcon name="badge" size="xs" /></template>
                </VnInput>
                <VnInput
                    :label="t('Client Name')"
                    v-model="params.clientName"
                    lazy-rules
                    is-outlined
                />
                <VnSelect
                    :label="t('Salesperson')"
                    v-model="params.salesPersonFk"
                    url="Workers/activeWithInheritedRole"
                    :filter="{ where: { role: 'salesPerson' } }"
                    :use-like="false"
                    option-filter="firstName"
                    dense
                    outlined
                    rounded
                />
                <VnSelect
                    :label="t('claim.attendedBy')"
                    v-model="params.attenderFk"
                    url="Workers/activeWithInheritedRole"
                    :filter="{ where: { role: 'salesPerson' } }"
                    :use-like="false"
                    option-filter="firstName"
                    dense
                    outlined
                    rounded
                />
                <VnSelect
                    :label="t('claim.state')"
                    v-model="params.claimStateFk"
                    :options="states"
                    option-label="description"
                    dense
                    outlined
                    rounded
                />
                <VnInputDate
                    v-model="params.created"
                    :label="t('claim.created')"
                    outlined
                    rounded
                    dense
                />
                <VnSelect
                    :label="t('Item')"
                    v-model="params.itemFk"
                    url="Items/withName"
                    :use-like="false"
                    sort-by="id DESC"
                    outlined
                    rounded
                    dense
                >
                    <template #option="scope">
                        <QItem v-bind="scope.itemProps">
                            <QItemSection>
                                <QItemLabel> #{{ scope.opt?.id }} </QItemLabel>
                                <QItemLabel caption>{{ scope.opt?.name }}</QItemLabel>
                            </QItemSection>
                        </QItem>
                    </template>
                </VnSelect>
                <VnSelect
                    :label="t('claim.responsible')"
                    v-model="params.claimResponsibleFk"
                    url="Workers/activeWithInheritedRole"
                    :filter="{ where: { role: 'salesPerson' } }"
                    :use-like="false"
                    option-filter="firstName"
                    dense
                    outlined
                    rounded
                />
                <VnSelect
                    :label="t('claim.zone')"
                    v-model="params.zoneFk"
                    url="Zones"
                    :use-like="false"
                    outlined
                    rounded
                    dense
                />
                <QCheckbox
                    v-model="params.myTeam"
                    :label="t('params.myTeam')"
                    toggle-indeterminate
                />
            </div>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        search: Contains
        clientFk: Customer
        clientName: Customer
        salesPersonFk: Salesperson
        attenderFk: Attender
        claimResponsibleFk: Responsible
        claimStateFk: State
        created: Created
        myTeam: My team
        itemFk: Item
        zoneFk: Zone
es:
    params:
        search: Contiene
        clientFk: Cliente
        clientName: Cliente
        salesPersonFk: Comercial
        attenderFk: Asistente
        claimResponsibleFk: Responsable
        claimStateFk: Estado
        created: Creada
        myTeam: Mi equipo
        itemFk: Artículo
        zoneFk: Zona
    Client Name: Nombre del cliente
    Salesperson: Comercial
    Item: Artículo
</i18n>
