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

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

const departments = ref();

const getLocale = (label) => {
    const globalLocale = `globals.params.${label}`;
    return te(globalLocale) ? t(globalLocale) : t(`params.${label}`);
};
</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>{{ getLocale(tag.label) }}: </strong>
                <span>{{ formatFn(tag.value) }}</span>
            </div>
        </template>
        <template #body="{ params }">
            <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>
                    <VnSelect
                        :label="t('Department')"
                        v-model="params.departmentFk"
                        :options="departments"
                        option-value="id"
                        option-label="name"
                        emit-value
                        map-options
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput :label="t('Email')" v-model="params.email" is-outlined />
                </QItemSection>
            </QItem>
            <QItem>
                <QItemSection>
                    <VnInput
                        :label="t('Extension')"
                        v-model="params.extension"
                        is-outlined
                    />
                </QItemSection>
            </QItem>
        </template>
    </VnFilterPanel>
</template>

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