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

const CONFIRMATION_TYPE = 'confirmation'
const AUTHENTICATION_TYPE = 'authentication'

const CONFIRMATION = {
    type: 'confirmation',
    pickerApi: apiGet('${base}/data_resource/list_no_confirmation')
}

const AUTHENTICATION = {
    type: 'authentication',
    pickerApi: apiGet('${base}/data_resource/list_no_authentication')
}

function detailForm(pickerApi = apiGet('${base}/data_resource/list'), showCreatedUserAndModifiedUser = false) {
    return {
        debug: information.debug,
        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: pickerApi,
                pickerSchema: {
                    ...resourceList(),
                },
                staticSchema: {
                    type: 'tpl',
                    tpl: "<span class='text-primary' style='cursor: pointer'>${targetName}</span>",
                    onEvent: {
                        click: {
                            actions: [
                                {
                                    actionType: 'dialog',
                                    ...resourceDetailDialog('targetId'),
                                }
                            ]
                        }
                    }
                }
            },
            {
                type: 'textarea',
                placeholder: '请输入确权说明',
                label: '确权说明',
                name: 'description',
                ...formInputClearable,
            },
            formInputMultiFileStatic('evidenceFiles', '相关材料'),
            {
                visibleOn: '${!detail}',
                type: 'input-file',
                label: '相关材料',
                name: 'evidenceFiles',
                multiple: true,
                required: true,
                joinValues: false,
                ...inputFileFormItemCommonOptions(undefined, size100MB),
            },
            {
                visibleOn: `\${${AUTHENTICATION_TYPE}}`,
                type: 'input-datetime-range',
                name: 'activeTime',
                extraName: 'expiredTime',
                label: '授权时间',
                required: true,
                format: 'YYYY-MM-DD HH:mm:ss',
                shortcuts: [
                    '7dayslater',
                    '14dayslater',
                    '30dayslater',
                    '180dayslater',
                    '365dayslater',
                ]
            },
            ...(showCreatedUserAndModifiedUser ? formCreatedUserAndModifiedUser() : [])
        ]
    }
}

export function confirmationAddDialog() {
    return permissionAddDialog(CONFIRMATION)
}

export function authenticationAddDialog() {
    return permissionAddDialog(AUTHENTICATION)
}

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

export function confirmationDetailDialog(field = 'id', actions = []) {
    return permissionDetailDialog(CONFIRMATION, field, actions)
}

export function authenticationDetailDialog(field = 'id', actions = []) {
    return permissionDetailDialog(AUTHENTICATION, field, actions)
}

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

export function confirmationEditeDialog(field = 'id') {
    return permissionEditeDialog(CONFIRMATION, field)
}

export function authenticationEditeDialog(field = 'id') {
    return permissionEditeDialog(AUTHENTICATION, field)
}

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