import './dialog-resource.css'
import {apiPost, horizontalFormOptions, inputFileFormItemCommonOptions} from "../constants.js";

const clearable = {
    clearable: true,
    clearValueOnEmpty: true,
}

export function resourceAddDialog() {
    return {
        actionType: 'dialog',
        dialog: {
            title: '新增数据资源',
            // size: 'lg',
            actions: [
                {
                    type: 'reset',
                    label: '重置',
                },
                {
                    type: 'submit',
                    label: '确定',
                    level: 'primary',
                }
            ],
            body: {
                debug: true,
                type: 'form',
                api: apiPost('${base}/data_resource/create'),
                ...horizontalFormOptions(),
                horizontal: {
                    left: 2,
                },
                body: [
                    {
                        type: 'input-text',
                        name: 'name',
                        label: '资源名称',
                        required: true,
                        ...clearable,
                    },
                    {
                        type: 'textarea',
                        name: 'description',
                        label: '资源描述',
                        ...clearable,
                        showCounter: true,
                        trimContents: true,
                        minRows: 2,
                        maxRows: 2,
                    },
                    {
                        type: 'fieldSet',
                        title: '资源类型定义',
                        body: [
                            {
                                name: 'resourceType',
                                type: 'select',
                                label: '资源类型',
                                selectFirst: true,
                                required: true,
                                options: [
                                    {label: 'API', value: 'api'},
                                    {label: '文件', value: 'file'},
                                    {label: '数据库', value: 'database'},
                                    {label: 'HDFS', value: 'hdfs'},
                                    {label: 'FTP', value: 'ftp'},
                                ]
                            },
                            {
                                visibleOn: "${resourceType === 'api'}",
                                type: 'fieldSet',
                                body: [
                                    {
                                        type: 'input-text',
                                        label: 'API地址',
                                        name: 'apiUrl',
                                        required: true,
                                        ...clearable,
                                    },
                                    {
                                        type: 'input-text',
                                        label: '用户名',
                                        name: 'apiUsername',
                                        ...clearable,
                                    },
                                    {
                                        type: 'input-password',
                                        label: '密码',
                                        name: 'apiPassword',
                                        ...clearable,
                                    },
                                ]
                            },
                            {
                                visibleOn: "${resourceType === 'file'}",
                                type: 'fieldSet',
                                body: [
                                    {
                                        type: 'input-file',
                                        name: 'filePath',
                                        description: '只适合小于2M的资源文件使用，大文件请使用其他资源类型',
                                        ...inputFileFormItemCommonOptions('.zip', 2097152),
                                    },
                                ]
                            },
                            {
                                visibleOn: "${resourceType === 'database'}",
                                type: 'fieldSet',
                                body: [
                                    {
                                        type: 'select',
                                        label: '数据库类型',
                                        name: 'databaseType',
                                        required: true,
                                        options: [
                                            {label: 'MySQL', value: 'MYSQL'},
                                            {label: 'Oracle', value: 'ORACLE'},
                                            {label: 'PostgreSQL', value: 'POSTGRESQL'},
                                        ]
                                    },
                                    {
                                        type: 'input-text',
                                        label: 'JDBC',
                                        name: 'databaseJdbc',
                                        required: true,
                                        ...clearable,
                                    },
                                    {
                                        type: 'input-text',
                                        label: '用户名',
                                        name: 'databaseUsername',
                                        ...clearable,
                                    },
                                    {
                                        type: 'input-password',
                                        label: '密码',
                                        name: 'databasePassword',
                                        ...clearable,
                                    },
                                ]
                            },
                            {
                                visibleOn: "${resourceType === 'hdfs'}",
                                type: 'fieldSet',
                                body: [
                                    {
                                        type: 'input-file',
                                        description: 'core-site.xml',
                                        name: 'coreSiteFile',
                                        ...inputFileFormItemCommonOptions('.xml', 1048576),
                                    },
                                    {
                                        type: 'input-file',
                                        description: 'hdfs-site.xml',
                                        name: 'hdfsSiteFile',
                                        ...inputFileFormItemCommonOptions('.xml', 1048576),
                                    },
                                ]
                            },
                            {
                                visibleOn: "${resourceType === 'ftp'}",
                                type: 'fieldSet',
                                body: [
                                    {
                                        type: 'input-text',
                                        label: 'FTP地址',
                                        name: 'ftpUrl',
                                        required: true,
                                        ...clearable,
                                    },
                                    {
                                        type: 'input-text',
                                        label: 'FTP账号',
                                        name: 'ftpUsername',
                                        ...clearable,
                                    },
                                    {
                                        type: 'input-password',
                                        label: 'FTP密码',
                                        name: 'ftpPassword',
                                        ...clearable,
                                    },
                                    {
                                        type: 'input-text',
                                        label: '相对路径',
                                        name: 'ftpPath',
                                        description: '若为空，则使用用户根目录',
                                        ...clearable,
                                    },
                                    {
                                        type: 'input-text',
                                        label: '文件筛选',
                                        name: 'ftpRegexFilter',
                                        description: '正则表达式，用于匹配文件的路径，只有符合筛选条件的文件才会被采集；若为空则默认采集全部文件',
                                        ...clearable,
                                    },
                                ]
                            },
                        ]
                    },
                    {
                        type: 'fieldSet',
                        title: '资源格式定义',
                        className: 'mt-5',
                        body: [
                            {
                                name: 'formatType',
                                type: 'select',
                                label: '资源格式',
                                selectFirst: true,
                                required: true,
                                options: [
                                    {label: '无', value: 'NONE'},
                                    {label: 'Line', value: 'LINE'},
                                    {label: 'JSON', value: 'JSON'},
                                    {label: 'JSON Line', value: 'JSON_LINE'},
                                    {label: 'CSV', value: 'CSV'},
                                ]
                            },
                            {
                                visibleOn: "${formatType === 'JSON'}",
                                type: 'json-schema-editor',
                                name: 'jsonSchema',
                                label: 'JSON格式',
                                required: true,
                                enableAdvancedSetting: true,
                                mini: true,
                            },
                            {
                                visibleOn: "${formatType === 'JSON_LINE'}",
                                type: 'json-schema-editor',
                                name: 'jsonLineSchema',
                                label: 'JSON格式',
                                description: 'JSON Line类型请定义单行JSON数据格式',
                                required: true,
                                enableAdvancedSetting: true,
                                mini: true,
                            },
                            {
                                visibleOn: "${formatType === 'CSV'}",
                                type: 'json-schema-editor',
                                name: 'csvSchema',
                                label: 'CSV格式',
                                description: '请定义单行数据中各个字段的格式',
                                required: true,
                                enableAdvancedSetting: true,
                                mini: true,
                                disabledTypes: [
                                    'object',
                                    'array',
                                    'null',
                                ]
                            },
                            {
                                type: 'input-file',
                                label: '资源示例',
                                description: '可以上传用于作为格式示范的样例数据',
                                name: 'example',
                                ...inputFileFormItemCommonOptions(),
                            },
                        ]
                    }
                ]
            }
        }
    }
}