<template>
    <BasicModal :maskClosable="false" :width="440" @register="registerModal" v-bind="$attrs" :title="getTitle" centered
        @ok="handleSubmit">
        <BasicForm @register="registerForm" />
    </BasicModal>
</template>
<script>
import { defineComponent, reactive, ref, onMounted, watch, computed } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { message } from 'ant-design-vue';
import { scheduledTaskformSchema } from './dataBackup.data';
import { editBackupsDatabase, EditScheduledTaskCL } from '/@/api/sys/dataBackUp';

export default defineComponent({
    name: 'scheduledTaskManageModal',
    components: { BasicModal, BasicForm },
    setup(_, { emit }) {
        const modalStatus = ref('add')
        const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
            console.log(data)
            resetForm();
            if (data.status === 'add') {
                updateSchema({ field: 'dbType', ifShow: true })
            } else if (data.status === 'edit') {
                updateSchema([
                    { field: 'dbType', ifShow: false }
                ])
                // const { FID, VERSION_NAME, VERSION_NO, VERSION_DISP } = data.form;
                // setFieldsValue({
                //     fid: FID,
                //     version_NAME: VERSION_NAME,
                //     version_NO: VERSION_NO,
                //     version_DISP: VERSION_DISP
                // })
            }
            modalStatus.value = data.status;
        });
        const [registerForm, { setFieldsValue, updateSchema, resetFields, validate, getFieldsValue }] = useForm({
            schemas: scheduledTaskformSchema,
            labelCol: { span: 6 },
            wrapperCol: { span: 16 },
            showActionButtonGroup: false
        });
        const getTitle = computed(() => {
            if (modalStatus.value === 'add') {
                return '新增任务'
            } else if (modalStatus.value === 'edit') {
                return '编辑任务'
            }
        })
        const resetForm = () => {
            resetFields();
            updateSchema([
                { field: 'dbip', ifShow: true },
                { field: 'dbloginname', ifShow: true },
                { field: 'pwd', ifShow: true },
                { field: 'dbname', ifShow: true },
                { field: 'bfFid', ifShow: false },
                { field: 'sjsz', componentProps: { options: [] } }
            ])
        }
        // 提交信息
        const handleSubmit = () => {
            validate().then(async (res) => {
                if (res) {
                    // 新增数据库，需要先保存数据库信息
                    // if (res.dbType === '0') {
                    //     let { dbip, dbloginname, pwd, dbname, bfzq, sjsz, zxsj, status } = getFieldsValue();
                    //     let dbInfo = await editBackupsDatabase({ dbip, dbloginname, pwd, dbname });
                    //     if (dbInfo.resp_code === 0 && dbInfo.datas) {
                    //         let bfFid = dbInfo.datas.dbid;
                    //         await EditScheduledTaskCL({ bfFid, bfsm });
                    //     } else {
                    //         message.error('保存数据库信息失败');
                    //     }
                    // }
                    // // 已有数据库，直接用dbid备份
                    // else {
                    //     let { bfFid, bfzq, sjsz, zxsj, status } = getFieldsValue();
                    //     await EditScheduledTaskCL({ bfFid, bfzq, sjsz, zxsj, status });
                    // }
                    console.log('res', res);
                    // 关闭弹窗
                    closeModal();
                    // 触发父组件事件
                    emit('success', res);
                }
            });
        };
        return {
            getTitle,
            registerModal,
            registerForm,
            handleSubmit,
        };
    },
});
</script>