<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';

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

const rolesOptions = ref([]);
</script>

<template>
    <FetchData
        url="VnRoles"
        :filter="{ fields: ['id', 'name'], order: 'name ASC' }"
        @on-fetch="(data) => (rolesOptions = data)"
        auto-load
    />
    <VnFilterPanel
        :data-key="props.dataKey"
        :search-button="true"
        :hidden-tags="['search']"
        :redirect="false"
    >
        <template #tags="{ tag, formatFn }">
            <div class="q-gutter-x-xs">
                <strong>{{ t(`account.card.${tag.label}`) }}: </strong>
                <span>{{ formatFn(tag.value) }}</span>
            </div>
        </template>
        <template #body="{ params, searchFn }">
            <QItem class="q-my-sm">
                <QItemSection>
                    <VnInput
                        :label="t('account.card.name')"
                        v-model="params.name"
                        lazy-rules
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-my-sm">
                <QItemSection>
                    <VnInput
                        :label="t('account.card.alias')"
                        v-model="params.nickname"
                        lazy-rules
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnSelect
                        :label="t('account.card.role')"
                        v-model="params.roleFk"
                        @update:model-value="searchFn()"
                        :options="rolesOptions"
                        option-value="id"
                        option-label="name"
                        emit-value
                        map-options
                        use-input
                        hide-selected
                        dense
                        outlined
                        rounded
                        :input-debounce="0"
                    />
                </QItemSection>
            </QItem>
        </template>
    </VnFilterPanel>
</template>
