<script setup>
import { ref, onBeforeMount } 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 { useValidator } from 'src/composables/useValidator';

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

const { t } = useI18n();
const validationsStore = useValidator();
const { models } = validationsStore;

const validations = ref([]);
const accessTypes = [{ name: '*' }, { name: 'READ' }, { name: 'WRITE' }];
const permissions = [{ name: 'ALLOW' }, { name: 'DENY' }];
const rolesOptions = ref([]);

onBeforeMount(() => {
    for (let model in models) validations.value.push({ name: model });
});
</script>

<template>
    <FetchData
        url="VnRoles"
        :filter="{ fields: ['name'], order: 'name ASC' }"
        @on-fetch="(data) => (rolesOptions = data)"
        auto-load
    />
    <VnFilterPanel
        :data-key="props.dataKey"
        :search-button="true"
        :hidden-tags="['search']"
    >
        <template #tags="{ tag, formatFn }">
            <div class="q-gutter-x-xs">
                <strong>{{ t(`acls.aclFilter.${tag.label}`) }}: </strong>
                <span>{{ formatFn(tag.value) }}</span>
            </div>
        </template>
        <template #body="{ params, searchFn }">
            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnSelect
                        :label="t('acls.aclFilter.principalId')"
                        v-model="params.principalId"
                        @update:model-value="searchFn()"
                        :options="rolesOptions"
                        option-value="name"
                        option-label="name"
                        use-input
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnSelect
                        :label="t('acls.aclFilter.model')"
                        v-model="params.model"
                        @update:model-value="searchFn()"
                        :options="validations"
                        option-value="name"
                        option-label="name"
                        use-input
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnInput
                        :label="t('acls.aclFilter.property')"
                        v-model="params.property"
                        lazy-rules
                        is-outlined
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnSelect
                        :label="t('acls.aclFilter.accessType')"
                        v-model="params.accessType"
                        @update:model-value="searchFn()"
                        :options="accessTypes"
                        option-value="name"
                        option-label="name"
                        use-input
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
            <QItem class="q-mb-sm">
                <QItemSection>
                    <VnSelect
                        :label="t('acls.aclFilter.permission')"
                        v-model="params.permission"
                        @update:model-value="searchFn()"
                        :options="permissions"
                        option-value="name"
                        option-label="name"
                        use-input
                        dense
                        outlined
                        rounded
                    />
                </QItemSection>
            </QItem>
        </template>
    </VnFilterPanel>
</template>
