"use strict";
import { khcAlert } from '../../../component/alert/khcAlert.js'
import { fullMap } from '../../../fullMap.js';
import {config} from "../../../config.js";
import { util } from "../../../util/util.js";
import {httpUtil} from "../../../util/httpUtil.js";
import {khcToastr} from "../../../component/alert/khcToastr.js";
import {khcModalSlide} from "../../../component/alert/khcModalSlide.js";
import {form} from "../../../component/page/form/form.js";
import {pageInfoMap} from "../../../pageInfoMap.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);
    }
}

//清空
function formClearCUD(){
    let jsonConfig=this;
    let component = fullMap.getObj(jsonConfig.objID);
    let eleMap = component.getFormMap();
    eleMap.get('ds_code')
    eleMap.get('query_code').setValue('');
    eleMap.get('form_code').setValue('');
}
functionMap.set('formClearCUD', formClearCUD);// 将函数添加到 Map 中

//预览
function toPreviewCUD() {
    let jsonConfig=this;
    let component = fullMap.getObj(jsonConfig.objID);
    let formValue = component.getFormValue();
    let dsCode=formValue['ds_code'];
    let queryCode=formValue['query_code'];
    let formCode=formValue['form_code']
    let params={dsCode:dsCode,formCode:formCode};
    component.setSaveBefore(()=>{
        component.khcModalForForm.hide();
        showDetail(params,formValue)
        return false;
    })
    component.saveData();
}
functionMap.set('toPreviewCUD', toPreviewCUD);// 将函数添加到 Map 中

function showDetail(params,formValue,renderData) {
    params=util.clone(params);
    params.order=1;//加一个参数，会重新请求后端获取参数
    let content = {};
    if(formValue['cud_preview_type']=='1'){
        content = JSON.parse(formValue['old_data'])
    }else{
        content = JSON.parse(formValue['new_data'])
    }
    console.log(content)
    let khcModalForForm = new khcModalSlide({ modalAreaID: config.getConfigJson().modalAreaID });
    khcModalForForm.render().then(() => {
        let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params });
        khcModalForForm.formComponent=formComponent;
        formComponent.khcModalForForm=khcModalForForm;
        khcModalForForm.formObjID=formComponent.getObjID()
        let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
        pageInfoMap.push(khcModalForFormKey,khcModalForForm);
        formComponent.setFormShowType('modals')
        formComponent.closeButton();
        formComponent.render().then(() => {
            formComponent.renderThenFunc=(content)=> {
                let eleMap = formComponent.getFormMap();
                if(pageInfoMap.getObj(formComponent.code.queryCode)==undefined||
                    pageInfoMap.getObj(formComponent.code.queryCode).page_parent_uuid==undefined){
                }else{
                    eleMap.get('page_parent_uuid').setValue(pageInfoMap.getObj(formComponent.code.queryCode).page_parent_uuid)
                }
                eleMap.forEach( (obj,key)=>{//在赋值之前，先全部置空，因为Mongo建表和MySQL不一样
                    obj.data.is_read_only='Y';//强制修改为只读
                    obj.setDisabled();
                    obj.setValue('');
                })
                for (let key in content) {
                    if(util.isNull(eleMap.get(key))){
                        if(config.getConfigJson().logLevel=='debug') console.log('（警告）字段名：'+key+' 未在表单组件同步配置，如buildSql查询出来的可忽略')
                        continue
                    }
                    eleMap.get(key).data.is_read_only='Y';//强制修改为只读
                    eleMap.get(key).setValue(content[key])
                    eleMap.get(key).setDisabled();
                }
                formComponent.formEdit()
                khcModalForForm.show();
            }
            formComponent.renderThenFunc(content);
        });
    });
}
functionMap.set('showDetail', showDetail);// 将函数添加到 Map 中