import './dialog-permission.css'
import {
    apiGet,
    apiPost,
    formCreatedUserAndModifiedUser,
    formInputClearable,
    formInputMultiFileStatic,
    horizontalFormOptions,
    inputFileFormItemCommonOptions,
    size100MB
} from "../constants.js";
import {resourceList} from "../../pages/index/tab-data.js";
import {resourceDetailDialog} from "../resource/dialog-resource.js";

function detailForm(showCreatedUserAndModifiedUser = false) {
    return {
        id: 'permission_form',
        type: 'form',
        ...horizontalFormOptions(),
        horizontal: {
            left: 2,
        },
        body: [
            {
                type: 'hidden',
                name: 'id',
            },
            {
                type: 'picker',
                name: 'targetId',
                label: '数据资源',
                required: true,
                multiple: false,
                size: 'md',
                valueField: 'id',
                labelField: 'name',
                source: apiGet('${base}/data_resource/list'),
                pickerSchema: {
                    ...resourceList(),
                },
                staticSchema: {
                    type: 'tpl',
                    tpl: "<span class='text-primary' style='cursor: pointer'>${targetName}</span>",
                    onEvent: {
                        click: {
                            actions: [
                                {
                                    actionType: 'dialog',
                                    ...resourceDetailDialog('targetId'),
                                }
                            ]
                        }
                    }
                }
            },
            {
                type: 'textarea',
                label: '确权说明',
                name: 'description',
                ...formInputClearable,
            },
            formInputMultiFileStatic('evidenceFiles', '相关材料'),
            {
                visibleOn: '${!detail}',
                type: 'input-file',
                label: '相关材料',
                name: 'evidenceFiles',
                multiple: true,
                required: true,
                joinValues: false,
                ...inputFileFormItemCommonOptions(undefined, size100MB),
            },
            ...(showCreatedUserAndModifiedUser ? formCreatedUserAndModifiedUser() : [])
        ]
    }
}

export function permissionAddDialog() {
    return {
        actionType: 'dialog',
        dialog: {
            title: '新增确权申请',
            size: 'md',
            actions: [
                {
                    type: 'reset',
                    label: '重置',
                },
                {
                    type: 'submit',
                    label: '确定',
                    level: 'primary',
                }
            ],
            body: {
                ...detailForm(),
                api: apiPost('${base}/confirmation/save'),
                data: {
                    add: true,
                },
            }
        }
    }
}

export function permissionDetailDialog(field = 'id', actions = []) {
    return {
        actionType: 'dialog',
        dialog: {
            title: '确权申请详情',
            size: 'md',
            actions: actions,
            body: {
                ...detailForm(true),
                initApi: apiGet(`\${base}/confirmation/detail/\${${field}}`),
                static: true,
                data: {
                    detail: true,
                },
            }
        }
    }
}

export function permissionEditeDialog(field = 'id') {
    return {
        actionType: 'dialog',
        dialog: {
            title: '确权申请详情',
            size: 'md',
            actions: [
                {
                    type: 'reset',
                    label: '重置',
                },
                {
                    type: 'submit',
                    label: '确定',
                    level: 'primary',
                }
            ],
            body: {
                ...detailForm(),
                api: apiPost('${base}/confirmation/save'),
                initApi: apiGet(`\${base}/confirmation/detail/\${${field}}`),
                data: {
                    edit: true,
                }
            },
        }
    }
}
