"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 {khcModalSlide} from "../../../component/alert/khcModalSlide.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);
    }
}
/**
 * 右侧模态框
 */
function createOneByModalSlide(){
    let jsonConfig=this;
    jsonConfig.paramsAfterFunc=function (params){
        params.formCode='form_vm_table_add';
        return params;
    }
    util.import2023(config.getConfigJson().tableActionPath,jsonConfig);
}
functionMap.set('createOneByModalSlide', createOneByModalSlide);// 将函数添加到 Map 中

function editPkFieldName(){
    let jsonConfig=this;
    let code;
    jsonConfig.paramsAfterFunc=function (params){
        params.formCode='form_vm_table_edit_pk_field';
        code=util.clone(params)
        return params;
    }
    jsonConfig.button_method='editOneByModalSlide';
    jsonConfig.getKhcModalForFormAfterFunc=function (pageComponent){
        return pageComponent.editPkFieldNameByModalSlideForm
    }
    jsonConfig.setKhcModalForFormAfterFunc=function (pageComponent,khcModalForForm){
        pageComponent.editPkFieldNameByModalSlideForm=khcModalForForm;
    }
    jsonConfig.renderThenFunc=function (content){
        let queryViewComponent = fullMap.getObj(jsonConfig.objID);
        let khcModalForForm=fullMap.getObj(queryViewComponent.pageID).editPkFieldNameByModalSlideForm;
        let formComponent=khcModalForForm.formComponent;
        let eleMap = formComponent.getFormMap();
        let queryInfo = JSON.parse(util.getUrlParam('queryInfo'));
        let pageParentUUID;
        if(util.isNull(queryInfo)){
            khcAlert.showSystemError('访问异常，请从正确的入口访问（00003）');
            console.error('queryInfo is null')
        }else{
            let params=util.clone(code);
            params['page_parent_uuid']=queryInfo['page_parent_uuid'];
            params.methodName='getPkFieldInfo';
            let obj={url:config.getConfigJson().myActionUrl,params:params}
            httpUtil.post(obj, (response)=>{
                let auto=response.data.data.data;
                eleMap.get('this_auto').setValue(auto)
                eleMap.get('next_auto').setValue(auto+1)
                eleMap.get('myself_auto').setValue()
            })
            for (let key in content) {
                if(util.isNull(eleMap.get(key))){
                    if(config.getConfigJson().logLevel=='debug') console.log('（警告）字段名：'+key+' 未在表单组件同步配置')
                    continue
                };
                eleMap.get(key).setValue(content[key])
            }
            formComponent.setSaveBefore( ()=> {
                let nextAuto=Number(eleMap.get('next_auto').getValue());
                let myselfAuto=Number(eleMap.get('myself_auto').getValue());
                if(myselfAuto>nextAuto){
                    return true;
                }else{
                    khcAlert.showSystemError('自定义起始位置必须大于：'+nextAuto);
                    return false;
                }
            })
            formComponent.setSaveAfter( ()=> {
                queryViewComponent.getTableObj().refresh();
                khcModalForForm.hide();
            })
            formComponent.formEdit();
            khcModalForForm.show();
        }

    }
    util.import2023(config.getConfigJson().tableActionPath,jsonConfig);
}
functionMap.set('editPkFieldName', editPkFieldName);// 将函数添加到 Map 中

function showCreateTableSql(){
    let jsonConfig=this;
    let code;
    jsonConfig.paramsAfterFunc=function (params){
        params.formCode='form_vm_table_show_sql';
        code=util.clone(params)
        return params;
    }
    jsonConfig.button_method='createOneByModalSlide';
    jsonConfig.getKhcModalForFormAfterFunc=function (pageComponent){
        return pageComponent.editPkFieldNameByModalSlideForm
    }
    jsonConfig.setKhcModalForFormAfterFunc=function (pageComponent,khcModalForForm){
        pageComponent.editPkFieldNameByModalSlideForm=khcModalForForm;
    }
    jsonConfig.renderThenFunc=function (content){
        let queryViewComponent = fullMap.getObj(jsonConfig.objID);
        let khcModalForForm=fullMap.getObj(queryViewComponent.pageID).editPkFieldNameByModalSlideForm;
        let formComponent=khcModalForForm.formComponent;
        let eleMap = formComponent.getFormMap();
        let queryInfo = JSON.parse(util.getUrlParam('queryInfo'));
        if(util.isNull(queryInfo)){
            khcAlert.showSystemError('访问异常，请从正确的入口访问（00003）');
            console.error('queryInfo is null')
        }else{
            let params=util.clone(code);
            params['page_parent_uuid']=queryInfo['page_parent_uuid'];
            params.methodName='showCreateTableSql';
            let obj={url:config.getConfigJson().myActionUrl,params:params}
            httpUtil.post(obj, (response)=>{
                let createTableSql=response.data.data.data;
                eleMap.get('show_create_table_sql').setValue(createTableSql)
            })
            formComponent.setSaveAfter( ()=> {
                queryViewComponent.getTableObj().refresh();
                khcModalForForm.hide();
            })
            formComponent.formCreate();
            khcModalForForm.show();
        }

    }
    util.import2023(config.getConfigJson().tableActionPath,jsonConfig);
}
functionMap.set('showCreateTableSql', showCreateTableSql);// 将函数添加到 Map 中
