"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";
import {khcModalSlide} from "../../../component/alert/khcModalSlide.js";
import {page} from "../../../component/page/page.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);
    }
}
let khcModalForForm=null;
function initQueryField(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let params =queryViewComponent.getCode();
    params.page_parent_uuid=pageInfoMap.getObj('query_info').page_parent_uuid;
    params.methodName='initQueryField';
    let obj={url:config.getConfigJson().myActionUrl,params:params}
    return httpUtil.post(obj, (response)=>{
        Swal.fire(
            '成功',
            '同步成功',
            'success'
        )
        queryViewComponent.getTableObj().refresh();
    })
}
functionMap.set('initQueryField', initQueryField);// 将函数添加到 Map 中

function previewQueryInfo(){
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let tableViewComponent = queryViewComponent.getTableObj();
    if((tableViewComponent.tUtil!=null&&tableViewComponent.isEditTableData())){
        khcAlert.showWarningMessage('检测到当前表格数据有改动，请先保存当前页数据')
        return false;//返回false
    }
    let tableDataMap = tableViewComponent.getContentMap();
    let pageComponent=fullMap.getObj(queryViewComponent.pageID);
    //先查询出dsCode和queryCode
    let queryParams = util.clone(tableViewComponent.code);
    queryParams.methodName='queryDsCodeAndQueryCode';
    queryParams.page_parent_uuid=pageInfoMap.getObj(pageComponent.formCode.code.queryCode).page_parent_uuid;
    let obj={url:config.getConfigJson().myActionUrl,params:queryParams}
    httpUtil.post(obj, (response)=>{
        let khcModalForForm = new khcModalSlide({ modalAreaID: config.getConfigJson().modalAreaID });
        fullMap.getObj('mask').show('渲染中...')
        khcModalForForm.render().then(() => {
            fullMap.getObj('mask').show('渲染中...')
            let parentObjID =khcModalForForm.getEleID();
            let pageData = { parentObjID: parentObjID,pageType:'ordinary' };
            let code={ version: '1.0.0', dsCode: response.data.data.ds_code,
                formCode: '',queryCode: response.data.data.query_code,col: 12};
            code.timestamp=new Date().getTime();//预览功能，加个时间戳每次取最新的渲染
            let formCode = { parentObjID: util.getUUID() + 'div', type: 'form', code: code }
            let queryCode = { parentObjID: util.getUUID() + 'div', type: 'query', code: code };
            pageData['componentName']=''
            pageData['pageCode'] = '';
            pageData['formCode'] = formCode;
            pageData['queryCode'] = queryCode;
            let thisFormPage = new page(pageData);
            thisFormPage.render();
            fullMap.push(thisFormPage.objID, thisFormPage);
            setTimeout(function (){
                //searchFunc()
                khcToastr.showWarningMessage('预览部分功能受限')
                fullMap.getObj('mask').hide();
            },1000)//首次渲染需要延迟调用搜索
            khcModalForForm.show();
        })
    })
}
functionMap.set('previewQueryInfo', previewQueryInfo);// 将函数添加到 Map 中