"use strict";
import { fullMap } from '../../../fullMap.js';
import { khcAlert } from '../../../component/alert/khcAlert.js'
import { form } from '../../../component/page/form/form.js';
import { util } from '../../../util/util.js'
import { khcModal } from '../../../component/alert/khcModal.js'
import {pageInfoMap} from "../../../pageInfoMap.js";
import {config} from "../../../config.js";
import {httpUtil} from "../../../util/httpUtil.js";
import {khcModalSlide} from "../../../component/alert/khcModalSlide.js";
import {khcToastr} from "../../../component/alert/khcToastr.js";
let functionMap=new Map();// 使用 Map 来存储多个函数。每个函数都通过 set 方法添加到 Map 中，键是函数名，值是对应的函数。
export function main(jsonConfig) {
    try {
        functionMap.get(jsonConfig.button_method).apply(jsonConfig);
    } catch (error) {
        khcAlert.showSystemError(error.message);
        console.log(error);
    }
}
let khcModalForForm=null;
function initFormField(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let params =queryViewComponent.getCode();
    params.page_parent_uuid=pageInfoMap.getObj('query_form_info').page_parent_uuid;
    params.methodName='initFormField';
    let obj={url:config.getConfigJson().myActionUrl,params:params}
    return httpUtil.post(obj, (response)=>{
        Swal.fire(
            '成功',
            '同步成功',
            'success'
        )
        queryViewComponent.getTableObj().refresh();
    })
}
functionMap.set('initFormField', initFormField);// 将函数添加到 Map 中

function previewFormInfo(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let tableViewComponent = queryViewComponent.getTableObj();
    if((tableViewComponent.tUtil!=null&&tableViewComponent.isEditTableData())){
        khcAlert.showWarningMessage('检测到当前表格数据有改动，请先保存当前页数据')
        return false;//返回false
    }
    let tableDataMap = tableViewComponent.getContentMap();
    let pageComponent=fullMap.getObj(queryViewComponent.pageID);
    //先查询出dsCode和formCode
    let queryParams = util.clone(tableViewComponent.code);
    queryParams.methodName='queryDsCodeAndFormCode';
    queryParams.page_parent_uuid=pageInfoMap.getObj(pageComponent.formCode.code.queryCode).page_parent_uuid;
    let obj={url:config.getConfigJson().myActionUrl,params:queryParams}
    httpUtil.post(obj, (response)=>{
        let params = {};
        params.dsCode=response.data.data.ds_code;
        params.formCode=response.data.data.form_code;
        params.timestamp=new Date().getTime();//时间戳是为了不走游览器缓存的表单数据，每次渲染走数据库查询最新
        khcModalForForm = new khcModalSlide({ modalAreaID: config.getConfigJson().modalAreaID });
        khcModalForForm.render().then(() => {
            let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params ,pageID:queryViewComponent.pageID});
            formComponent.khcModalForForm=khcModalForForm;
            khcModalForForm.formComponent=formComponent;
            khcModalForForm.formObjID=formComponent.getObjID()
            let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
            pageInfoMap.push(khcModalForFormKey,khcModalForForm);
            formComponent.setFormShowType('modals')
            //同理可以实现saveBefore
            formComponent.setSaveBefore( ()=> {
                khcModalForForm.hide();
                khcToastr.showWarningMessage('预览表单，不存储数据')
                return false;//返回false，后面保存代码块无需执行
            })
            formComponent.render().then(() => {
                formComponent.renderThenFunc=()=>{
                    khcToastr.showWarningMessage('预览表单，请不要填写业务数据')
                    formComponent.formCreate();
                    khcModalForForm.show();
                }
                formComponent.renderThenFunc();
            });
        })
    })
}
functionMap.set('previewFormInfo', previewFormInfo);// 将函数添加到 Map 中