"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";
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 createOneByModalSlide(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let params = {};
    let pageComponent=fullMap.getObj(queryViewComponent.pageID);
    let componentArray = pageComponent.componentArray;
    for (let i = 0; i < componentArray.length; i++) {
        if (componentArray[i].type == 'form') {
            params = componentArray[i].code;
        }
    }
    let khcModalForForm=pageComponent.createOneByModalSlideForm;
    if(khcModalForForm==null){
        khcModalForForm = new khcModalSlide({ modalAreaID: config.getConfigJson().modalAreaID });
        pageComponent.createOneByModalSlideForm=khcModalForForm;
        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.setSaveAfter( ()=> {
                let pkId = formComponent.saveOneData.id;
                let dsCode = formComponent.code.dsCode;
                let formCode = formComponent.code.formCode;
                let queryCode = pageComponent.queryComponent.code.queryCode;
                let pageCode = pageComponent.pageCode;
                let processInstanceID = formComponent.saveOneData.process_instance_id;
                let taskId = formComponent.saveOneData.process_task_id;
                if (!util.isNull(processInstanceID)) {
                    let editObj={pageCode:pageCode,dsCode:dsCode,queryCode:queryCode,formCode:formCode,pkId:pkId,taskId:taskId};
                    util.toEditPage(editObj);
                } else {
                    formComponent.clearValue();
                    khcModalForForm.hide();
                    pageComponent.queryComponent.getTableObj().refresh();
                }
            })
            formComponent.render().then(() => {
                formComponent.renderThenFunc=()=>{
                    let eleMap = formComponent.getFormMap();
                    if(pageInfoMap.getObj(formComponent.code.queryCode)!=undefined){
                        eleMap.get('page_parent_uuid').setValue(pageInfoMap.getObj(formComponent.code.queryCode).page_parent_uuid)
                    }
                    let dsCodeObj=eleMap.get('ds_code');
                    let queryCodeObj=eleMap.get('query_code')
                    let formCodeObj=eleMap.get('form_code')
                    util.removeEventListener(queryCodeObj.getEleID(),'blur',queryCodeObj.getEleID().validateFunc)//移除校验事件
                    util.removeEventListener(formCodeObj.getEleID(),'blur',formCodeObj.getEleID().validateFunc)//移除校验事件
                    document.getElementById(dsCodeObj.getEleID()).setAttribute("autocomplete","off")
                    document.getElementById(queryCodeObj.getEleID()).setAttribute("autocomplete","off")
                    document.getElementById(formCodeObj.getEleID()).setAttribute("autocomplete","off")
                    dsCodeObj.callback= (eleID,item)=>{
                        dsCodeObj.setValue(item);
                        queryCodeObj.setValue('');
                        formCodeObj.setValue('');
                        params.ds_code=item;
                        params.methodName='findComponentData';
                        let obj={url:config.getConfigJson().myActionUrl,params:params}
                        httpUtil.post(obj, (response)=>{
                            let queryCodeFunc=function (item){
                                queryCodeObj.setValue(item);
                                queryCodeObj.validate();
                            }
                            let formCodeFunc=function (item){
                                formCodeObj.setValue(item);
                                formCodeObj.validate();
                            }
                            let initCodeFunc=function (response){
                                util.removeEventListener(queryCodeObj.getEleID(),'blur',queryCodeObj.validateFunc)//去掉文本框绑定的事件
                                util.removeEventListener(formCodeObj.getEleID(),'blur',formCodeObj.validateFunc)//去掉文本框绑定的事件
                                util.autocomplete({
                                    inp:document.getElementById(queryCodeObj.getEleID()),
                                    lookup:response.data.data.queryData,
                                    onSelect: queryCodeFunc
                                });
                                util.autocomplete({
                                    inp:document.getElementById(formCodeObj.getEleID()),
                                    lookup:response.data.data.formData,
                                    onSelect: formCodeFunc
                                });
                            }
                            initCodeFunc(response);
                        })
                    }
                    formComponent.formCreate();
                    khcModalForForm.show();
                }
                formComponent.renderThenFunc()
            });
        })
    }else{
        let formComponent=khcModalForForm.formComponent
        formComponent.renderThenFunc();

    }

}
functionMap.set('createOneByModalSlide', createOneByModalSlide);// 将函数添加到 Map 中
/**
 * 右侧模态框
 * @param objID
 */
function editOneByModalSlide(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let tableViewComponent = queryViewComponent.getTableObj();
    let content = tableViewComponent.getContentMap().get(jsonConfig.pkCode.toString());
    let params = {};
    let pageComponent=fullMap.getObj(queryViewComponent.pageID);
    let componentArray = pageComponent.componentArray;
    for (let i = 0; i < componentArray.length; i++) {
        if (componentArray[i].type == 'form') {
            params = componentArray[i].code;
        }
    }
    let khcModalForForm=pageComponent.editOneByModalSlideForm;
    if(khcModalForForm==null){
        khcModalForForm = new khcModalSlide({ modalAreaID: config.getConfigJson().modalAreaID });
        pageComponent.editOneByModalSlideForm=khcModalForForm;
        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);//将khcModal存到pageMap里,拿到此对象可控制隐藏和展示
            formComponent.setFormShowType('modals')
            formComponent.render().then(() => {
                formComponent.renderThenFunc=(content)=>{
                    let eleMap = formComponent.getFormMap();
                    let dsCodeObj=eleMap.get('ds_code');
                    let queryCodeObj=eleMap.get('query_code')
                    let formCodeObj=eleMap.get('form_code')
                    document.getElementById(dsCodeObj.getEleID()).setAttribute("autocomplete","off")
                    document.getElementById(queryCodeObj.getEleID()).setAttribute("autocomplete","off")
                    document.getElementById(formCodeObj.getEleID()).setAttribute("autocomplete","off")
                    for (let key in content) {
                        eleMap.get(key).setValue(content[key])
                    }
                    params.ds_code=content.ds_code;
                    params.methodName='findComponentData';
                    let obj={url:config.getConfigJson().myActionUrl,params:params}
                    httpUtil.post(obj, (response)=>{
                        let queryCodeFunc=function (item){
                            queryCodeObj.setValue(item);
                            queryCodeObj.validate();
                        }
                        let formCodeFunc=function (item){
                            formCodeObj.setValue(item);
                            formCodeObj.validate();
                        }
                        let initCodeFunc=function (response){
                            util.removeEventListener(queryCodeObj.getEleID(),'blur',queryCodeObj.validateFunc)//去掉文本框绑定的事件
                            util.removeEventListener(formCodeObj.getEleID(),'blur',formCodeObj.validateFunc)//去掉文本框绑定的事件
                            util.autocomplete({
                                inp:document.getElementById(queryCodeObj.getEleID()),
                                lookup:response.data.data.queryData,
                                onSelect: queryCodeFunc
                            });
                            util.autocomplete({
                                inp:document.getElementById(formCodeObj.getEleID()),
                                lookup:response.data.data.formData,
                                onSelect: formCodeFunc
                            });
                        }
                        initCodeFunc(response);
                        dsCodeObj.callback= (eleID,item)=>{
                            dsCodeObj.setValue(item);
                            queryCodeObj.setValue('');
                            formCodeObj.setValue('');
                            params.ds_code=item;
                            params.methodName='findComponentData';
                            let obj={url:config.getConfigJson().myActionUrl,params:params}
                            httpUtil.post(obj, (response)=>{
                                initCodeFunc(response);
                            })
                        }
                    })
                    let read=pageInfoMap.getObj('readOnly#'+params.formCode)
                    if(read!=undefined){
                        for (let i = 0; i < read.length; i++) {
                            eleMap.get(read[i]).setDisabled();
                        }
                    }
                    let canNull=pageInfoMap.getObj('canNull#'+params.formCode)
                    //同理可以实现saveBefore
                    formComponent.setSaveAfter( ()=> {
                        queryViewComponent.getTableObj().refresh();
                        khcModalForForm.hide();
                    })
                    if(canNull!=undefined){
                        for (let i = 0; i < canNull.length; i++) {
                            eleMap.get(canNull[i]).data.can_null='Y';
                            eleMap.get(canNull[i]).refreshCanNull();
                        }
                    }
                    formComponent.formEdit();//一些高级属性在新增时大部分是不需要设置所以隐藏，在编辑时再展示
                    khcModalForForm.show();
                }
                formComponent.renderThenFunc(content);
            });
        })
    }else{
        let formComponent=khcModalForForm.formComponent
        formComponent.renderThenFunc(content);
    }
}
functionMap.set('editOneByModalSlide', editOneByModalSlide);// 将函数添加到 Map 中