import { FormSchema } from '/@/components/Table';
import { getBackupsDatabaseList } from '/@/api/sys/dataBackUp';
import moment from 'moment';

const getBackupsDatabaseSelect = () => {
    return new Promise((resolve) => {
        let params = {
            keyStr: ""
        }
        getBackupsDatabaseList(params).then(res => {
            if (res.resp_code === 0 && res.datas) {
                let resData: Array<{ label: string, value: string }> = []
                res.datas.pageData.forEach(element => {
                    resData.push(
                        {
                            label: element.dbname,
                            value: element.fid
                        }
                    )
                });
                resolve(resData)
            }
            else {
                resolve([])
            }
        })
    })
}
export const formSchema: FormSchema[] = [
    {
        field: 'fid',
        label: '备份id',
        component: 'Input',
        ifShow: false
    },
    {
        field: 'dbType',
        label: '数据库',
        component: 'RadioGroup',
        ifShow: true,
        required: true,
        defaultValue: '0',
        componentProps({ formActionType }) {
            const { updateSchema } = formActionType;
            return {
                options: [
                    {
                        label: '新增记录',
                        value: '0'
                    },
                    {
                        label: '已有记录',
                        value: '1'
                    }
                ],
                onChange(event: ChangeEvent) {
                    if ((event.target as HTMLInputElement).value === "0") {
                        updateSchema([
                            { field: 'dbip', ifShow: true },
                            { field: 'dbloginname', ifShow: true },
                            { field: 'pwd', ifShow: true },
                            { field: 'dbname', ifShow: true },
                            { field: 'dbid', ifShow: false }
                        ])
                    } else {
                        updateSchema([
                            { field: 'dbip', ifShow: false },
                            { field: 'dbloginname', ifShow: false },
                            { field: 'pwd', ifShow: false },
                            { field: 'dbname', ifShow: false },
                            { field: 'dbid', ifShow: true }
                        ])
                    }
                },
                disabled: false
            };
        },
    },
    {
        field: 'dbip',
        label: '数据库ip',
        component: 'Input',
        ifShow: true,
        required: true,
        componentProps: {
            maxLength: 36,
            placeholder: '请输入数据库ip',
            disabled: false
        },
    },
    {
        field: 'dbloginname',
        label: '数据库登录名',
        component: 'Input',
        ifShow: true,
        required: true,
        componentProps: {
            maxLength: 36,
            placeholder: '请输入数据库登录名',
            disabled: false
        },
    },
    {
        field: 'pwd',
        label: '数据库密码',
        component: 'Input',
        ifShow: true,
        required: true,
        componentProps: {
            maxLength: 36,
            placeholder: '请输入数据库密码',
            disabled: false
        },
    },
    {
        field: 'dbname',
        label: '数据库名称',
        component: 'Input',
        ifShow: true,
        required: true,
        componentProps: {
            maxLength: 36,
            placeholder: '请输入数据库名称',
            disabled: false
        },
    },
    {
        field: 'dbid',
        label: '数据库名称',
        component: 'ApiSelect',
        ifShow: false,
        required: true,
        componentProps: {
            api: getBackupsDatabaseSelect,
            placeholder: '请选择数据库',
            disabled: false
        },
    },
    {
        field: 'bfsm',
        label: '备份说明',
        component: 'Input',
        ifShow: true,
        required: true,
        componentProps: {
            maxLength: 255,
            placeholder: '请输入备份说明',
            disabled: false
        },
    },
];

const weeks = [
    {
        label: "星期一",
        value: 1
    },
    {
        label: "星期二",
        value: 2
    },
    {
        label: "星期三",
        value: 3
    },
    {
        label: "星期四",
        value: 4
    },
    {
        label: "星期五",
        value: 5
    },
    {
        label: "星期六",
        value: 6
    },
    {
        label: "星期日",
        value: 7
    }
];
const days = [];
for (let i = 1; i <= 31; i++) {
    days.push({
        label: i.toString() + '号',
        value: i
    });
}

export const scheduledTaskformSchema: FormSchema[] = [
    {
        field: 'fid',
        label: '定时任务id',
        component: 'Input',
        ifShow: false
    },
    {
        field: 'dbType',
        label: '数据库',
        component: 'RadioGroup',
        ifShow: true,
        required: true,
        defaultValue: '0',
        componentProps({ formActionType }) {
            const { updateSchema } = formActionType;
            return {
                options: [
                    {
                        label: '新增记录',
                        value: '0'
                    },
                    {
                        label: '已有记录',
                        value: '1'
                    }
                ],
                onChange(event: ChangeEvent) {
                    if ((event.target as HTMLInputElement).value === "0") {
                        updateSchema([
                            { field: 'dbip', ifShow: true },
                            { field: 'dbloginname', ifShow: true },
                            { field: 'pwd', ifShow: true },
                            { field: 'dbname', ifShow: true },
                            { field: 'bfFid', ifShow: false }
                        ])
                    } else {
                        updateSchema([
                            { field: 'dbip', ifShow: false },
                            { field: 'dbloginname', ifShow: false },
                            { field: 'pwd', ifShow: false },
                            { field: 'dbname', ifShow: false },
                            { field: 'bfFid', ifShow: true }
                        ])
                    }
                },
                disabled: false
            };
        },
    },
    {
        field: 'dbip',
        label: '数据库ip',
        component: 'Input',
        ifShow: true,
        required: true,
        componentProps: {
            maxLength: 36,
            placeholder: '请输入数据库ip',
            disabled: false
        },
    },
    {
        field: 'dbloginname',
        label: '数据库登录名',
        component: 'Input',
        ifShow: true,
        required: true,
        componentProps: {
            maxLength: 36,
            placeholder: '请输入数据库登录名',
            disabled: false
        },
    },
    {
        field: 'pwd',
        label: '数据库密码',
        component: 'Input',
        ifShow: true,
        required: true,
        componentProps: {
            maxLength: 36,
            placeholder: '请输入数据库密码',
            disabled: false
        },
    },
    {
        field: 'dbname',
        label: '数据库名称',
        component: 'Input',
        ifShow: true,
        required: true,
        componentProps: {
            maxLength: 36,
            placeholder: '请输入数据库名称',
            disabled: false
        },
    },
    {
        field: 'bfFid',
        label: '数据库名称',
        component: 'ApiSelect',
        ifShow: false,
        required: true,
        componentProps: {
            api: getBackupsDatabaseSelect,
            placeholder: '请选择数据库',
            disabled: false
        },
    },
    {
        field: 'bfzq',
        label: '备份周期',
        component: 'Select',
        ifShow: true,
        required: true,
        componentProps({ formActionType }) {
            const { updateSchema, setFieldsValue } = formActionType;
            return {
                options: [
                    {
                        label: '每周',
                        value: 1
                    },
                    {
                        label: '每月',
                        value: 2
                    }
                ],
                onChange(event) {
                    if (event === 1) {
                        updateSchema({ field: 'sjsz', componentProps: { options: weeks } })
                        setFieldsValue({ sjsz: '' })
                    } else {
                        updateSchema({ field: 'sjsz', componentProps: { options: days } })
                        setFieldsValue({ sjsz: '' })
                    }
                },
                disabled: false
            };
        },
    },
    {
        field: 'sjsz',
        label: '时间设置',
        component: 'Select',
        ifShow: true,
        required: true,
        componentProps: {
            options: [],
            placeholder: '请选择备份周期',
            disabled: false
        },
    },
    {
        field: 'zxsj',
        label: '执行时间',
        component: 'TimePicker',
        ifShow: true,
        required: true,
        defaultValue: moment('00:00', 'HH:mm'),
        componentProps: {
            format: "HH:mm",
            placeholder: '请选择执行时间',
            disabled: false
        },
    },
    {
        field: 'status',
        label: '是否启用',
        component: 'RadioGroup',
        ifShow: true,
        required: true,
        defaultValue: '1',
        componentProps: {
            options: [
                {
                    label: '禁用',
                    value: '0'
                },
                {
                    label: '启用',
                    value: '1'
                }
            ],
        },
    },
];