"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 function main(jsonConfig) {
    try {
        functionMap.get(jsonConfig.button_method).apply(jsonConfig);
    } catch (error) {
        khcAlert.showSystemError(error.message);
        console.log(error);
    }
}
function editSystemConfig(){
    let jsonConfig=this;
    jsonConfig.button_method='editOneByModalSlide';
    jsonConfig.paramsAfterFunc=function (params){
        params={};
        params.dsCode='ds_template';
        params.formCode='form_template_system_config';
        return util.clone(params);
    }
    jsonConfig.getKhcModalForFormAfterFunc=function (pageComponent){
        return pageComponent.editSystemConfigByModalSlideForm
    }
    jsonConfig.setKhcModalForFormAfterFunc=function (pageComponent,khcModalForForm){
        pageComponent.editSystemConfigByModalSlideForm=khcModalForForm;
    }
    jsonConfig.renderThenFunc=function (content){
        let queryViewComponent = fullMap.getObj(jsonConfig.objID);
        let khcModalForForm=fullMap.getObj(queryViewComponent.pageID).editSystemConfigByModalSlideForm;
        let formComponent=khcModalForForm.formComponent;
        let eleMap = formComponent.getFormMap();
        let configJson=JSON.parse(content['base_value'])
        eleMap.get('password_error_num').setValue(configJson['password_error_num'])
        eleMap.get('password_valid_day_num').setValue(configJson['password_valid_day_num'])
        eleMap.get('password_remind_day_num').setValue(configJson['password_remind_day_num'])
        eleMap.get('menu_render_type').setValue(configJson['menu_render_type'])
        formComponent.setSaveBefore(()=>{
            let newConfigJson={}
            newConfigJson.password_error_num=eleMap.get('password_error_num').getValue();
            newConfigJson.password_valid_day_num=eleMap.get('password_valid_day_num').getValue();
            newConfigJson.password_remind_day_num=eleMap.get('password_remind_day_num').getValue();
            newConfigJson.menu_render_type=eleMap.get('menu_render_type').getValue();
            let newConfigStr=util.toSafe(JSON.stringify(newConfigJson));
            content['base_value']=newConfigStr;
            console.log(content)
            fullMap.getObj('mask').show('正在保存...')
            let formCode = 'form_base_config';
            let dsCode = 'ds_base_config';
            let formValueArray=[];
            formValueArray.push(content)
            let params = { "dsCode": dsCode, "formCode": formCode, "saveData": formValueArray }
            let obj={url:config.getConfigJson().saveUrl,params:params}
            httpUtil.post(obj, (response)=>{
                fullMap.getObj('mask').hide();
                khcAlert.showSuccessMessage('保存成功')
                formComponent.runDefaultFunc();
                formComponent.runSaveAfter();
            })
            return false;
        })
        formComponent.setSaveAfter( ()=> {
            queryViewComponent.getTableObj().refresh();
            khcModalForForm.hide();
        })
        formComponent.formEdit();
        khcModalForForm.show();
    }
    util.import2023(config.getConfigJson().tableActionPath,jsonConfig);
}
functionMap.set('editSystemConfig', editSystemConfig);// 将函数添加到 Map 中

function exportDBTable(){
    let jsonConfig=this;
    let queryComponent = fullMap.getObj(jsonConfig.objID);
    let params = util.clone(queryComponent.getTableObj().code);
    params=util.clone(params);
    params.queryCode='query_base_config_export_db_table_template';
    params.pkId=jsonConfig.pkCode;
    fullMap.getObj('mask').show(`正在导出中...`)
    let obj={url:config.getConfigJson().exportWordDataUrl,params:params}
    return httpUtil.post(obj, (response)=>{
        fullMap.getObj('mask').hide();
        khcToastr.showSuccessMessage('导出成功');
        let downExcelUrl=config.getConfigJson().downFileUrl+'?realFileName='+response.data.data.realFileName+'&fileDiskName='+response.data.data.diskFileName+'&suffix='+response.data.data.suffix
        window.open(downExcelUrl);
    })
}
functionMap.set('exportDBTable', exportDBTable);// 将函数添加到 Map 中

function exportApi(){
    let jsonConfig=this;
    let queryComponent = fullMap.getObj(jsonConfig.objID);
    let params = util.clone(queryComponent.getTableObj().code);
    params=util.clone(params);
    params.queryCode='query_base_config_export_api_template';
    params.pkId=jsonConfig.pkCode;
    fullMap.getObj('mask').show(`正在导出中...`)
    let obj={url:config.getConfigJson().exportWordDataUrl,params:params}
    return httpUtil.post(obj, (response)=>{
        fullMap.getObj('mask').hide();
        khcToastr.showSuccessMessage('导出成功');
        let downExcelUrl=config.getConfigJson().downFileUrl+'?realFileName='+response.data.data.realFileName+'&fileDiskName='+response.data.data.diskFileName+'&suffix='+response.data.data.suffix
        window.open(downExcelUrl);
    })
}
functionMap.set('exportApi', exportApi);// 将函数添加到 Map 中