"use strict";
import { fullMap } from '../../../fullMap.js';
import { khcAlert } from '../../../component/alert/khcAlert.js'
import {util} from "../../../util/util.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 queryInfoPageRenderAfter(){
    let jsonConfig=this;
    let homeComponent=fullMap.getObj(jsonConfig.objID)
    let queryInfo = JSON.parse(util.getUrlParam('queryInfo'));
    if(util.isNull(queryInfo)){
        khcAlert.showSystemError('访问异常，请从正确的入口访问（00003）');
        console.error('queryInfo is null')
    }else{
        let params={};
        params.dsCode = 'ds_query';
        params.queryCode = 'query';
        params.formCode = 'form_query';
        params.formVersion = '1.0.0';
        params.pkFieldId='uuid';
        params.pkId = queryInfo['page_parent_uuid'];
        params.valueToShowValue= 'Y';
        let obj={url:config.getConfigJson().queryOneUrl,params:params}
        httpUtil.post(obj,(response)=>{
            console.log(response.data.data)
            document.getElementById(homeComponent.getParentPageDivId()+'_header').style.display=''
            document.getElementById(homeComponent.getParentPageDivId()+'_header_default').innerText=response.data.data['query_name']+'--查询组件字段维护'
            let html=`<div class="row no-gutters row-bordered ui-bordered text-center mb-1 mt-3">
                  <div class="d-flex col flex-column text-dark py-3" style="padding-top: 0.6rem !important;padding-bottom: 0.6rem !important;">
                    <div class="font-weight-bold">${response.data.data['query_name']}</div>
                    <div class="text-muted small">查询组件名称</div>
                  </div>
                  <div class="d-flex col flex-column text-dark py-3" style="padding-top: 0.6rem !important;padding-bottom: 0.6rem !important;">
                    <div class="font-weight-bold">${response.data.data['query_code']}</div>
                    <div class="text-muted small">查询组件编码</div>
                  </div>
                  <div class="d-flex col flex-column text-dark py-3" style="padding-top: 0.6rem !important;padding-bottom: 0.6rem !important;">
                    <div class="font-weight-bold" id="${homeComponent.getParentPageDivId()}_header_content_ds_code"></div>
                    <div class="text-muted small">数据源编码</div>
                  </div>
                  <div class="d-flex col flex-column text-dark py-3" style="padding-top: 0.6rem !important;padding-bottom: 0.6rem !important;">
                    <div class="font-weight-bold" id="${homeComponent.getParentPageDivId()}_header_content_ds_table_name"></div>
                    <div class="text-muted small">表名</div>
                  </div>
                </div>`
            document.getElementById(homeComponent.getParentPageDivId()+'_header_content_div').insertAdjacentHTML("beforeEnd",html)
            renderDsCode(homeComponent,response.data.data['page_parent_uuid']);
        })
    }
}
functionMap.set('queryInfoPageRenderAfter', queryInfoPageRenderAfter);// 将函数添加到 Map 中

function renderDsCode(homeComponent,page_parent_uuid){
    let params={};
    params.dsCode = 'ds';
    params.queryCode = 'query_ds';
    params.formCode = 'form_ds';
    params.formVersion = '1.0.0';
    params.pkFieldId='uuid';
    params.pkId = page_parent_uuid;
    let obj={url:config.getConfigJson().queryOneUrl,params:params}
    httpUtil.post(obj,(response)=>{
        console.log(response.data.data)
        document.getElementById(homeComponent.getParentPageDivId()+'_header_content_ds_code').innerText=response.data.data['ds_code']
        document.getElementById(homeComponent.getParentPageDivId()+'_header_content_ds_table_name').innerText=response.data.data['ds_table_name']
    })
}