"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 {config} from "../../../config.js";
import {pageInfoMap} from "../../../pageInfoMap.js";
import {httpUtil} from "../../../util/httpUtil.js";
import {khcToastr} from "../../../component/alert/khcToastr.js";
let functionMap=new Map();// 使用 Map 来存储多个函数。每个函数都通过 set 方法添加到 Map 中，键是函数名，值是对应的函数。
export async function main(jsonConfig) {
    try {
        functionMap.get(jsonConfig.button_method).apply(jsonConfig);
    } catch (error) {
        khcAlert.showSystemError(error.message);
        console.log(error);
    }
}
// 预览
function previewCUD(){
    let jsonConfig=this;
    jsonConfig.button_method='editOneByModalSlide'
    jsonConfig.paramsAfterFunc=function (params){
        params.formCode='form_cud_template';
        return util.clone(params);
    }
    jsonConfig.getKhcModalForFormAfterFunc=function (pageComponent){
        return pageComponent.editUserDefaultLoginMobileByModalSlideForm;
    }
    jsonConfig.setKhcModalForFormAfterFunc=function (pageComponent,khcModalForForm){
        pageComponent.editUserDefaultLoginMobileByModalSlideForm=khcModalForForm;
    }
    jsonConfig.renderThenFunc=(content)=>{
        let queryViewComponent = fullMap.getObj(jsonConfig.objID);
        let khcModalForForm=fullMap.getObj(queryViewComponent.pageID).editUserDefaultLoginMobileByModalSlideForm;
        let formComponent=khcModalForForm.formComponent;
        formComponent.renderValue(content).then(()=>{
            let eleMap = formComponent.getFormMap();
            let dsCodeObj=eleMap.get('ds_code');
            let queryCodeObj=eleMap.get('query_code')
            let formCodeObj=eleMap.get('form_code')
            queryCodeObj.setValue('');
            formCodeObj.setValue('');
            eleMap.get('cud_preview_type').setValue('');
            eleMap.get('cud_preview_type').removeDisabled();
            document.getElementById(dsCodeObj.getEleID()).setAttribute("autocomplete","off")
            document.getElementById(queryCodeObj.getEleID()).setAttribute("autocomplete","off")
            document.getElementById(formCodeObj.getEleID()).setAttribute("autocomplete","off")
            let params={};
            params.dsCode='ds_page_component';
            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);
                    })
                }
            })
            if(content['type']=='create'){
                eleMap.get('cud_preview_type').setValue('2');
                eleMap.get('cud_preview_type').setDisabled();
            }
            if(content['type']=='delete'){
                eleMap.get('cud_preview_type').setValue('1');
                eleMap.get('cud_preview_type').setDisabled();
            }
            if(content['data'].indexOf(',')>0){
                khcAlert.showWarningMessage('不支持批量编辑预览')
                return ;
            }
            formComponent.formEdit();//一些高级属性在新增时大部分是不需要设置所以隐藏，在编辑时再展示
            formComponent.setButtonParams(jsonConfig.buttonParams);
            khcModalForForm.show();
        })
    }
    util.import2023(config.getConfigJson().tableActionPath,jsonConfig);
}
functionMap.set('previewCUD', previewCUD);// 将函数添加到 Map 中