"use strict";
import { util } from "../../util/util.js";
import {fullMap} from "../../fullMap.js";
import {khcToastr} from "./khcToastr.js";
class khcModal {
    constructor(data) {
        this.eleID = util.getUUID();//table的domid
        this.modalArea=data.modalAreaID;
        this.modalWidth=data.modalWidth;
        this.objID = this.eleID + 'div';//本组件的根Div的domid
        this.isDestroy= false;//isDestroy 是否remove
        this.formObjID=null;
        this.modalHideType=null;
        this.isShowModal=true;//如果禁用模态框区域外关闭时此字段才会生效（用于标识关闭来源）
        //document.getElementById(this.modalArea).innerHTML='';
    }
    render() {
        let that=this;
        return new Promise((resolve, reject) => {
            let modalWidthStyle=`${!util.isNull(that.modalWidth)?`width:${that.modalWidth}px;max-width:${that.modalWidth}px;`:''}`;
            let htmlTemplate=`
            <div class="modal fade" id="${that.objID}" style="display: none;" aria-hidden="true">
            <div class="modal-dialog modal-lg" style="${modalWidthStyle}">
            <form class="modal-content">
            <div id="${that.eleID}" class="modal-body" style="min-height: 300px;"></div>
            <div id="${that.eleID}_footer" class="card"  style="display: none;"></div>
            </form></div></div>`;
            document.getElementById(that.modalArea).insertAdjacentHTML('beforeEnd', htmlTemplate);
            resolve();
        })
    }

    /**
     * modal_hide_type ==1 不禁用 模态框外点击会处发关闭模态框
     * modal_hide_type ==2 禁用 模态框外点击会处发关闭模态框
     */
    show(){
        let that=this;
        $('#'+that.objID).modal('show');
        if(that.formObjID!=null){
            that.modalHideType=fullMap.getObj(that.formObjID).formStructureData.formInfo.modal_hide_type;
            if(that.modalHideType=='2'){
                $('#'+that.objID).data('bs.modal')._config.backdrop = 'static';//禁用模态框外的关闭，只能点返回按钮关闭模态框
            }
        }
        let modalObj=$('#'+that.objID).data('bs.modal');
        $(modalObj._element).on('click.dismiss.bs.modal', (event) => {
            //官方的解释是：避免事件冒泡触发此函数(这里由于是包含关系，所以必须避免这种情况)
            if (event.target !== event.currentTarget) {
                return
            }
            if(that.hideAfterFunc!=undefined){
                that.hideAfterFunc();
            }
            if(that.modalHideType=='2'&&that.isShowModal){
                khcToastr.showErrorMessage('该表单只能通过返回按钮关闭')
            }else{
                if(that.parentModal!=null&&that.parentModal!=undefined){
                    that.parentModal.show();
                }
            }
        })
    }
    hide(){
        let that=this;
        if(that.modalHideType=='2'){
            that.isShowModal=false;//标识关闭来源是通过返回按钮来的
        }
        if(that.isDestroy){
            document.getElementById(that.objID).remove();
        }
        if(that.parentModal!=null&&that.parentModal!=undefined){
            that.parentModal.show();
            setTimeout(function (){
                $('#'+that.objID).modal('hide');
            },200)
        }else{
            $('#'+that.objID).modal('hide');
        }
        if(that.hideAfterFunc!=undefined){
            that.hideAfterFunc();
        }
    }
    getEleID(){
        return this.eleID;
    }
   
}
export { khcModal }
