"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";
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 editMenuRole(){
    let jsonConfig=this;
    let code={dsCode:'ds_system_menurole',formCode:'form_menurole',queryCode:''}
    jsonConfig.button_method='editOneByModalSlide';
    jsonConfig.paramsAfterFunc=function (params){
        return util.clone(code);
    }
    jsonConfig.getKhcModalForFormAfterFunc=function (pageComponent){
        return pageComponent.editMenuRoleByModalSlideForm
    }
    jsonConfig.setKhcModalForFormAfterFunc=function (pageComponent,khcModalForForm){
        pageComponent.editMenuRoleByModalSlideForm=khcModalForForm;
    }
    jsonConfig.renderThenFunc=function (content){
        let queryViewComponent = fullMap.getObj(jsonConfig.objID);
        let khcModalForForm=fullMap.getObj(queryViewComponent.pageID).editMenuRoleByModalSlideForm;
        let formComponent=khcModalForForm.formComponent;
        let eleMap = formComponent.getFormMap();
        let params=util.clone(code);
        params.menuId=content.menu_id;
        params.methodName='getAllRoleByMenuId';
        let obj={url:config.getConfigJson().myActionUrl,params:params}
        httpUtil.post(obj, (response)=>{
            eleMap.get('menu_id').setValue(content.menu_id)
            eleMap.get('role_id').setValue(response.data.data.data)
        })
        formComponent.setSaveAfter( ()=> {
            queryViewComponent.getTableObj().refresh();
            khcModalForForm.hide();
        })
        formComponent.formEdit();
        khcModalForForm.show();
    }
    util.import2023(config.getConfigJson().tableActionPath,jsonConfig);
}
functionMap.set('editMenuRole', editMenuRole);// 将函数添加到 Map 中

function editMenuIcon(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let tableViewComponent = queryViewComponent.getTableObj();
    let content = tableViewComponent.getContentMap().get(jsonConfig.pkCode.toString());
    let params = {};
    params.dsCode='ds_system_menu';
    params.formCode='form_menu_icon';
    let khcModalForForm = new khcModal({ modalAreaID: config.getConfigJson().modalAreaID });
    khcModalForForm.render().then(() => {
        let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params ,pageID:queryViewComponent.pageID});
        formComponent.khcModalForForm=khcModalForForm;
        khcModalForForm.formObjID=formComponent.getObjID()
        let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
        pageInfoMap.push(khcModalForFormKey,khcModalForForm);
        formComponent.setFormShowType('modals')
        formComponent.render().then(() => {
            let eleMap = formComponent.getFormMap();
            for (let key in content) {
                eleMap.get(key).setValue(content[key])
            }
            //重新渲染 menu_icon
            let labelObjArray = document.getElementById(formComponent.formMap.get("menu_icon").objID).querySelector('div').querySelectorAll('label');
            for (let labelObj of labelObjArray) {
                let spanObj = labelObj.querySelector('span');
                let iconHtml='<span style="cursor:pointer;"><i class="'+util.toOldChar(spanObj.innerHTML)+'"></i></span>';
                spanObj.innerHTML=iconHtml;
                spanObj.style.fontSize='20px'
                spanObj.style.width='40px'
            }
            // 隐藏input
            let eleObj=document.getElementsByName(formComponent.formMap.get("menu_icon").eleID);
            for (let i = 0; i < eleObj.length; i++) {
                eleObj[i].style.display ='none'
            }
            document.getElementById(formComponent.formMap.get("menu_icon").objID).addEventListener("click",(e)=>{
                if(e.target.tagName=="INPUT"){
                    formComponent.saveData();
                    khcModalForForm.hide();
                }
            })
            formComponent.formEdit();
            khcModalForForm.show();
        });
        //同理可以实现saveBefore
        formComponent.setSaveAfter( ()=> {
            queryViewComponent.getTableObj().refresh();
            khcModalForForm.hide();
        })
    })
}
functionMap.set('editMenuIcon', editMenuIcon);// 将函数添加到 Map 中