<script setup>
import { useI18n } from 'vue-i18n';
import { ref, computed } from 'vue';
import VnTable from 'components/VnTable/VnTable.vue';
import VnSearchbar from 'components/ui/VnSearchbar.vue';
import AccountSummary from './Card/AccountSummary.vue';
import { useSummaryDialog } from 'src/composables/useSummaryDialog';
import AccountFilter from './AccountFilter.vue';
import RightMenu from 'src/components/common/RightMenu.vue';
const { t } = useI18n();
const { viewSummary } = useSummaryDialog();
const tableRef = ref();
const filter = {
    include: { relation: 'role', scope: { fields: ['id', 'name'] } },
};
const columns = computed(() => [
    {
        align: 'left',
        name: 'id',
        label: t('Id'),
        isId: true,
        field: 'id',
        cardVisible: true,
    },
    {
        align: 'left',
        name: 'roleFk',
        label: t('role'),
        columnFilter: {
            component: 'select',
            name: 'roleFk',
            attrs: {
                url: 'VnRoles',
                optionValue: 'id',
                optionLabel: 'name',
            },
        },
        format: ({ role }, dashIfEmpty) => dashIfEmpty(role?.name),
    },
    {
        align: 'left',
        name: 'nickname',
        label: t('Nickname'),
        isTitle: true,
        component: 'input',
        columnField: {
            component: null,
        },
        cardVisible: true,
        create: true,
    },
    {
        align: 'left',
        name: 'name',
        label: t('Name'),
        component: 'input',
        columnField: {
            component: null,
        },
        cardVisible: true,
        create: true,
    },
    {
        align: 'left',
        name: 'email',
        label: t('email'),
        component: 'input',
        create: true,
        visible: false,
    },
    {
        align: 'right',
        label: '',
        name: 'tableActions',
        actions: [
            {
                title: t('components.smartCard.viewSummary'),
                icon: 'preview',
                action: (row) => viewSummary(row.id, AccountSummary),
                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 'nickname':
            return { [param]: { like: `%${value}%` } };
        case 'roleFk':
            return { [param]: value };
    }
};
</script>

<template>
    <VnSearchbar
        data-key="AccountUsers"
        :expr-builder="exprBuilder"
        :label="t('account.search')"
        :info="t('account.searchInfo')"
        :filter="filter"
    />
    <RightMenu>
        <template #right-panel>
            <AccountFilter data-key="AccountUsers" />
        </template>
    </RightMenu>
    <VnTable
        ref="tableRef"
        data-key="AccountUsers"
        url="VnUsers/preview"
        :filter="filter"
        order="id DESC"
        :columns="columns"
        default-mode="table"
        redirect="account"
        :use-model="true"
        :right-search="false"
        auto-load
    />
</template>

<i18n>
    es:
        Id: Id
        Nickname: Nickname
        Name: Nombre
</i18n>
