"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";
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 toComponent(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let tableViewComponent = queryViewComponent.getTableObj();
    let content = tableViewComponent.getContentMap().get(jsonConfig.pkCode.toString());
    let queryInfo={"page_parent_uuid":content.uuid};
    let pageCode='page_component';
    window.open(config.getIndexUrl()+'?pageCode='+pageCode+'&queryInfo='+JSON.stringify(queryInfo));
}
functionMap.set('toComponent', toComponent);// 将函数添加到 Map 中

function editPageRole2(){
    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_page_role';
    params.formCode='form_page_role';
    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();
            params.pageId=content.id;
            params.methodName='getAllRoleByPageId';
            let obj={url:config.getConfigJson().myActionUrl,params:params}
            httpUtil.post(obj, (response)=>{
                eleMap.get('page_id').setValue(content.id)
                eleMap.get('role_id').setValue(response.data.data.data)
            })
            formComponent.formEdit();
            khcModalForForm.show();
        });
        formComponent.setSaveAfter( ()=> {
            khcModalForForm.hide();
        })
    })
}
functionMap.set('editPageRole2', editPageRole2);// 将函数添加到 Map 中

function editPageRole(){
    let jsonConfig=this;
    let code={dsCode:'ds_page_role',formCode:'form_page_role',queryCode:''}
    jsonConfig.button_method='editOneByModalSlide';
    jsonConfig.paramsAfterFunc=function (params){
        return util.clone(code);
    }
    jsonConfig.getKhcModalForFormAfterFunc=function (pageComponent){
        return pageComponent.editPageRoleByModalSlideForm
    }
    jsonConfig.setKhcModalForFormAfterFunc=function (pageComponent,khcModalForForm){
        pageComponent.editPageRoleByModalSlideForm=khcModalForForm;
    }
    jsonConfig.renderThenFunc=function (content){
        let queryViewComponent = fullMap.getObj(jsonConfig.objID);
        let khcModalForForm=fullMap.getObj(queryViewComponent.pageID).editPageRoleByModalSlideForm;
        let formComponent=khcModalForForm.formComponent;
        let eleMap = formComponent.getFormMap();
        let params=util.clone(code);
        params.pageId=content.id;
        params.methodName='getAllRoleByPageId';
        let obj={url:config.getConfigJson().myActionUrl,params:params}
        httpUtil.post(obj, (response)=>{
            eleMap.get('page_id').setValue(content.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('editPageRole', editPageRole);// 将函数添加到 Map 中