<script setup>
import { useI18n } from 'vue-i18n';
import { computed, ref } from 'vue';
import VnTable from 'components/VnTable/VnTable.vue';
import { useRoute } from 'vue-router';
import VnSearchbar from 'components/ui/VnSearchbar.vue';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';
import RoleSummary from './Card/RoleSummary.vue';
const route = useRoute();
const { t } = useI18n();
const $props = defineProps({
    id: {
        type: Number,
        default: 0,
    },
});
const tableRef = ref();
const entityId = computed(() => $props.id || route.params.id);
const { viewSummary } = useSummaryDialog();
const columns = computed(() => [
    {
        align: 'left',
        name: 'id',
        label: t('Id'),
        isId: true,
        columnFilter: {
            inWhere: true,
            component: 'select',
            name: 'search',
            attrs: {
                url: 'VnRoles',
                fields: ['id', 'name'],
            },
        },
        cardVisible: true,
    },
    {
        align: 'left',
        name: 'name',
        label: t('Name'),
        cardVisible: true,
        create: true,
    },
    {
        align: 'left',
        name: 'description',
        label: t('Description'),
        cardVisible: true,
        create: true,
    },
    {
        align: 'right',
        label: '',
        name: 'tableActions',
        actions: [
            {
                title: t('components.smartCard.viewSummary'),
                icon: 'preview',
                action: (row) => viewSummary(row.id, RoleSummary),
                isPrimary: true,
            },
        ],
    },
]);
const exprBuilder = (param, value) => {
    switch (param) {
        case 'search':
            return /^\d+$/.test(value)
                ? { id: value }
                : {
                      or: [
                          { name: { like: `%${value}%` } },
                          { nickname: { like: `%${value}%` } },
                      ],
                  };
        case 'name':
        case 'description':
            return { [param]: { like: `%${value}%` } };
    }
};
</script>

<template>
    <VnSearchbar
        data-key="Roles"
        :expr-builder="exprBuilder"
        :label="t('role.searchRoles')"
        :info="t('role.searchInfo')"
    />
    <VnTable
        ref="tableRef"
        data-key="Roles"
        :url="`VnRoles`"
        :create="{
            urlCreate: 'VnRoles',
            title: t('Create rol'),
            onDataSaved: ({ id }) => tableRef.redirect(id),
            formInitialData: {
                editorFk: entityId,
            },
        }"
        order="id ASC"
        :disable-option="{ card: true }"
        :columns="columns"
        default-mode="table"
        redirect="account/role"
    />
</template>

<i18n>
    es:
        Id: Id
        Description: Descripción
        Name: Nombre
</i18n>
