"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 {httpUtil} from "../../../util/httpUtil.js";
import {config} from "../../../config.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 createOne(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let params = {};
    let componentArray = fullMap.getObj(queryViewComponent.pageID).componentArray;
    for (let i = 0; i < componentArray.length; i++) {
        if (componentArray[i].type == 'form') {
            params = componentArray[i].code;
        }
    }
    let formPage = fullMap.getObj(queryViewComponent.pageID);
    formPage.formComponent.initForm();
    let eleMap = formPage.formComponent.getFormMap();
    let queryParam=pageInfoMap.getObj(formPage.formComponent.code.queryCode);
    eleMap.get('page_parent_uuid').setValue(queryParam.page_parent_uuid)
    formPage.formComponent.formCreate();//一些高级属性在新增时大部分是不需要设置所以隐藏，在编辑时再展示
    formPage.showComponent('form');
    formPage.formComponent.setSaveAfter(function () {
        formPage.formComponent.clearValue();
        formPage.showComponent('query');
        formPage.queryComponent.getTableObj().refresh();
    });
}
functionMap.set('createOne', createOne);// 将函数添加到 Map 中

function initButton(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let params =queryViewComponent.getCode();
    params.page_parent_uuid=pageInfoMap.getObj(params.queryCode).page_parent_uuid;
    params.methodName='initButton';
    let obj={url:config.getConfigJson().myActionUrl,params:params}
    return httpUtil.post(obj, (response)=>{
        Swal.fire(
            '成功',
            '同步成功',
            'success'
        )
        queryViewComponent.getTableObj().refresh();
    })
}
functionMap.set('initButton', initButton);// 将函数添加到 Map 中

function editButtonIcon(){
    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_button';
    params.formCode='form_button_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("button_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("button_icon").eleID);
            for (let i = 0; i < eleObj.length; i++) {
                eleObj[i].style.display ='none'
            }
            document.getElementById(formComponent.formMap.get("button_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('editButtonIcon', editButtonIcon);// 将函数添加到 Map 中