import {
    resourceAddDialog,
    resourceDetailDialog,
    resourceEditeDialog
} from "../../components/resource/dialog-resource.js";
import {
    apiGet,
    arrayInCheck,
    arrayOutCheck,
    checkState,
    crudCommonOptions,
    mappingField,
    operationField,
    permissionStateMapping,
    timeField,
    userOnly
} from "../../components/constants.js";

export function resourceList() {
    return {
        type: 'crud',
        api: apiGet('${base}/data_resource/list'),
        ...crudCommonOptions(),
        headerToolbar: [
            'reload',
            {
                type: 'action',
                label: '',
                icon: 'fa fa-plus',
                ...resourceAddDialog()
            },
        ],
        columns: [
            {
                label: '名称',
                name: 'name',
                width: 150,
            },
            {
                label: '描述',
                name: 'description',
            },
            mappingField('confirmationState', '确权状态', permissionStateMapping),
            timeField('createdTime', '创建时间'),
            operationField('操作', undefined, [
                {
                    type: 'action',
                    label: '详情',
                    level: 'link',
                    ...resourceDetailDialog(),
                },
                {
                    type: 'dropdown-button',
                    level: 'link',
                    icon: 'fa fa-ellipsis-h',
                    hideCaret: true,
                    trigger: 'hover',
                    buttons: [
                        {
                            disabledOn: arrayOutCheck([checkState.none, checkState.draft], 'confirmationState'),
                            disabledTip: '审查或确权成功后无法编辑',
                            tooltipPlacement: 'top',
                            type: 'action',
                            label: '编辑',
                            level: 'link',
                            ...resourceEditeDialog(),
                        },
                        {
                            disabledOn: arrayInCheck([checkState.checking], 'confirmationState'),
                            disabledTip: '审查中无法删除',
                            tooltipPlacement: 'bottom',
                            type: 'action',
                            label: "删除",
                            confirmTitle: '确认删除',
                            confirmText: '确认删除名称为「${name}」的数据资源吗？',
                            actionType: 'ajax',
                            api: apiGet('${base}/data_resource/remove/${id}')
                        },
                    ]
                },
            ]),
        ]
    }
}

export function tabData() {
    return {
        visibleOn: userOnly,
        title: '数据资源',
        icon: 'fa fa-database',
        reload: true,
        body: resourceList(),
    }
}