"use strict";
import { base } from './base.js'
import { util } from '../../util/util.js'
import { tree } from '../page/query/tree.js'
import {khcModal} from '../alert/khcModal.js'
import {khcToastr} from "../alert/khcToastr.js";
class inputTreeCheckBox extends base {
    constructor(data) {
        super(data);
        this.type = 'treeCheckBox';

    }
    render(parentObjID) {
        let that=this;
        that.initFunc();
        that.renderHtml(parentObjID).then(()=>{
            that.renderTree();
        })
    }
    renderHtml(parentObjID) {
        return new Promise((resolve, reject) => {
            let that=this;
            let data = that.data
            let dictDataArray=that.data.dict_code.data;
            if(util.isNull(dictDataArray)){
                dictDataArray=[];
            }
            for(let i=0;i<dictDataArray.length;i++){
                that.dictMap.set(dictDataArray[i].id.toString(),dictDataArray[i].name);
            }
            let labelName = data.label_name;
            let eleName = that.data.name;
            let placeholder = data.placeholder;
            let defaultValue = data.default_value;
            let canNull = data.can_null;
            that.initFormCol(that);
            let questionCircleHtml=that.getQuestionCircleIconHtml();
            let htmlTemplate=`
            <div class="form-group row col-sm-${data.col}"  id="${that.objID}" style="display: none">
            <label class="col-form-label col-sm-${that.labelCol} text-sm-right" style="line-height:25px;vertical-align: middle;">
            <font id="${that.objID}CanNull" style="font-size: 22px;vertical-align: middle;${canNull!='Y'?'display:none;':''}" color="red">*</font>
            ${labelName}</label>
            <div class="col-sm-${that.inputCol}">
                <div class="input-group-append">
                    <div id="${that.eleID}_show_tree_div" style="width: 100%;"></div>
                </div>
                <small class="form-text text-muted"><font style="display:none;"  validate="validate"  color="red">12412345123</font></small>
            </div></div>`;
            document.getElementById(parentObjID).insertAdjacentHTML("beforeEnd", htmlTemplate);
            that.bindFunc();
            resolve();
        })
    }
    initFunc(){
        let that=this;
        that.formBindFunc= ()=>{
            that.renderTree();
        }
        that.bindInputButtonFunc=()=>{
            that.inputButtonFunc();
        }
        that.inputButtonFunc=()=>{//较复杂的代码逻辑请重写此方法
            let myselfFuncStr=util.toOldChar(that.data.input_button_func);
            util.executeDynamicCode(myselfFuncStr,{eleObj:that});
        }
    }
    bindFunc(){
        let that=this;
        if(that.data.is_show_input_button=='Y'){
            document.getElementById(that.eleID+'_input_button').addEventListener('click',that.bindInputButtonFunc);
        }
    }
    renderTree() {
        let that=this;
        if(that.data.is_read_only=='N'){
            let treeData=util.clone(that.data);
            let parentDomID=that.eleID+'_show_tree_div'
            treeData['parentDomID']=parentDomID
            document.getElementById(parentDomID).setAttribute('class','ztree');
            that.treeView = new tree(treeData);
            that.treeView.openLevel(that.data.tree_open_level)
            that.treeView.render();
        }
    }
    getValue(){
        let that=this;
        if(that.treeView==undefined){
            return '';
        }
        let checkNodes=that.treeView.getCheckedNodes();
        let idValue = '';
        if (checkNodes.length > 0) {
            for (let i = 0; i < checkNodes.length; i++) {
                if (i < (checkNodes.length - 1)) {
                    idValue += checkNodes[i].id + ",";
                } else {
                    idValue += checkNodes[i].id;
                }
            }
            idValue=","+idValue
        }
        return idValue;
    }
    setValue(value) {
        let that=this;
        if(that.treeView!=undefined){
            that.treeView.setCheckBoxData(value);
            that.treeView.refreshTreeValue();
        }
    }
    hide(){
        document.getElementById(this.objID).setAttribute('style', 'display:none');//隐藏
    }
    show(){
        document.getElementById(this.objID).setAttribute('style', '');//展示
    }
}
export { inputTreeCheckBox }