/*********************************************************************************************************************
 * zTree操作deviceTree代码段
 *********************************************************************************************************************/
//zTree属性配置
var
    setting = {
        check: {
            enable: true,
            nocheckInherit: true,
            chkboxType: {"Y": "ps", "N": "s"}
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        view: {
            fontCss: setHighlight // 高亮一定要设置，setHighlight是自定义方法
        },
        callback: {
            beforeClick: pointSelected,
            onClick: pointSelected
        }
    };

function setTreeDataSelect(arr) {
    var zTree = $.fn.zTree.getZTreeObj("tree");
    var nodes = zTree.transformToArray(zTree.getNodes());

    for (var i = 0; i < nodes.length; i++) {
        for (var j = 0; j < arr.length; j++) {
            if (nodes[i].id === arr[j]) {
                zTree.checkNode(nodes[i], true, false, false);
                break;
            }
        }

        if (nodes[i].id == "0") {
            zTree.checkNode(nodes[i], true, false, false);
        }
    }
}

function pointSelected() {
    var zTree = $.fn.zTree.getZTreeObj("tree");
    var nodes = zTree.getSelectedNodes();
    var arrayNodes = zTree.transformToArray(nodes);

    if (0 === nodes.length) {
        return;
    }

    /*//获取每个节点的id添加空格作为表格的查询条件筛选数据
    try {
        if (typeof(valueChange) === "function") {
            arrayNodes[0].rdIndex == "0" ? valueChange("parentNull") : valueChange(arrayNodes[0].rdIndex);
        }
    } catch (e) {
        console.error('function test exception.');
    }*/

    //选中字体高亮
    nodes[0].highlight = !nodes[0].highlight;
    zTree.updateNode(nodes[0]);
}

function getAllSelected() {
    var zTree = $.fn.zTree.getZTreeObj("tree");
    return zTree.getCheckedNodes(true);
}

// 设置高亮字体
function setHighlight(treeId, treeNode) {
    return (!!treeNode.highlight) ? {color: "#000000", "font-weight": "bold"} : {
        color: "#666",
        "font-weight": "normal"
    };
}

/*********************************
 * 是否显示checkBox
 * type-1:显示
 * type-2:不显示
 *********************************/
function triggerCheckBox(type) {
    var zTree = $.fn.zTree.getZTreeObj("tree");
    var nodes = zTree.transformToArray(zTree.getNodes());
    var isShow = false;

    if (type == 2) {
        isShow = true;
    }

    for (var i = 0; i < nodes.length; i++) {
        nodes[i].nocheck = isShow;
        zTree.updateNode(nodes[i]);
    }
}

function initDiv() {
    document.getElementById("father").style.height = document.body.offsetHeight - 4 + "px"; //减去border的宽度
    document.getElementById("tree").style.height = document.getElementById("father").offsetHeight - 2 + "px"; //减去border的宽度
}

$(function () {
    initDiv();
    getTreeData();
});

//数据库获取数据
function getTreeData() {
    $.ajax({
        url: "/pqs9900/sysset/menuList",
        type: "post",
        async: false,
        dataType: "json",
        success: function (data) {
            initTree(data.body);
            triggerCheckBox(2);

            try {
                if (typeof(setTableData) === "function") {
                    setTableData(data.body);
                }
            } catch (e) {
                console.error('function test exception.');
            }
        },
        error: function () {
            layer.msg("获取资源树基础配置失败.");
        }
    });
}

//初始化终端树
function initTree(treeData) {
    if (treeData == null || treeData.length == 0) {
        layer.msg("数据库数据为空,资源树基础配置载失败.");
        return;
    }

    var datalist = [];
    for (var i = 0; i < treeData.length; i++) {
        var str = '';
        str += '{id:"' + treeData[i].guid + '"';
        str += ',name:"' + treeData[i].name + '"';
        str += ',pId:"' + treeData[i].parentNode + '"}';
        datalist.push(str);
    }

    datalist = eval('[' + datalist + ']');// string->json数组
    var zNodes = datalist;

    var treeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
    treeObj.expandAll(true);
}