"use strict";
import { fullMap } from '../../../fullMap.js';
import { khcAlert } from '../../../component/alert/khcAlert.js'
import { khcModal } from '../../../component/alert/khcModal.js'
import {util} from "../../../util/util.js";
import {httpUtil} from "../../../util/httpUtil.js";
import {config} from "../../../config.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 preview() {
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let tableViewComponent = queryViewComponent.getTableObj();
    let content = tableViewComponent.getContentMap().get(jsonConfig.pkCode.toString());
    khcModalForForm = new khcModal({ modalAreaID: config.getConfigJson().modalAreaID });
    let showObjId=util.getUUID();
    let khcModalHtml = '';
    khcModalHtml += '<div class="card mb-4">'
    khcModalHtml += '<h6 class="card-header">'
    khcModalHtml += '预览'
    khcModalHtml += '</h6>';
    khcModalHtml += '<div style="padding: 0.1rem;" class="" id="'+showObjId+'">'
    khcModalHtml += '</div></div>';
    khcModalHtml += '<div class="card mb-4">'
    khcModalHtml += '<h6 class="card-header">'
    khcModalHtml += '信息发布'
    khcModalHtml += '</h6>';
    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 buttonHtml = '<div class="demo-inline-spacing mt-3" style="margin-left:20px">';
        buttonHtml += '<button type="button" id="'+showObjId+'returnQuery" style="margin: 0.375rem 0.375rem 0.9375rem 0 !important;" class="btn btn-dark">返回</button>';
        buttonHtml += '</div>';
        document.getElementById(showObjId).insertAdjacentHTML("beforeEnd", buttonHtml);
        let html='<div class="ql-editor">'
        html+= content.content;
        html+='</div>'

        document.getElementById(showObjId+'_preview').insertAdjacentHTML("beforeEnd", html);
        document.getElementById(showObjId+'returnQuery').addEventListener('click',  ()=>{
            khcModalForForm.hide();
        });
        khcModalForForm.show();
    })
}
functionMap.set('preview', preview);// 将函数添加到 Map 中

function publish() {
    let jsonConfig=this;
    let queryViewComponent = fullMap.getObj(jsonConfig.objID);
    let tableViewComponent = queryViewComponent.getTableObj();
    let content = tableViewComponent.getContentMap().get(jsonConfig.pkCode.toString());
    let alertData = {};
    alertData.title = '请确认是否发布';
    alertData.text = '模拟发布（发布后会通过 websocket 广播一次消息）';
    alertData['cancelButtonColor'] = '#181C21E6';
    khcAlert.showConfirm(alertData, ()=>{
        let params =queryViewComponent.getCode();
        params.content=content;
        params.methodName='publish';
        let obj={url:config.getConfigJson().myActionUrl,params:params}
        return httpUtil.post(obj, (response)=>{
            queryViewComponent.getTableObj().refresh();
            Swal.fire(
                '发布成功',
                '模拟发布成功（已使用websocket广播）',
                'success'
            )
        })
    });
}
functionMap.set('publish', publish);// 将函数添加到 Map 中