"use strict";
import {util} from "../../util/util.js";
import {httpUtil} from "../../util/httpUtil.js";
import {fullMap} from "../../fullMap.js";
import {khcToastr} from "../alert/khcToastr.js";
import {config} from "../../config.js";

class htmlView{
    constructor(data) {
        this.eleID = util.getUUID();
        this.objID = util.getUUID() + 'div';
        this.parentObjID = data.parentObjID;
        this.code=data.htmlViewCode.code;
        this.htmlViewCode = data.htmlViewCode;
        this.componentInfo=data.componentInfo
        this.htmlViewHeight = this.code.htmlViewHeight;
        this.option={};
    }
    init(){
        return new Promise((resolve, reject) => {
            let that=this;
            let htmlTemplate=``;
            let col=that.htmlViewCode.code.col;
            let colClass=that.componentInfo['myself_col_class'];
            let myselfStyle=that.componentInfo['myself_style'];
            let heightStyle=''
            if(!util.isNull(that.htmlViewHeight)){
                heightStyle=`height:${that.htmlViewHeight}px;`
            }
            if(util.isNull(colClass)){
                colClass=`col-${!util.isNull(col)?col:'12'} col-xl-${!util.isNull(col)?col:'12'}`;
            }
            htmlTemplate+=`
            <div id="${that.objID}" class="${colClass}" style="${myselfStyle}">
                <div id="${that.objID}_html_view"  style="margin-bottom: 20px;${heightStyle}"></div>
            </div>`;
            document.getElementById(that.parentObjID).insertAdjacentHTML("beforeEnd", htmlTemplate);
            resolve();
        })
    }
    render(){
        let that=this;
        that.init().then(()=>{
            that.refreshSimpleCardValue(0);
        })
    }
    refreshSimpleCardValue(){
        let that=this;
        let params = { version: '1.0.0', dsCode: 'ds_html_view',queryCode:'query_html_view', methodName: 'getHtmlViewInfo' }
        params.htmlViewCode=that.code.htmlViewData.html_view_code;
        let obj={url:config.getConfigJson().myActionUrl,params:params}
        return httpUtil.post(obj, (response)=>{
            that.htmlViewInfo=response.data.data.htmlViewInfo;
            let htmlTemplate=that.htmlViewInfo['html_content'];
            htmlTemplate=util.toOldChar(htmlTemplate);
            htmlTemplate=htmlTemplate.replace('${version}',util.getLocalStorageItem('version'));
            if(!util.isNull(that.htmlViewInfo['data'])){
                let replaceContent={data:that.htmlViewInfo['data']};
                htmlTemplate = util.replaceTemplate(htmlTemplate, replaceContent);
            }
            document.getElementById(that.objID+'_html_view').innerHTML=htmlTemplate;
        })
    }
}
export {htmlView}