"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 simpleCard{
    constructor(data) {
        this.eleID = util.getUUID();
        this.objID = util.getUUID() + 'div';
        this.parentObjID = data.parentObjID;
        this.code=data.simpleCardCode.code;
        this.simpleCardCode = data.simpleCardCode;
        this.option={};
    }
    init(){
        return new Promise((resolve, reject) => {
            let that=this;
            that.componentMap = new Map();//存储组件信息
            let htmlTemplate=``;
            let col=that.simpleCardCode.code.col;
            let titleFontSize=util.getFontSizeByLevel(7);
            htmlTemplate+=`
            <div id="${that.objID}" 
            class="col-${!util.isNull(col)?col*2:'12'} col-xl-${!util.isNull(col)?col:'12'}">
            <div class="card" style="margin-bottom: 20px;">
                <div class="card-body pt-3 pb-3">
                    <div class="d-flex align-items-center">
                        <div id="${this.objID}_icon_div" style="margin-top: 8px;"></div>
                        <div class="ml-3">
                            <div class="text-muted small" id="${this.objID}_title" style="font-size: ${titleFontSize}"></div>
                            <div class="text-large">
                                <span id="${this.objID}_value" style="font-size: 26px;font-weight: 600;"></span>
                                <span id="${this.objID}_unit" style="font-size: 16px;color: #a3a4a6;"></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            </div>`;
            document.getElementById(that.parentObjID).insertAdjacentHTML("beforeEnd", htmlTemplate);
            resolve();
        })
    }
    render(){
        let that=this;
        that.init().then(()=>{
            that.refreshSimpleCardValue(0);
        })
    }
    setValue(value){
        let that=this;
        document.getElementById(that.objID+'_value').innerText=value;
    }
    setTitle(title){
        let that=this;
        document.getElementById(that.objID+'_title').innerText=title;
    }
    setIcon(icon){
        let that=this;
        let iconHtml=`<div class="${icon} display-4" id="${this.objID}_icon"></div>`;
        document.getElementById(that.objID+'_icon_div').innerHTML=iconHtml;
    }
    setIconColor(color){
        let that=this;
        if(util.isNull(color)){
            //未设置颜色
        }else{
            color=util.getColorByName(color);
            if(color){
                document.getElementById(that.objID+'_icon').style.color=color;
            }else{
                document.getElementById(that.objID+'_icon').style.color=color;
            }
        }
    }
    setValueColor(color){
        let that=this;
        if(util.isNull(color)){
            //未设置颜色
        }else{
            color=util.getColorByName(color);
            if(color){
                document.getElementById(that.objID+'_value').style.color=color;
            }else{
                document.getElementById(that.objID+'_value').style.color=color;
            }
        }
    }
    refreshSimpleCardValue(value){
        if(util.isNull(value)){
            value='';
        }
        let that=this;
        let params = { version: '1.0.0', dsCode: 'ds_simple_card',queryCode:'query_simple_card', methodName: 'getSimpleCardInfo',value:value }
        params.simpleCardCode=that.code.simpleCardData.simple_card_code;
        let obj={url:config.getConfigJson().myActionUrl,params:params}
        return httpUtil.post(obj, (response)=>{
            that.simpleCardInfo=response.data.data.simpleCardInfo;
            if(!util.isNull(that.simpleCardInfo['simple_card_value_unit'])){
                document.getElementById(that.objID+'_unit').innerText=that.simpleCardInfo['simple_card_value_unit'];
            }
            that.setTitle(that.simpleCardInfo.simple_card_title)
            that.setIcon(that.simpleCardInfo.simple_card_icon)
            that.setIconColor(that.simpleCardInfo.simple_card_icon_color)
            that.setValueColor(that.simpleCardInfo.simple_card_value_color)
            let dataType=that.simpleCardInfo.simple_card_data_type;
            if(dataType!=''&&dataType=='1'){//静态JSON
                let data=util.toOldChar(that.simpleCardInfo.simple_card_json)
                let dataObj=JSON.parse(data);
                that.setValue(dataObj.data);
            }
            if(dataType=='2'){//sql
                that.setValue(that.simpleCardInfo.data);
            }
            if(dataType=='3'){//扩展类
                that.setValue(that.simpleCardInfo.data);
            }
        })
    }
}
export {simpleCard}