<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" 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 }">
            <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"
                    @update:model-value="searchFn()"
                    url="Workers/activeWithInheritedRole"
                    :filter="{ where: { role: 'salesPerson' } }"
                    :use-like="false"
                    option-value="id"
                    option-label="name"
                    option-filter="firstName"
                    dense
                    outlined
                    rounded
                />
                <VnSelect
                    :label="t('claim.attendedBy')"
                    v-model="params.attenderFk"
                    @update:model-value="searchFn()"
                    url="Workers/activeWithInheritedRole"
                    :filter="{ where: { role: 'salesPerson' } }"
                    :use-like="false"
                    option-value="id"
                    option-label="name"
                    option-filter="firstName"
                    dense
                    outlined
                    rounded
                />
                <VnSelect
                    :label="t('claim.state')"
                    v-model="params.claimStateFk"
                    @update:model-value="searchFn()"
                    :options="states"
                    option-value="id"
                    option-label="description"
                    dense
                    outlined
                    rounded
                />
                <VnInputDate
                    v-model="params.created"
                    @update:model-value="searchFn()"
                    :label="t('claim.created')"
                    outlined
                    rounded
                    dense
                />
                <VnSelect
                    :label="t('Item')"
                    v-model="params.itemFk"
                    @update:model-value="searchFn()"
                    url="Items/withName"
                    option-value="id"
                    option-label="name"
                    :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"
                    @update:model-value="searchFn()"
                    url="Workers/activeWithInheritedRole"
                    :filter="{ where: { role: 'salesPerson' } }"
                    :use-like="false"
                    option-value="id"
                    option-label="name"
                    option-filter="firstName"
                    dense
                    outlined
                    rounded
                />
                <QCheckbox
                    v-model="params.myTeam"
                    :label="t('params.myTeam')"
                    @update:model-value="searchFn()"
                    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
es:
    params:
        search: Contiene
        clientFk: Cliente
        clientName: Cliente
        salesPersonFk: Comercial
        attenderFk: Asistente
        claimResponsibleFk: Responsable
        claimStateFk: Estado
        created: Creada
        myTeam: Mi equipo
        itemFk: Artículo
    Client Name: Nombre del cliente
    Salesperson: Comercial
    Item: Artículo
</i18n>
