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

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,
        columnFilter: {
            component: 'select',
            name: 'search',
            attrs: {
                url: 'VnUsers/preview',
                fields: ['id', 'name'],
            },
        },
    },
    {
        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,
        },
        columnFilter: {
            inWhere: true,
        },
        cardVisible: true,
        create: true,
    },
    {
        align: 'left',
        name: 'name',
        label: t('Name'),
        component: 'input',
        columnField: {
            component: null,
        },
        columnFilter: {
            inWhere: true,
        },
        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('View Summary'),
                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"
    />
    <VnTable
        ref="tableRef"
        data-key="AccountUsers"
        url="VnUsers/preview"
        :filter="filter"
        order="id DESC"
        :columns="columns"
        default-mode="table"
        redirect="account"
        :use-model="true"
    />
</template>

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