var treeObj = null;
var loadColor;

function getMonitorType() {
    $.ajax({
        type: "POST",
        url: "/pqs9000/theme/getThemeInfo",
        async: false,
        data: {},
        dataType: 'json',
        success: function (data) {
            var name = data.body.name + "上报";
            loadColor = data.body.value;
            var option = $("<option value='4'>"
                + name + "</option>");
            $('#datatype').append(option);
        }
    })
}

//*********************左边树内容start
//文档流完毕后，开始获取树结构
$(document).ready(function () {
    getMonitorType();
    //文档流加载完成后，给设备树一个高度，同时给个border边框
    var treeHeight = $(window).height() - 35;
    $("#tree").css("height", treeHeight);
    $("#leftContainer").css("border-right", "solid 1px");
    $("#fileDiv").css("height", treeHeight - 60);
    getTree();
});
//树的基础配置
var setting = {
    check: {
        enable: true,
        chkDisabledInherit: true
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    view: {
        fontCss: setHighlight // 高亮一定要设置，setHighlight是自定义方法
    },
    callback: {
        onClick: zTreeOnClick
    }
};

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

//获取设备树
function getTree() {
    var loadingIndex
    var endTime = getEndTime();
    var startTime = getStartTime("月份", endTime);

    $.ajax({
        url: "/pqs9000/plate/nodeplatetree",
        type: "post",
        async: true,
        dataType: "json",
        beforeSend: function () {
            loadingIndex = ityzl_SHOW_LOAD_LAYER();
        },
        data: {
            startTime: startTime,
            endTime: endTime
        },
        success: function (data) {
            if (data.code == 500 || data.body == null) {
                layer.msg("获取终端树内容失败.");
            } else {
                initTree(data.body);
            }

            ityzl_CLOSE_LOAD_LAYER(loadingIndex);
        }
    });

    // $.ajax({
    //     type: 'POST',
    //     url: '/pqs9000/device/deviceTree',
    //     beforeSend: function () {
    //         loadingIndex = ityzl_SHOW_LOAD_LAYER();
    //     },
    //     error: function () {
    //         //console.log("获取设备树内容失败！");
    //     },
    //     dataType: "json",
    //     success: function (zNodes) {
    //         for (var index = 0; index < zNodes.length; index++) {
    //             if (zNodes[index].nodeType === 1) {
    //                 zNodes[index].icon = "../images/img/base.png";
    //             } else if (zNodes[index].nodeType === 2) {
    //                 zNodes[index].icon = "../images/img/power.png";
    //             } else if (zNodes[index].nodeType === 3) {
    //                 zNodes[index].icon = "../images/img/substation.png";
    //             } else if (zNodes[index].nodeType === 4) {
    //                 zNodes[index].icon = "../images/img/device.png";
    //             } else if (zNodes[index].nodeType === 5) {
    //                 zNodes[index].icon = "../images/img/subvstation.png";
    //             } else if (zNodes[index].nodeType === 6) {
    //                 zNodes[index].icon = "../images/img/gray.png";
    //             }
    //         }
    //         treeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
    //         //展开第一层级
    //         var zTree = $.fn.zTree.getZTreeObj("tree");
    //         var nodes = zTree.getNodes();
    //         zTree.expandNode(nodes[0], true);
    //         checkedTree(zTree);
    //         //***********关闭loading
    //         ityzl_CLOSE_LOAD_LAYER(loadingIndex);
    //     }
    // });
}

// 初始化终端树
function initTree(treeData) {
    if (treeData.length == 0) {
        layer.msg("数据库数据为空,终端树加载失败.");
        return;
    }

    var datalist = [];
    for (var i = 0; i < treeData.length; i++) {
        var str = '';

        str += '{' + "id:" + treeData[i].id;
        str += ',' + "name:'" + treeData[i].name + "'";
        str += ',' + "nodeIndex:" + treeData[i].nodeIndex;
        str += ',' + "nodeType:" + treeData[i].nodeType;
        str += ',' + "ppId:" + treeData[i].ppId;
        if (treeData[i].nodeType == "0") {
            str += ',' + "open:" + true;
        } else {
            str += ',' + "open:" + false;
        }
        str += ',' + "state:" + treeData[i].state;
        str += ',' + "status:" + treeData[i].status;
        str += ',' + "powerType:" + treeData[i].powerType;
        str += ',' + "pttype:" + treeData[i].pttype;
        str += ',' + "pId:" + treeData[i].pId + '}';
        datalist.push(str);
    }

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

    for (var index = 0; index < zNodes.length; index++) {
        if (zNodes[index].nodeType == 0) {
            if (loadColor == "#006565") {
                zNodes[index].icon = "../images/img/gwbase.png";
            } else {
                zNodes[index].icon = "../images/img/nwbase.png";
            }
        } else if (zNodes[index].nodeType == 1) {
            if (loadColor == "#006565") {
                zNodes[index].icon = "../images/img/gwprovince.png";
            } else {
                zNodes[index].icon = "../images/img/nwprovince.png";
            }
        } else if (zNodes[index].nodeType == 2) {
            if (loadColor == "#006565") {
                zNodes[index].icon = "../images/img/gwpower.png";
            } else {
                zNodes[index].icon = "../images/img/nwpower.png";
            }
        } else if (zNodes[index].nodeType == 3) {
            if (loadColor == "#006565") {
                zNodes[index].icon = "../images/img/gwsubstation.png";
            } else {
                zNodes[index].icon = "../images/img/nwsubstation.png";
            }
        } else if (zNodes[index].nodeType == 4) {
            if (zNodes[index].state == 2) {
                zNodes[index].icon = "../images/img/gray.png";
            } else if (zNodes[index].state == 1) {
                zNodes[index].icon = "../images/img/yellow.png";
            } else {
                if (zNodes[index].status == 0) {
                    zNodes[index].icon = "../images/img/red.png";
                } else {
                    zNodes[index].icon = "../images/img/green.png";
                }
            }
        }
    }

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

    //展开第一层级
    var zTree = $.fn.zTree.getZTreeObj("tree");
    var nodes = zTree.getNodes();
    zTree.expandNode(nodes[0], true);
    checkedTree(zTree);
}

//默认选中某个节点
function checkedTree(zTree) {
    if (zTree != null) {
        var nodes = zTree.transformToArray(zTree.getNodes());
        for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].name === "测试1") {
                var devName = nodes[i].getParentNode().getParentNode();
                var subvName = nodes[i].getParentNode();
                $("#lineIndex").val(nodes[i].nodeIndex);
                $("#monitorInfo").text(devName.name + '->' + subvName.name + '->' + nodes[i].name);
                //高亮
                zTree.expandNode(nodes[i].getParentNode().getParentNode().getParentNode().getParentNode(), true);
                zTree.expandNode(nodes[i].getParentNode().getParentNode().getParentNode(), true);
                zTree.expandNode(nodes[i].getParentNode().getParentNode(), true);
                zTree.expandNode(nodes[i].getParentNode(), true);
                zTree.expandNode(nodes[i], true);
                nodes[i].highlight = true;
                zTree.updateNode(nodes[i]); // 更新节点，让高亮生效
            }
        }
        // 高亮并展开搜索到的节点
        // for (var i = 0; i < nodes.length; i++) {
        //     nodes[i].highlight = true;
        //     treeObj.updateNode(nodes[i]); // 更新节点，让高亮生效
        // }

    }
}

//树节点点击效果
function zTreeOnClick() {
    var zTree = $.fn.zTree.getZTreeObj("tree");
    if (zTree != null) {
        var nodes = zTree.getSelectedNodes();   //获取树控件选中对象集合
        // 关闭所有节点的高亮
        var allNodes = treeObj.transformToArray(treeObj.getNodes());
        for (var i = 0; i < allNodes.length; i++) {
            allNodes[i].highlight = false;
            treeObj.updateNode(allNodes[i]);
        }
        // 高亮并展开搜索到的节点
        for (var i = 0; i < nodes.length; i++) {
            nodes[i].highlight = true;
            treeObj.updateNode(nodes[i]); // 更新节点，让高亮生效
        }
        if (nodes[0].level === 4) {    //如果选中监测点信息则进行处理，其他信息不处理
            var devName = nodes[0].getParentNode().getParentNode();
            var subvName = nodes[0].getParentNode();
            $("#lineIndex").val(nodes[0].nodeIndex);
            $("#monitorInfo").text(devName.name + '->' + subvName.name + '->' + nodes[0].name);
        }
    }
}

//搜索框内值变化触发函数
function valueChange(value) {
    if (value == '') {
        closeHigh();
    }
}

// 关闭所有高亮节点
function closeHigh() {
    var treeObj = $.fn.zTree.getZTreeObj("tree");
    var allNodes = treeObj.transformToArray(treeObj.getNodes());// 关闭所有节点的高亮

    for (var i = 0; i < allNodes.length; i++) {
        allNodes[i].highlight = false;
        treeObj.updateNode(allNodes[i]);
    }
}

// 模糊搜索name满足条件的节点
function searchNode(value) {
    if (value === "")
        return;
    var treeObj = $.fn.zTree.getZTreeObj("tree");
    var nodes = treeObj.getNodesByParamFuzzy("name", value);// 获取搜索的节点
    closeHigh();
    treeObj.expandAll(false);// 展开第一层节点
    treeObj.expandNode(treeObj.getNodes()[0], true);
    // 高亮并展开搜索到的节点
    for (var i = 0; i < nodes.length; i++) {
        nodes[i].highlight = true;
        treeObj.updateNode(nodes[i]); // 更新节点，让高亮生效
        treeObj.expandNode(nodes[i].getParentNode(), true);
    }
}

//*********************左边树内容end