import {
    apiGet,
    copyField,
    crudCommonOptions,
    mappingField,
    userRoleMapping,
    userStateMapping
} from '../../components/constants.js'
import {userCheckDialog, userDetailDialog} from "../../components/user/dialog-user-check.js";
import {userAdministratorRegisterDialog} from "../../components/user/dialog-user-register.js";

export function tabUser() {
    return {
        visibleOn: '${role === "ADMINISTRATOR"}',
        title: '用户管理',
        icon: 'fa fa-user',
        body: [
            {
                type: 'crud',
                api: apiGet('${base}/user_management/list'),
                ...crudCommonOptions(),
                headerToolbar: [
                    'reload',
                    {
                        type: 'action',
                        icon: 'fa fa-plus',
                        tooltip: '新增账号',
                        ...userAdministratorRegisterDialog(),
                    },
                ],
                columns: [
                    {
                        name: 'username',
                        label: '邮箱',
                        ...copyField(
                            'username',
                            undefined,
                            undefined,
                            userDetailDialog(),
                        )
                    },
                    {
                        label: '角色',
                        width: 120,
                        align: 'center',
                        ...mappingField('role', userRoleMapping)
                    },
                    {
                        label: '账号状态',
                        width: 80,
                        align: 'center',
                        ...mappingField('state', userStateMapping)
                    },
                    {
                        label: '创建时间',
                        width: 150,
                        align: 'center',
                        type: 'tpl',
                        tpl: "${IF(createdTime, DATETOSTR(createdTime), '-')}"
                    },
                    {
                        label: '上次登陆时间',
                        width: 150,
                        align: 'center',
                        type: 'tpl',
                        tpl: "${IF(lastLoginTime, DATETOSTR(lastLoginTime), '-')}"
                    },
                    {
                        label: '操作',
                        width: 100,
                        type: 'operation',
                        fixed: 'right',
                        className: 'nowrap',
                        buttons: [
                            {
                                visibleOn: "${state === 'CHECKING'}",
                                label: '审核',
                                icon: 'fa fa-fingerprint',
                                level: 'primary',
                                size: 'xs',
                                ...userCheckDialog(),
                            },
                            {
                                visibleOn: "${state === 'NORMAL' && role !== 'ADMINISTRATOR'}",
                                label: '禁用',
                                icon: 'fa fa-ban',
                                level: 'danger',
                                size: 'xs',
                                confirmText: '确认禁用账号${username}？',
                                confirmTitle: '禁用账号',
                                actionType: 'ajax',
                                api: apiGet('${base}/user_management/disable/${username}'),
                            },
                            {
                                visibleOn: "${state === 'DISABLED' && role !== 'ADMINISTRATOR'}",
                                label: '启用',
                                icon: 'fa fa-check',
                                level: 'success',
                                size: 'xs',
                                confirmText: '确认启用账号${username}？',
                                confirmTitle: '启用账号',
                                actionType: 'ajax',
                                api: apiGet('${base}/user_management/enable/${username}'),
                            }
                        ]
                    },
                ]
            }
        ]
    }
}