"use strict";
import { fullMap } from '../../../fullMap.js';
import { pageInfoMap } from '../../../pageInfoMap.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 {khcMask} from "../../../component/alert/khcMask.js";
import {httpUtil} from "../../../util/httpUtil.js";
import {khcModalSlide} from "../../../component/alert/khcModalSlide.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 dsTableRenderAfter(){
    let jsonConfig=this;
    let table=fullMap.getObj(jsonConfig.objID);
    //隐藏最外层背景和分页信息
    document.getElementById(table.getObjID()).className='';
    let that=table;
    if(!document.getElementById('showDataSource')){//判断是否第一次加载，是就放一个div
        document.getElementById(table.getObjID()).children[0].children[0].children[0].insertAdjacentHTML("afterEnd", '<div id="showDataSource" class="row"></div>');
        document.getElementById(table.getObjID()).style='';
        document.getElementById(table.getObjID()).children[0].setAttribute('style','padding: 0.15rem 0')
        document.getElementById(table.getObjID()).children[0].className='card-datatable table-responsive mt-4';
        document.getElementById(table.eleID).parentNode.parentNode.parentNode.setAttribute('style','display: none;')
        document.getElementById(table.eleID+'pageInfoWithPage').parentNode.parentNode.parentNode.setAttribute('style','display: none;')
    }else{
        document.getElementById('showDataSource').innerHTML='';
    }
    let iconButtonHtml='';
    let buttonArray=table.queryInfo.rowButtonArray;
    for (let j = 0; j < buttonArray.length; j++) {
        iconButtonHtml+='                        <span name="'+buttonArray[j].button_name+'" class="'+buttonArray[j].button_icon+'" style="cursor:pointer" title="'+buttonArray[j].button_title+'"></span>&nbsp;\n';
    }

    let html='';
    let contents=table.contents;
    for (let i = 0; i < contents.length; i++) {
        let queryHtml=getQueryHtml(contents[i],table.getObjID());
        let formHtml=getFormHtml(contents[i],table.getObjID())
        let bpmModelButtonHtml='';
        html+=`<div class="col-md" style="padding-right: 0rem;padding-left: 0;">
                            <div class="card mb-3">
                              <div class="card-header with-elements">
                                <span class="card-header-title mr-2">${contents[i].ds_name}</span>&nbsp;&nbsp;
                                <div class="card-header-elements ml-md-auto"  id="${table.getObjID()}_icon${contents[i].id}">
                                      ${iconButtonHtml}
                                </div>
                              </div>
                              <div class="card-body">
                                <div class="form-row">
                                  <div class="col-sm-4">
                                    <label class="col-sm-12">编码：${contents[i].ds_code}</label>
                                  </div>
                                  <div class="col-sm-4">
                                    <label class="col-sm-12">表名：${contents[i].ds_table_name}</label>
                                  </div>
                                  <div class="col-sm-4">
                                    <label class="col-sm-12">主键：${contents[i].ds_pk_id}</label>
                                  </div>
                                </div>
                                <div class="form-row">
                                  <div>${queryHtml}</div>
                                </div>
                                <div class="form-row">
                                  <div>${formHtml}</div>
                                </div>
                              </div>
                              <div class="card-footer">
                                <button type="button" class="btn btn-sm btn-primary mr-2" id="${table.getObjID()}_to_table${contents[i].id}">表结构维护</button>
                                ${contents[i]['is_lucene']=='Y'?`<button type="button" class="btn btn-sm btn-info mr-2" id="${table.getObjID()}_to_lucene${contents[i].id}">Lucene配置</button>`:''}
                                
                                ${bpmModelButtonHtml}
                              </div>
                            </div>
                          </div>
                          <div class="w-100"></div>`;
    }
    document.getElementById('showDataSource').innerHTML=html;
    for (let i = 0; i < contents.length; i++) {
        for (let j = 0; j < buttonArray.length; j++) {
            let buttonEle = document.getElementById(table.getObjID()+'_icon'+contents[i].id).querySelector('span[name="'+buttonArray[j].button_name+'"]');
            buttonEle.addEventListener(buttonArray[j].button_event,  ()=> {
                let jsonConfig={objID:that.queryObjID,button_method:buttonArray[j].button_method,pkCode:contents[i].id}
                if (util.isNull(buttonArray[j].js_path)) {
                    util.import2023(config.getConfigJson().tableActionPath,jsonConfig);
                } else {
                    util.import2023(buttonArray[j].js_path,jsonConfig);
                }
            })
        }
        for (let j = 0; j < contents[i].query_data.length; j++) {
            let content=contents[i].query_data[j];
            document.getElementById(table.getObjID()+'_query_edit'+content.id).addEventListener('click',  ()=>{
                let params = {};
                params.dsCode='ds_query';
                params.formCode='form_query';
                khcModalForForm = new khcModal({ modalAreaID: config.getConfigJson().modalAreaID });
                khcModalForForm.render().then(() => {
                    let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params });
                    khcModalForForm.formObjID=formComponent.getObjID()
                    let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
                    pageInfoMap.push(khcModalForFormKey,khcModalForForm);
                    formComponent.setFormShowType('modals')
                    formComponent.render().then(() => {
                        let eleMap = formComponent.getFormMap();
                        for (let key in content) {
                            eleMap.get(key).setValue(content[key])
                        }
                        formComponent.formEdit();
                        khcModalForForm.show();
                    });
                    //同理可以实现saveBefore
                    formComponent.setSaveAfter( ()=> {
                        table.refresh()
                        khcModalForForm.hide();
                    })
                })
            })
            document.getElementById(table.getObjID()+'_query_to_field'+content.id).addEventListener('click',  ()=>{
                let queryInfo={"page_parent_uuid":content.uuid};
                let pageCode='page_ds_query_info';
                let url=config.getIndexUrl()+'?pageCode='+pageCode+'&queryInfo='+JSON.stringify(queryInfo)
                window.open(url);
            })
            document.getElementById(table.getObjID()+'_query_to_button'+content.id).addEventListener('click',  ()=>{
                let queryInfo={"page_parent_uuid":content.uuid};
                let pageCode='page_ds_button';
                let url=config.getIndexUrl()+'?pageCode='+pageCode+'&queryInfo='+JSON.stringify(queryInfo)
                window.open(url);
            })
            document.getElementById(table.getObjID()+'_query_to_simple_card'+content.id).addEventListener('click',  ()=>{
                let queryInfo={"page_parent_uuid":content.uuid};
                let pageCode='page_query_simple_card';
                let url=config.getIndexUrl()+'?pageCode='+pageCode+'&queryInfo='+JSON.stringify(queryInfo)
                window.open(url);
            })
            document.getElementById(table.getObjID()+'_query_role'+content.id).addEventListener('click',  ()=>{
                let params = {};
                params.dsCode='ds_query';
                params.formCode='form_query_role';
                khcModalForForm = new khcModal({ modalAreaID: config.getConfigJson().modalAreaID });
                khcModalForForm.render().then(() => {
                    let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params });
                    khcModalForForm.formObjID=formComponent.getObjID()
                    let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
                    pageInfoMap.push(khcModalForFormKey,khcModalForForm);
                    formComponent.setFormShowType('modals')
                    formComponent.render().then(() => {
                        let eleMap = formComponent.getFormMap();
                        for (let key in content) {
                            eleMap.get(key).setValue(content[key])
                        }
                        formComponent.formEdit();
                        khcModalForForm.show();
                    });
                    //同理可以实现saveBefore
                    formComponent.setSaveAfter( ()=> {
                        table.refresh()
                        khcModalForForm.hide();
                    })
                })
            })
            document.getElementById(table.getObjID()+'_query_copy'+content.id).addEventListener('click',  ()=>{
                let params = {};
                params.dsCode='ds_query';
                params.formCode='form_query_copy';
                khcModalForForm = new khcModal({ modalAreaID: config.getConfigJson().modalAreaID });
                khcModalForForm.render().then(() => {
                    let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params });
                    khcModalForForm.formObjID=formComponent.getObjID()
                    let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
                    pageInfoMap.push(khcModalForFormKey,khcModalForForm);
                    formComponent.setFormShowType('modals')
                    formComponent.render().then(() => {
                        let eleMap = formComponent.getFormMap();
                        for (let key in content) {
                            eleMap.get(key).setValue(content[key])
                        }
                        let fieldList=['id','org_id','uuid','create_time','create_user'];
                        for (let i = 0; i <fieldList.length; i++) {
                            if(eleMap.get(fieldList[i])!=undefined){
                                eleMap.get(fieldList[i]).setValue('');
                            }
                        }
                        eleMap.get('query_code').setValue(content['query_code']+'_copy');
                        formComponent.formEdit();
                        khcModalForForm.show();
                    });
                    //同理可以实现saveBefore
                    formComponent.setSaveAfter( ()=> {
                        params.page_parent_uuid=content.uuid;
                        params.oldCode=content['query_code'];
                        params.newCode=formComponent.formValueArray[0].query_code;
                        params.methodName='copy';
                        let obj={url:config.getConfigJson().myActionUrl,params:params}
                        httpUtil.post(obj, (response)=>{
                            //如果拷贝成功什么都不操作，如果拷贝失败，会被全局异常抓到并提示。
                        })
                        table.refresh()
                        khcModalForForm.hide();
                    })
                })
            })
            document.getElementById(table.getObjID()+'_query_delete'+content.id).addEventListener('click',  ()=>{
                let params = {};
                params.dsCode='ds_query';
                params.queryCode='query';
                params.formCode='form_query';
                params.pkIds = [{ pkId: content.id }]
                khcAlert.showDelConfirm('', ()=> {
                    let obj={url:config.getConfigJson().delUrl,params:params}
                    httpUtil.post(obj, (response)=>{
                        Swal.fire(
                            '已删除',
                            '删除成功',
                            'success'
                        )
                        table.refresh()
                    })
                })
            })
        }
        for (let j = 0; j < contents[i].form_data.length; j++) {
            let content=contents[i].form_data[j];
            document.getElementById(table.getObjID()+'_form_edit'+content.id).addEventListener('click',  ()=>{
                let params = {};
                params.dsCode='ds_form';
                params.formCode='form';
                khcModalForForm = new khcModal({ modalAreaID: config.getConfigJson().modalAreaID });
                khcModalForForm.render().then(() => {
                    let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params });
                    khcModalForForm.formObjID=formComponent.getObjID()
                    let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
                    pageInfoMap.push(khcModalForFormKey,khcModalForForm);
                    formComponent.setFormShowType('modals')
                    formComponent.render().then(() => {
                        let eleMap = formComponent.getFormMap();
                        for (let key in content) {
                            eleMap.get(key).setValue(content[key])
                        }
                        formComponent.formEdit();
                        khcModalForForm.show();
                    });
                    //同理可以实现saveBefore
                    formComponent.setSaveAfter( ()=> {
                        table.refresh()
                        khcModalForForm.hide();
                    })
                })
            })
            document.getElementById(table.getObjID()+'_form_to_field'+content.id).addEventListener('click',  ()=>{
                let queryInfo={"page_parent_uuid":content.uuid};
                let pageCode='page_ds_form_info';
                let url=config.getIndexUrl()+'?pageCode='+pageCode+'&queryInfo='+JSON.stringify(queryInfo)
                window.open(url);
            })
            document.getElementById(table.getObjID()+'_form_to_button'+content.id).addEventListener('click',  ()=>{
                let queryInfo={"page_parent_uuid":content.uuid};
                let pageCode='page_ds_button';
                let url=config.getIndexUrl()+'?pageCode='+pageCode+'&queryInfo='+JSON.stringify(queryInfo)+'&type=form'
                window.open(url);
            })
            document.getElementById(table.getObjID()+'_form_edit_pdf_template'+content.id).addEventListener('click',  ()=>{
                let params = {};
                params.dsCode='ds_pdf_template';
                params.queryCode='query_pdf_template';
                params.formCode='form_pdf_template';
                khcModalForForm = new khcModal({ modalAreaID: config.getConfigJson().modalAreaID });
                khcModalForForm.render().then(() => {
                    let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params });
                    khcModalForForm.formObjID=formComponent.getObjID()
                    let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
                    pageInfoMap.push(khcModalForFormKey,khcModalForForm);
                    formComponent.setFormShowType('modals')
                    formComponent.render().then(() => {
                        let eleMap = formComponent.getFormMap();
                        params.pkId=content.uuid;
                        params.pkFieldId='page_parent_uuid';
                        let obj={url:config.getConfigJson().queryOneUrl,params:params}
                        httpUtil.post(obj, (response)=>{

                            let pdfTemplateContent=response.data.data;
                            for (let key in pdfTemplateContent) {
                                eleMap.get(key).setValue(pdfTemplateContent[key])
                            }
                            eleMap.get('page_parent_uuid').setValue(content['uuid'])
                            if(eleMap.get('pdf_template_type').getValue()!='custom'){
                                eleMap.get('pdf_template').hide();
                            }
                            let pdfEleObj=document.getElementById(eleMap.get('pdf_template').eleID);
                            let pdfTypeEleObj=document.getElementById(eleMap.get('pdf_template_type').eleID);
                            pdfEleObj.addEventListener('input',  (e)=>{
                                let inputObj=pdfEleObj;
                                if(inputObj.value=='pdf'){
                                    fullMap.getObj('mask').show('正在生成PDF模板...');
                                    let params = {};
                                    params.dsCode='ds_base_config';
                                    params.queryCode='query_base_config';
                                    params.formCode='form_base_config';
                                    params.pkId='system.settings.ds.form.pdf.template.init';
                                    params.pkFieldId='base_code';
                                    params.uuid=content.uuid;
                                    let obj={url:config.getConfigJson().queryOneUrl,params:params}
                                    httpUtil.post(obj, (response)=>{
                                        inputObj.value=util.toOldChar(response.data.data.base_value);
                                        fullMap.getObj('mask').hide();
                                    })
                                }
                            })
                            pdfTypeEleObj.addEventListener('change',  (e)=>{
                                if(pdfTypeEleObj.value!='custom'){
                                    eleMap.get('pdf_template').hide();
                                }else{
                                    eleMap.get('pdf_template').show();
                                }
                            })
                        })
                        formComponent.formEdit();
                        khcModalForForm.show();
                    });
                    //同理可以实现saveBefore
                    formComponent.setSaveAfter( ()=> {
                        table.refresh()
                        khcModalForForm.hide();
                    })
                })
            })
            document.getElementById(table.getObjID()+'_form_preview_form_info'+content.id).addEventListener('click',  ()=>{
                //先查询出dsCode和formCode
                let queryParams = {dsCode:"ds_form_info",queryCode:"query_form_info"}
                queryParams.methodName='queryDsCodeAndFormCode';
                queryParams.page_parent_uuid=content.uuid;
                let obj={url:config.getConfigJson().myActionUrl,params:queryParams}
                httpUtil.post(obj, (response)=>{
                    let params = {};
                    params.dsCode=response.data.data.ds_code;
                    params.formCode=response.data.data.form_code;
                    params.timestamp=new Date().getTime();//时间戳是为了不走游览器缓存的表单数据，每次渲染走数据库查询最新
                    khcModalForForm = new khcModalSlide({ modalAreaID: config.getConfigJson().modalAreaID });
                    khcModalForForm.render().then(() => {
                        let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params});
                        formComponent.khcModalForForm=khcModalForForm;
                        khcModalForForm.formComponent=formComponent;
                        khcModalForForm.formObjID=formComponent.getObjID()
                        let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
                        pageInfoMap.push(khcModalForFormKey,khcModalForForm);
                        formComponent.setFormShowType('modals')
                        //同理可以实现saveBefore
                        formComponent.setSaveBefore( ()=> {
                            khcModalForForm.hide();
                            khcToastr.showWarningMessage('预览表单，不存储数据')
                            return false;//返回false，后面保存代码块无需执行
                        })
                        formComponent.render().then(() => {
                            formComponent.renderThenFunc=()=>{
                                khcToastr.showWarningMessage('预览表单，请不要填写业务数据')
                                formComponent.formCreate();
                                khcModalForForm.show();
                            }
                            formComponent.renderThenFunc();
                        });
                    })
                })
            })
            document.getElementById(table.getObjID()+'_form_copy_json'+content.id).addEventListener('click',  ()=>{
                //先查询出dsCode和formCode
                let queryParams = {dsCode:"ds_form_info",queryCode:"query_form_info"}
                queryParams.methodName='queryDsCodeAndFormCode';
                queryParams.page_parent_uuid=content.uuid;
                let obj={url:config.getConfigJson().myActionUrl,params:queryParams}
                httpUtil.post(obj, (response)=>{
                    let params = {};
                    params.dsCode=response.data.data.ds_code;
                    params.formCode=response.data.data.form_code;
                    params.timestamp=new Date().getTime();//时间戳是为了不走游览器缓存的表单数据，每次渲染走数据库查询最新
                    let obj={url:config.getConfigJson().metaUrl,params:params,isClientCache:false}//isClientCache：true 将查询结果存到临时缓存
                    httpUtil.post(obj,(response)=>{
                        let exportJSON={}
                        exportJSON.formInfo=response.data.data.form.formInfo
                        exportJSON.formData=response.data.data.form.formData
                        let khcModalForForm = new khcModal({ modalAreaID: config.getConfigJson().modalAreaID });
                        let showObjId=util.getUUID();
                        let khcModalHtml = '';
                        khcModalHtml += '<div class="card">'
                        khcModalHtml += '<h6 class="card-header">'
                        khcModalHtml += `选择导出类型`
                        khcModalHtml += '</h6>';
                        khcModalHtml += '<div style="padding: 0.1rem;display: none" class="" id="'+showObjId+'">'
                        khcModalHtml += '</div></div>';
                        khcModalHtml += '<div class="card mb-4">'
                        khcModalHtml += '<div style="padding: 0.1rem;" class="ql-container ql-snow" id="'+showObjId+'_preview">'
                        khcModalHtml += '</div></div>';
                        khcModalForForm.render().then(() => {
                            document.getElementById(khcModalForForm.getEleID()).insertAdjacentHTML("beforeEnd", khcModalHtml);
                            let html='<div class="ql-editor">'
                            html+= `<div class="card-body">
                  <div class="form-group row">
                    <label class="col-form-label col-sm-2 text-sm-right">导出类型</label>
                    <div class="col-sm-10">
                      <div class="col-sm-10" style="top:8px;vertical-align: middle;">
                            <label class="form-check form-check-inline">
                                <input class="form-check-input" type="radio" name="${showObjId}_radio" value="JSON" checked>
                                <span class="form-check-label">JSON</span>
                            </label>
                            
                            <label class="form-check form-check-inline">
                                <input class="form-check-input" type="radio" name="${showObjId}_radio" value="HTML">
                                <span class="form-check-label">HTML（待开发）</span>
                            </label>
                        </div>
                    </div>
                  </div>
                  <div class="form-group row">
                    <div class="col-sm-10 ml-sm-auto">
                      <button type="button" class="btn btn-primary" id="${showObjId}_copyButton">复制</button><button type="button" class="btn btn-info ml-4" id="${showObjId}_exportButton">导出</button>
                    </div>
                  </div>
              </div>`;
                            html+='</div>'
                            document.getElementById(showObjId+'_preview').insertAdjacentHTML("beforeEnd", html);
                            document.getElementById(`${showObjId}_copyButton`).addEventListener('click',function (){
                                let eleObj=document.getElementsByName(`${showObjId}_radio`);
                                let radioValue='';
                                for (let i = 0; i < eleObj.length; i++) {
                                    if (eleObj[i].checked == true) {
                                        radioValue=eleObj[i].value;
                                        break;
                                    }
                                }
                                if(radioValue=='JSON'){
                                    new Clipboard('.btn',{
                                        text:function(){//.btn 这个元素在就行，这里使用的是自定义复制文本内容，更多使用示例阅读官方文档：https://it.cha138.com/ios/show-45625.html
                                            return JSON.stringify(exportJSON);
                                        }
                                    })
                                    khcToastr.showSuccessMessage('内容已复制到剪切板。')
                                    return
                                }
                                if(radioValue=='HTML'){
                                    khcToastr.showWarningMessage('开发中')
                                    return
                                }
                            })
                            document.getElementById(`${showObjId}_exportButton`).addEventListener('click',function (){
                                let eleObj=document.getElementsByName(`${showObjId}_radio`);
                                let radioValue='';
                                for (let i = 0; i < eleObj.length; i++) {
                                    if (eleObj[i].checked == true) {
                                        radioValue=eleObj[i].value;
                                        break;
                                    }
                                }
                                if(radioValue=='JSON'){
                                    const blob = new Blob([JSON.stringify(exportJSON, null, 2)], { type: 'application/json' });
                                    const link = document.createElement('a');  // 创建一个链接元素
                                    link.href = URL.createObjectURL(blob);  // 将Blob转换为URL
                                    const date = new Date();
                                    const year = date.getFullYear();
                                    const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份从0开始，所以加1
                                    const day = date.getDate().toString().padStart(2, '0');
                                    link.download = `${exportJSON.formInfo.form_name}-${year}${month}${day}`+'.json';  // 设置下载文件的名称
                                    link.click();  // 触发下载
                                    khcToastr.showSuccessMessage('导出成功。')
                                    return
                                }
                                if(radioValue=='HTML'){
                                    khcToastr.showWarningMessage('开发中')
                                    return
                                }
                            })
                        })
                        khcModalForForm.show();
                        /*new Clipboard('.abtn'+content.id,{text:function(){//.btn 这个元素在就行，这里使用的是自定义复制文本内容，更多使用示例阅读官方文档：https://it.cha138.com/ios/show-45625.html
                                khcToastr.showSuccessMessage('内容已复制到剪切板。')
                                return textToCopy;
                            }
                        })*/
                    })
                })
            })
            if(content.is_bpm_model=='Y'){
                document.getElementById(table.getObjID()+'_form_edit_bpm_field'+content.id).addEventListener('click',  ()=>{
                    let params = {};
                    params.dsCode='ds_bpm_field';
                    params.queryCode='query_bpm_field_dual_list';
                    params.formCode='form_bpm_field_dual_list';
                    khcModalForForm = new khcModal({ modalAreaID: config.getConfigJson().modalAreaID });
                    khcModalForForm.render().then(() => {
                        let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params });
                        khcModalForForm.formObjID=formComponent.getObjID()
                        let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
                        pageInfoMap.push(khcModalForFormKey,khcModalForForm);
                        formComponent.setFormShowType('modals')
                        formComponent.render().then(() => {
                            let eleMap = formComponent.getFormMap();
                            params.methodName='getBpmFieldByFormCode';
                            params.form_code=content['form_code'];
                            params.ds_code=contents[i].ds_code;
                            let obj={url:config.getConfigJson().myActionUrl,params:params}
                            httpUtil.post(obj, (response)=>{
                                let fieldData=response.data.data.fieldData;
                                let dictData=response.data.data.dictData;
                                eleMap.get('field_name').data.dict_code={};
                                eleMap.get('field_name').data.dict_code.data=dictData;
                                eleMap.get('field_name').refreshHtml();
                                eleMap.get('field_name').setValue(fieldData);
                                eleMap.get('page_parent_uuid').setValue(content['uuid']);
                                eleMap.get('form_code').setValue(content['form_code']);
                                eleMap.get('ds_code').setValue(contents[i].ds_code);
                            })
                            formComponent.formEdit();
                            khcModalForForm.show();
                        });
                        //同理可以实现saveBefore
                        formComponent.setSaveAfter( ()=> {
                            table.refresh()
                            khcModalForForm.hide();
                        })
                    })
                });
                document.getElementById(table.getObjID()+'_form_edit_todo_title'+content.id).addEventListener('click',  ()=>{
                    let params = {};
                    params.dsCode='ds_todo_title';
                    params.queryCode='query_todo_title';
                    params.formCode='form_todo_title';
                    khcModalForForm = new khcModal({ modalAreaID: config.getConfigJson().modalAreaID });
                    khcModalForForm.render().then(() => {
                        let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params });
                        khcModalForForm.formObjID=formComponent.getObjID()
                        let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
                        pageInfoMap.push(khcModalForFormKey,khcModalForForm);
                        formComponent.setFormShowType('modals')
                        formComponent.render().then(() => {
                            let eleMap = formComponent.getFormMap();
                            params.pkId=content.uuid;
                            params.pkFieldId='page_parent_uuid';
                            let obj={url:config.getConfigJson().queryOneUrl,params:params}
                            httpUtil.post(obj, (response)=>{
                                let pdfTemplateContent=response.data.data;
                                for (let key in pdfTemplateContent) {
                                    eleMap.get(key).setValue(pdfTemplateContent[key])
                                }
                                eleMap.get('page_parent_uuid').setValue(content['uuid']);
                                eleMap.get('form_code').setValue(content['form_code']);
                                eleMap.get('ds_code').setValue(contents[i].ds_code);
                            })
                            formComponent.formEdit();
                            khcModalForForm.show();
                        });
                        //同理可以实现saveBefore
                        formComponent.setSaveAfter( ()=> {
                            table.refresh()
                            khcModalForForm.hide();
                        })
                    })
                });
            }
            document.getElementById(table.getObjID()+'_form_copy'+content.id).addEventListener('click',  ()=>{
                let params = {};
                params.dsCode='ds_form';
                params.formCode='form_copy';
                khcModalForForm = new khcModal({ modalAreaID: config.getConfigJson().modalAreaID });
                khcModalForForm.render().then(() => {
                    let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params });
                    khcModalForForm.formObjID=formComponent.getObjID()
                    let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
                    pageInfoMap.push(khcModalForFormKey,khcModalForForm);
                    formComponent.setFormShowType('modals')
                    formComponent.render().then(() => {
                        let eleMap = formComponent.getFormMap();
                        for (let key in content) {
                            eleMap.get(key).setValue(content[key])
                        }
                        let fieldList=['id','org_id','uuid','create_time','create_user'];
                        for (let i = 0; i <fieldList.length; i++) {
                            if(eleMap.get(fieldList[i])!=undefined){
                                eleMap.get(fieldList[i]).setValue('');
                            }
                        }
                        eleMap.get('form_code').setValue(content['form_code']+'_copy');
                        formComponent.formEdit();
                        khcModalForForm.show();
                    });
                    //同理可以实现saveBefore
                    formComponent.setSaveAfter( ()=> {
                        params.page_parent_uuid=content.uuid;
                        params.oldCode=content['form_code'];
                        params.newCode=formComponent.formValueArray[0].form_code;
                        params.methodName='copy';
                        let obj={url:config.getConfigJson().myActionUrl,params:params}
                        httpUtil.post(obj, (response)=>{
                            //如果拷贝成功什么都不操作，如果拷贝失败，会被全局异常抓到并提示。
                        })
                        table.refresh()
                        khcModalForForm.hide();
                    })
                })
            })
            document.getElementById(table.getObjID()+'_form_deploy'+content.id).addEventListener('click',  ()=>{
                let data = {};
                data['title'] = '请确认是否部署';//由于版本一直在更新迭代，暂时关闭部署
                data['cancelButtonColor'] = '#181C21E6';
                khcAlert.showConfirm(data, ()=>{
                    let params = {};
                    params.dsCode='ds_form';
                    params.ds_code=contents[i].ds_code;
                    params.form_code=content['form_code'];
                    params.methodName='deploy';
                    let obj={url:config.getConfigJson().myActionUrl,params:params}
                    return httpUtil.post(obj, (response)=>{
                        content['form_version']=response.data.data.version;
                        Swal.fire(
                            '部署成功',
                            '',
                            'success'
                        )
                    })
                });
            })
            document.getElementById(table.getObjID()+'_form_delete'+content.id).addEventListener('click',  ()=>{
                let params = {};
                params.dsCode='ds_form';
                params.queryCode='query_form';
                params.formCode='form';
                params.pkIds = [{ pkId: content.id }]
                khcAlert.showDelConfirm('', ()=> {
                    let obj={url:config.getConfigJson().delUrl,params:params}
                    return httpUtil.post(obj, (response)=>{
                        Swal.fire(
                            '已删除',
                            '删除成功',
                            'success'
                        )
                        table.refresh()
                    })
                })
            })
        }
        document.getElementById(table.getObjID()+'_to_table'+contents[i].id).addEventListener('click',  ()=>{
            let queryInfo={page_parent_uuid:contents[i].uuid};
            let pageCode='page_ds_vm_table';
            let url=config.getIndexUrl()+'?pageCode='+pageCode+'&queryInfo='+JSON.stringify(queryInfo)
            window.open(url);
        })
        if(contents[i]['is_lucene']=='Y'){
            document.getElementById(table.getObjID()+'_to_lucene'+contents[i].id).addEventListener('click',  ()=>{
                let queryInfo={page_parent_uuid:contents[i].uuid};
                let pageCode='page_lucene_index_field';
                let url=config.getIndexUrl()+'?pageCode='+pageCode+'&queryInfo='+JSON.stringify(queryInfo)
                window.open(url);
            })
        }
        document.getElementById(table.getObjID()+'_query'+contents[i].id).addEventListener('click',  ()=>{
            let params = {};
            params.dsCode='ds_query';
            params.formCode='form_query_add';
            khcModalForForm = new khcModal({ modalAreaID: config.getConfigJson().modalAreaID });
            khcModalForForm.render().then(() => {
                let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params });
                khcModalForForm.formObjID=formComponent.getObjID()
                let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
                pageInfoMap.push(khcModalForFormKey,khcModalForForm);
                formComponent.setFormShowType('modals')
                formComponent.render().then(() => {
                    let eleMap = formComponent.getFormMap();
                    eleMap.get('page_parent_uuid').setValue(contents[i].uuid)
                    formComponent.formCreate();
                    khcModalForForm.show();
                });
                //同理可以实现saveBefore
                formComponent.setSaveAfter( ()=> {
                    table.refresh()
                    khcModalForForm.hide();
                })
            })
        })
        document.getElementById(table.getObjID()+'_form'+contents[i].id).addEventListener('click',  ()=>{
            let params = {};
            params.dsCode='ds_form';
            params.formCode='form_add';
            khcModalForForm = new khcModal({ modalAreaID: config.getConfigJson().modalAreaID });
            khcModalForForm.render().then(() => {
                let formComponent = new form({ parentObjID: khcModalForForm.getEleID(), code: params });
                khcModalForForm.formObjID=formComponent.getObjID()
                let khcModalForFormKey='khcModalForForm#'+formComponent.getObjID();
                pageInfoMap.push(khcModalForFormKey,khcModalForForm);
                formComponent.setFormShowType('modals')
                formComponent.render().then(() => {
                    let eleMap = formComponent.getFormMap();
                    eleMap.get('page_parent_uuid').setValue(contents[i].uuid)
                    formComponent.formCreate();
                    khcModalForForm.show();
                });
                //同理可以实现saveBefore
                formComponent.setSaveAfter( ()=> {
                    table.refresh()
                    khcModalForForm.hide();
                })
            })
        })
    }
}
functionMap.set('dsTableRenderAfter', dsTableRenderAfter);// 将函数添加到 Map 中

function getQueryHtml(obj,objID){
    let data=obj.query_data;
    let html=`<label style="padding-left: 5px">查询组件：</label>`
    for (let j = 0; j < data.length; j++) {
        html+=`<div class="ui-feed-icon-container d-inline-block mr-3 mb-3">
                <div class="project-task-actions btn-group" title="${data[j].query_name}">
                  <button type="button" class="btn btn-sm md-btn-flat dropdown-toggle" data-toggle="dropdown">
                    ${data[j].query_code}
                  </button>
                  <div class="dropdown-menu dropdown-menu-right">
                    <a class="dropdown-item" href="javascript:void(0)" id="${objID}_query_edit${data[j].id}">编辑</a>
                    <a class="dropdown-item" href="javascript:void(0)" id="${objID}_query_copy${data[j].id}">复制</a>
                    <a class="dropdown-item" href="javascript:void(0)" id="${objID}_query_to_field${data[j].id}">字段维护</a>
                    <a class="dropdown-item" href="javascript:void(0)" id="${objID}_query_to_button${data[j].id}">按钮维护</a>
                    <a class="dropdown-item" href="javascript:void(0)" id="${objID}_query_to_simple_card${data[j].id}">SimpleCard维护</a>
                    <a class="dropdown-item" href="javascript:void(0)" id="${objID}_query_role${data[j].id}">查看数据全部角色</a>
                    <a class="dropdown-item" href="javascript:void(0)" id="${objID}_query_delete${data[j].id}">删除</a>
                  </div>
                </div>
              </div>`;
    }
    html+=`<a href="javascript:void(0)" id="${objID}_query${obj.id}" class="ticket-assignee-add bg-lighter text-muted mb-3" title="新增查询组件">
              <span class="ion ion-md-add"></span>
            </a>`
    return html;
}
function getFormHtml(obj,objID){
    let data=obj.form_data;
    let html=`<label style="padding-left: 5px">表单组件：</label>`
    for (let j = 0; j < data.length; j++) {
        let bpmButtonHtml='';
        let bpmSubHtml='';//
        if(data[j].is_bpm_model=='Y'){
            bpmSubHtml+='**';
            bpmButtonHtml+=`<a class="dropdown-item" href="javascript:void(0)" id="${objID}_form_edit_bpm_field${data[j].id}">设置工作流变量</a>
                                          <a class="dropdown-item" href="javascript:void(0)" id="${objID}_form_edit_todo_title${data[j].id}">待办 title 生成规则</a>` ;
        }
        html+=`
                                    <div class="ui-feed-icon-container d-inline-block mr-3 mb-3">
                                      <div class="project-task-actions btn-group" title="${data[j].form_name}">
                                        <button type="button" class="btn btn-sm md-btn-flat dropdown-toggle" data-toggle="dropdown">
                                          ${data[j].form_code}${bpmSubHtml}
                                        </button>
                                        <div class="dropdown-menu dropdown-menu-right">
                                          <a class="dropdown-item" href="javascript:void(0)" id="${objID}_form_edit${data[j].id}">编辑</a>
                                          <a class="dropdown-item" href="javascript:void(0)" id="${objID}_form_copy${data[j].id}">复制</a>
                                          <a class="dropdown-item" href="javascript:void(0)" id="${objID}_form_deploy${data[j].id}" style="display: none">部署</a>
                                          <a class="dropdown-item" href="javascript:void(0)" id="${objID}_form_to_field${data[j].id}">字段维护</a>
                                          <a class="dropdown-item" href="javascript:void(0)" id="${objID}_form_to_button${data[j].id}">按钮维护</a>
                                          <a class="dropdown-item" href="javascript:void(0)" id="${objID}_form_edit_pdf_template${data[j].id}">PDF模板维护</a>
                                          <a class="dropdown-item" href="javascript:void(0)" id="${objID}_form_preview_form_info${data[j].id}">预览表单</a>
                                          <a class="dropdown-item" href="javascript:void(0)" id="${objID}_form_copy_json${data[j].id}">导出</a>
                                            ${bpmButtonHtml}
                                          <a class="dropdown-item" href="javascript:void(0)" id="${objID}_form_delete${data[j].id}">删除</a>
                                        </div>
                                      </div>
                                    </div>` ;
    }
    html+=`<a href="javascript:void(0)" id="${objID}_form${obj.id}" class="ticket-assignee-add bg-lighter text-muted mb-3" title="新增表单组件">
                                  <span class="ion ion-md-add"></span>
                                </a>`
    return html;
}
function dsQueryRenderAfter(){
    let jsonConfig=this;
    let query=fullMap.getObj(jsonConfig.objID);
    let queryMap=query.getQueryMap();
    let dsNameObj=queryMap.get('ds_name');
    dsNameObj.callback= (eleID,item)=>{
        document.getElementById(eleID).value=item;
        query.search();
    }
    let dsCodeObj=queryMap.get('ds_code');
    dsCodeObj.callback= (eleID,item)=>{
        document.getElementById(eleID).value=item;
        query.search();
    }
}
functionMap.set('dsQueryRenderAfter', dsQueryRenderAfter);// 将函数添加到 Map 中