import {
    apiGet,
    arrayInCheck,
    arrayOutCheck,
    checkState,
    crudCommonOptions,
    customerOnly,
    mappingField,
    operationField,
    permissionStateMapping,
    providerOnly,
    stringField,
    stringWrapField,
    timeField,
    userOnly,
} from "../../components/constants.js";
import {
    authenticationAddDialog,
    authenticationDetailDialog,
    authenticationEditeDialog,
    confirmationAddDialog,
    confirmationDetailDialog,
    confirmationEditeDialog,
} from "../../components/permission/dialog-permission.js";

export function tabPermissions() {
    return {
        visibleOn: userOnly,
        title: '权属管理',
        icon: 'fa fa-key',
        reload: true,
        body: {
            type: 'tabs',
            tabs: [
                {
                    visibleOn: providerOnly,
                    title: '确权管理',
                    body: {
                        type: 'crud',
                        api: {
                            ...apiGet('${base}/confirmation/list')
                        },
                        ...crudCommonOptions(),
                        headerToolbar: [
                            'reload',
                            {
                                type: 'action',
                                label: '',
                                icon: 'fa fa-plus',
                                ...confirmationAddDialog()
                            },
                        ],
                        columns: [
                            stringField('name', '名称', 200),
                            stringWrapField('description', '描述'),
                            mappingField('state', '状态', permissionStateMapping),
                            timeField('createdTime', '创建时间'),
                            operationField('操作', undefined, [
                                {
                                    type: 'action',
                                    label: '查看',
                                    level: 'link',
                                    ...confirmationDetailDialog(),
                                },
                                {
                                    visibleOn: "${state === 'CHECKING'}",
                                    type: 'action',
                                    label: '撤销',
                                    level: 'link',
                                    confirmTitle: '确认撤销',
                                    confirmText: '确认撤销名称为「${name}」的确权申请吗？',
                                    actionType: 'ajax',
                                    api: apiGet('${base}/confirmation/retract/${id}'),
                                },
                                {
                                    visibleOn: "${state === 'DRAFT' || state === 'REJECT'}",
                                    type: 'action',
                                    label: '提交',
                                    level: 'link',
                                    confirmTitle: '确认提交',
                                    confirmText: '确认提交名称为「${name}」的确权申请吗？',
                                    actionType: 'ajax',
                                    api: apiGet('${base}/confirmation/submit/${id}'),
                                },
                                {
                                    type: 'dropdown-button',
                                    level: 'link',
                                    icon: 'fa fa-ellipsis-h',
                                    hideCaret: true,
                                    trigger: 'hover',
                                    buttons: [
                                        {
                                            disabledOn: "${state !== 'DRAFT'}",
                                            type: 'action',
                                            label: '编辑',
                                            level: 'link',
                                            ...confirmationEditeDialog(),
                                        },
                                        {
                                            disabledOn: "${state === 'CHECKING'}",
                                            type: 'action',
                                            label: "删除",
                                            confirmTitle: '确认删除',
                                            confirmText: '确认删除名称为「${name}」的确权申请吗？删除后对应的数据资源处于未确权状态。',
                                            actionType: 'ajax',
                                            api: apiGet('${base}/confirmation/remove/${id}'),
                                        },
                                    ]
                                },
                            ]),
                        ]
                    }
                },
                {
                    visibleOn: customerOnly,
                    title: '授权管理',
                    body: {
                        type: 'crud',
                        api: {
                            ...apiGet('${base}/authentication/list')
                        },
                        ...crudCommonOptions(),
                        headerToolbar: [
                            'reload',
                            {
                                type: 'action',
                                label: '',
                                icon: 'fa fa-plus',
                                ...authenticationAddDialog()
                            },
                        ],
                        columns: [
                            stringField('name', '名称', 200),
                            stringWrapField('description', '描述'),
                            mappingField('state', '状态', permissionStateMapping),
                            timeField('createdTime', '创建时间'),
                            operationField('操作', undefined, [
                                {
                                    type: 'action',
                                    label: '查看',
                                    level: 'link',
                                    ...authenticationDetailDialog(),
                                },
                                {
                                    visibleOn: arrayInCheck([checkState.ownerChecking, checkState.checking], 'state'),
                                    type: 'action',
                                    label: '撤销',
                                    level: 'link',
                                    confirmTitle: '确认撤销',
                                    confirmText: '确认撤销名称为「${name}」的授权申请吗？',
                                    actionType: 'ajax',
                                    api: apiGet('${base}/authentication/retract/${id}'),
                                },
                                {
                                    visibleOn: arrayInCheck([checkState.draft], 'state'),
                                    type: 'action',
                                    label: '提交',
                                    level: 'link',
                                    confirmTitle: '确认提交',
                                    confirmText: '确认提交名称为「${name}」的授权申请吗？',
                                    actionType: 'ajax',
                                    api: apiGet('${base}/authentication/submit/${id}'),
                                },
                                {
                                    type: 'dropdown-button',
                                    level: 'link',
                                    icon: 'fa fa-ellipsis-h',
                                    hideCaret: true,
                                    trigger: 'hover',
                                    buttons: [
                                        {
                                            disabledOn: arrayOutCheck([checkState.draft], 'state'),
                                            type: 'action',
                                            label: '编辑',
                                            level: 'link',
                                            ...authenticationEditeDialog(),
                                        },
                                        {
                                            disabledOn: arrayInCheck([checkState.ownerChecking, checkState.checking], 'state'),
                                            type: 'action',
                                            label: "删除",
                                            confirmTitle: '确认删除',
                                            confirmText: '确认删除名称为「${name}」的确权申请吗？删除后对应的数据资源处于未确权状态。',
                                            actionType: 'ajax',
                                            api: apiGet('${base}/authentication/remove/${id}'),
                                        },
                                    ]
                                },
                            ]),
                        ]
                    }
                },
            ]
        }
    }
}