<script setup>
import { useI18n } from 'vue-i18n';
import { ref, computed } from 'vue';
import VnTable from 'components/VnTable/VnTable.vue';
import VnSection from 'src/components/common/VnSection.vue';

const tableRef = ref();
const { t } = useI18n();
const dataKey = 'AccountAliasList';

const columns = computed(() => [
    {
        align: 'left',
        name: 'id',
        label: t('Id'),
        isId: true,
        cardVisible: true,
    },
    {
        align: 'left',
        name: 'alias',
        label: t('Alias'),
        cardVisible: true,
        create: true,
    },
    {
        align: 'left',
        name: 'description',
        label: t('Description'),
        cardVisible: true,
        create: true,
    },
]);

const exprBuilder = (param, value) => {
    switch (param) {
        case 'search':
            return /^\d+$/.test(value)
                ? { id: value }
                : { alias: { like: `%${value}%` } };
    }
};
</script>

<template>
    <VnSection
        :data-key="dataKey"
        :columns="columns"
        prefix="mailAlias"
        :array-data-props="{ url: 'MailAliases', order: 'id DESC', exprBuilder }"
    >
        <template #body>
            <VnTable
                :data-key="dataKey"
                ref="tableRef"
                :create="{
                    urlCreate: 'MailAliases',
                    title: 'Create MailAlias',
                    onDataSaved: ({ id }) => tableRef.redirect(id),
                    formInitialData: {},
                }"
                :columns="columns"
                :disable-option="{ card: true }"
                default-mode="table"
                redirect="account/alias"
                :is-editable="true"
                :use-model="true"
                :right-search="false"
            />
        </template>
    </VnSection>
</template>
<i18n>
    es:
        Id: Id
        Alias: Alias
        Description: Descripción
</i18n>
