"use strict";
import {config} from "../../../config.js";
import {util} from "../../../util/util.js";
import {httpUtil} from "../../../util/httpUtil.js";
import {khcModalSlide} from "../../alert/khcModalSlide.js";
import {form} from "../../page/form/form.js";
import {pageInfoMap} from "../../../pageInfoMap.js";
import {khcAlert} from "../../alert/khcAlert.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 initSystemNotify(){
    let jsonConfig=this;
    let params = { version: '1.0.0', dsCode: 'ds_system_notify',queryCode:'query_system_notify', methodName: 'getSystemNotifyList'}
    let obj={url:config.getConfigJson().myActionUrl,params:params}
    httpUtil.post(obj, (response)=>{
        let myData=response.data.data.data;
        /*let myData=[
            {uuid:'u1',subject:'系统通知',content:'关于表单类型新增页面选项卡（tabs）类型通知',send_time:'2023-06-02 22:35:11',system_notify_priority:'红色'},
            {uuid:'u2',subject:'系统通知',content:'菜单调整：用户和部门放到组织机构下',send_time:'2023-06-01 14:02:15',system_notify_priority:'黄色'},
            {uuid:'u3',subject:'系统通知',content:'菜单调整：菜单和角色放到权限管控下',send_time:'2023-05-30 12:01:15',system_notify_priority:'绿色'},
            {uuid:'u4',subject:'系统通知',content:'菜单调整：首页调整',send_time:'2023-05-28 11:02:11',system_notify_priority:'灰色'}]*/
        let dataTemplate=``;
        if(myData.length>0){
            for (let j = 0; j < myData.length; j++) {
                let iconColor='';
                if(myData[j].system_notify_priority=='1')iconColor=util.getColorByName('红色');
                if(myData[j].system_notify_priority=='2')iconColor=util.getColorByName('黄色');
                if(myData[j].system_notify_priority=='3')iconColor=util.getColorByName('绿色');
                if(myData[j].system_notify_priority=='4')iconColor=util.getColorByName('灰色');
                dataTemplate+=`
                <a href="javascript:void(0)" class="list-group-item list-group-item-action media d-flex align-items-center" id="${jsonConfig.objID}${myData[j].uuid}">
                <div class="ui-icon ui-icon-sm ion ion-md-notifications-outline bg- border-0 text-white" style="background-color: ${iconColor}"></div>
                <div class="media-body line-height-condenced ml-3" title="${myData[j].content}">
                <div class="text-primary">${myData[j].subject}</div>
                <div class="text-light small mt-1">${util.simpleDuration(myData[j].send_time)}</div>
                </div>
                </a>
                `;
            }
        }else{
            dataTemplate+=`<div class="d-block text-center text-light small p-2 my-1" _mstvisible="3">
                <div class="ion ion-md-notifications-outline d-block display-4"></div>
                <div class="">没有未读通知</div>
            </div>`;
        }

        if(myData.length>0){
            let color=util.getColorByName('红色');
            document.getElementById(jsonConfig.iconData.div_id+'IndicatorColor').style.backgroundColor=color;
        }else{
            document.getElementById(jsonConfig.iconData.div_id+'IndicatorColor').style.backgroundColor='';
        }
        document.getElementById(jsonConfig.iconData.div_id).innerHTML='';
        document.getElementById(jsonConfig.iconData.div_id).insertAdjacentHTML("beforeEnd", dataTemplate);

        for (let j = 0; j < myData.length; j++) {
            document.getElementById(jsonConfig.objID+myData[j].uuid).addEventListener('click',function (){
                let params = {};
                params.dsCode='ds_system_notify';
                params.formCode='form_system_notify_info';
                params.queryCode='query_system_notify';
                let khcModalForForm = new khcModalSlide({ 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.initSystemNotifyJsonConfig=util.clone(jsonConfig);
                    formComponent.render().then(() => {
                        formComponent.renderValue(myData[j]).then(()=>{
                            formComponent.formEdit();
                            khcModalForForm.show();
                        });
                    });
                    //同理可以实现saveBefore
                    formComponent.setSaveAfter(()=> {
                        khcModalForForm.hide();
                    })
                })
            })
        }
    })

}
functionMap.set('initSystemNotify', initSystemNotify);// 将函数添加到 Map 中