<template>
    <BasicModal :maskClosable="false" :width="440" @register="registerModal" v-bind="$attrs" title="数据备份" 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 { formSchema } from './dataBackup.data';
import { editBackupsDatabase, addDataBackup } from '/@/api/sys/dataBackUp';

export default defineComponent({
    name: 'addDataBackup',
    components: { BasicModal, BasicForm },
    setup(_, { emit }) {
        const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
            console.log(data)
            resetForm();
        });
        const [registerForm, { setFieldsValue, updateSchema, resetFields, validate, getFieldsValue }] = useForm({
            schemas: formSchema,
            labelCol: { span: 6 },
            wrapperCol: { span: 16 },
            showActionButtonGroup: false
        });
        const resetForm = () => {
            resetFields();
            updateSchema([
                { field: 'addType', ifShow: true },
                { field: 'dbip', ifShow: true },
                { field: 'dbloginname', ifShow: true },
                { field: 'pwd', ifShow: true },
                { field: 'dbname', ifShow: true },
                { field: 'dbid', ifShow: false }
            ])
        }
        // 提交信息
        const handleSubmit = () => {
            validate().then(async (res) => {
                if (res) {
                    // 新增数据库，需要先保存数据库信息
                    // if (res.dbType === '0') {
                    //     let { dbip, dbloginname, pwd, dbname, bfsm } = getFieldsValue();
                    //     let dbInfo = await editBackupsDatabase({ dbip, dbloginname, pwd, dbname });
                    //     if (dbInfo.resp_code === 0 && dbInfo.datas) {
                    //         let dbid = dbInfo.datas.dbid;
                    //         await addDataBackup({ dbBackups:{dbid, bfsm}});
                    //     } else {
                    //         message.error('保存数据库信息失败');
                    //     }
                    // } 
                    // // 已有数据库，直接用dbid备份
                    // else {
                    //     let { dbid, bfsm } = getFieldsValue();
                    //     await addDataBackup({ dbBackups:{dbid, bfsm}});
                    // }
                    console.log('res', res);
                    // 关闭弹窗
                    closeModal();
                    // 触发父组件事件
                    emit('success', res);
                }
            });
        };
        return {
            registerModal,
            registerForm,
            handleSubmit,
        };
    },
});
</script>