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

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

const departments = ref();
</script>

<template>
    <FetchData url="Departments" @on-fetch="(data) => (departments = 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>
                <QItemSection>
                    <VnInput :label="t('FI')" v-model="params.fi" is-outlined
                        ><template #prepend>
                            <QIcon name="badge" size="xs"></QIcon> </template
                    ></VnInput>
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        :label="t('First Name')"
                        v-model="params.firstName"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        :label="t('Last Name')"
                        v-model="params.lastName"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        :label="t('User Name')"
                        v-model="params.userName"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection v-if="!departments">
                    <QSkeleton type="QInput" class="full-width" />
                </QItemSection>
                <QItemSection v-if="departments">
                    <QSelect
                        :label="t('Department')"
                        v-model="params.departmentFk"
                        @update:model-value="searchFn()"
                        :options="departments"
                        option-value="id"
                        option-label="name"
                        emit-value
                        map-options
                        use-input
                        dense
                        outlined
                        rounded
                        :input-debounce="0"
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        :label="t('Extension')"
                        v-model="params.extension"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
        </template>
    </VnFilterPanel>
</template>

<i18n>
en:
    params:
        search: Contains
        fi: FI
        firstName: First name
        lastName: Last name
        userName: User
        extension: Extension
        departmentFk: Department
es:
    params:
        search: Contiene
        fi: NIF
        firstName: Nombre
        lastName: Apellidos
        userName: Usuario
        extension: Extensión
        departmentFk: Departamento
    FI: NIF
    First Name: Nombre
    Last Name: Apellidos
    User Name: Usuario
    Department: Departamento
    Extension: Extensión
</i18n>
