"use strict";
import {util} from '../../../util/util.js'
import {httpUtil} from "../../../util/httpUtil.js";
import {verify} from "../../../util/verify.js";
import {config} from "../../../config.js";
class tree{
    constructor(data) {
        this.data=data;
        this.eleID = util.getUUID();
        this.objID = this.eleID + 'div';
        this.type='tree';
        this.dictData=data.dict_code.data;
        this.parentDomID=data.parentDomID;
        this.clickFunc=null;
        this.level=2;
    }
    onClick(clickFunc){
        this.clickFunc=clickFunc;
    }
    openLevel(level){
        if(!util.isNull(level)&&verify['mustInt'](level).isPass=='true'){
            this.level=level;
        }
    }
    getData(){
        return this.data;
    }
    render(){
        this.renderTreeHtml();
    }
    setCheckBoxData(value){
        this.checkboxData=value;
    }
    setDictData(dictData){
        let that=this;
        if(!util.isNull(dictData)){
            that.dictData=dictData;
            that.renderTreeHtml();
        }
    }
    //newDictData 最新最新获取的字典数据
    renderTreeHtml(newDictData){
        let that=this;
        if(newDictData==undefined){
            that.refreshTreeValue().then(()=>{
                return ;
            });
        }else{
            that.dictData=newDictData;
        }
        if(util.isNull(that.dictData)){
            that.dictData=[]
        }
        that.dictData.forEach(item =>{
            item.titleName = item.name
        })
        let setting = {
            data: {
                simpleData: {
                    enable: true
                },key:{
                    title: 'titleName'
                }
            },
            edit: {
                enable: false
            },
            callback: {
                onClick: that.clickFunc
            }
        };
        for (let i = 0; i < that.dictData.length; i++) {
            if (that.dictData[i].level < that.level) {
                that.dictData[i]['open'] = true;
            }
        }
        let zNodes = that.dictData;
        if(that.data.componentType=='form'&&that.data.show_type=='treeCheckBox'){// 类型为form组件 判断表单类型是否为：treeCheckBox
            if(that.data.is_read_only=='N'){
                setting.check={
                    enable: true
                }
                if(true){//暂时默认都禁止勾选，未来可配置是否禁止勾选父节点
                    setting.check.chkboxType= { "Y": "", "N": "" } // 禁止勾选父节点
                }
            }
            if (that.checkboxData != null && that.checkboxData.length > 0) {
                let checkedNodeArray = that.checkboxData.split(',');
                let nodeMap = new Map();
                for (let i = 0; i < checkedNodeArray.length; i++) {
                    if(util.isNull(checkedNodeArray[i]))continue;
                    nodeMap.set(parseInt(checkedNodeArray[i]), checkedNodeArray[i]);
                }
                for (let i = 0; i < zNodes.length; i++) {
                    if (nodeMap.get(parseInt(zNodes[i].id)) != null) {
                        zNodes[i].checked = true;
                    }
                }
            }
        }
        let treeHtmlTemplate=`
        <input type="search" id="${that.parentDomID}SearchKey" placeholder=" 输入检索内容" class="form-control" style="display: none;padding: 10px;">
        <div id="${that.parentDomID}Search" style="overflow:auto;height:calc(70vh);"></div>`;
        document.getElementById(that.parentDomID).innerHTML='';
        document.getElementById(that.parentDomID).insertAdjacentHTML("beforeEnd", treeHtmlTemplate);
        that.zTreeObject = $.fn.zTree.init($("#"+that.parentDomID+'Search'), setting, zNodes)
        if(that.data.componentType=='form'&&that.data.show_type=='treeCheckBox'&&true){// 类型为form组件 判断表单类型是否为：treeCheckBox
            //是否禁用所有父节点
            let nodes = that.zTreeObject.transformToArray(that.zTreeObject.getNodes());
            for (let j=0; j<nodes.length; j++) {
                if (nodes[j].isParent){
                    that.zTreeObject.setChkDisabled(nodes[j], true);
                }
            }
        }
        if(zNodes.length>20)document.getElementById(`${that.parentDomID}SearchKey`).style.display=''
        fuzzySearch(that.parentDomID+'Search','#'+that.parentDomID+'SearchKey',null,true);
    }
    getValue(){
        if(this.zTreeObject==undefined){
            return '';
        }
        if(this.zTreeObject.getSelectedNodes().length>0){//返回当前啊选中的
            return this.zTreeObject.getSelectedNodes()[0].id
        }else if(!util.isNull(this.data.tree_show_parent_id)){//返回默认值
            return this.data.tree_show_parent_id;
        }else{
            return '';//返回空字符串
        }
    }
    setValue(value){
        if(util.isNull(value)){
            this.render();
        }else{
            let nodes = this.zTreeObject.getNodes();
            for (let i=0, l=nodes.length; i < l; i++) {
                if(nodes[i].id==value){
                    this.zTreeObject.selectNode(nodes[i], false, true);
                }
            }
        }
    }
    getSelectedNodes(){
        return this.zTreeObject.getSelectedNodes();
    }
    setSelectedNodes(nodes){
        this.zTreeObject.selectNode(nodes, false, true);
    }
    getCheckedNodes(){
        return this.zTreeObject.getCheckedNodes();
    }
    getRootID(){
        let pIdArray=[];
        for (let i = 0; i < this.dictData.length; i++) {
            pIdArray.push(this.dictData[i].pId)
        }
        let min=Math.min(...pIdArray);
        return min;
    }
    refreshTreeValue(nodes){
        let that=this;
        let treeCode=that.data.dict_code.code;
        if(util.isNull(treeCode)){//如果自定义tree字典，可以将code置为空，再通过setDictData自定义传入即可
            return ;//没有字典直接返回
        }
        let params={treeCode:treeCode,queryParam:{pId:that.data.tree_show_parent_id}}//pId为空则为查询所有
        let obj={url:config.getConfigJson().queryTreeUrl,params:params}
        return httpUtil.post(obj, (response)=>{
            that.renderTreeHtml(response.data.data.treeData);
            if(!util.isNull(nodes)){
                that.setSelectedNodes(nodes[0]);
            }
            if (that.checkboxData != null && that.checkboxData.length > 0) {
                let checkedNodeArray = that.checkboxData.split(',');
                let nodeMap = new Map();
                for (let i = 0; i < checkedNodeArray.length; i++) {
                    if(util.isNull(checkedNodeArray[i]))continue;
                    nodeMap.set(parseInt(checkedNodeArray[i]), checkedNodeArray[i]);
                }
                let nodes = that.zTreeObject.transformToArray(that.zTreeObject.getNodes());
                for (let i=0; i<nodes.length; i++) {
                    if(nodeMap.get(parseInt(nodes[i].id)) != null){
                        this.zTreeObject.selectNode(nodes[i], false, true);//选中
                    }else{
                        this.zTreeObject.checkNode(nodes[i], false, false);//不选中
                    }
                }
            }
        })
    }
    hide(){
    }
    show(){
    }
}
export {tree};