var treeObj = null;
var setting = {
    check: {
        enable: true,
        chkDisabledInherit: true
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    view: {
        fontCss: setHighlight // 高亮一定要设置，setHighlight是自定义方法
    },
    callback: {
        onClick: zTreeOnClick
    }
};
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 === 3) {   //变电站
            showSubDiv(nodes[0].nodeIndex);
        } else if (nodes[0].level === 4) {    //终端
            showDevDiv(nodes[0].nodeIndex);
        } else if (nodes[0].level === 6) {    //线路
            showLineDiv(nodes[0].nodeIndexStr,nodes[0].nodeIndex);
        }
    }
}

/**
 * 根据点击显示变电站内容
 */
function showSubDiv(subIndex) {
    $("#subDiv").css("display","block");
    $("#devDiv").css("display","none");
    $("#lineDiv").css("display","none");
    $("#subDiv").css("margin-top",($(window).height()-$("#subDiv").height())/2);
    loadSub(subIndex);
}

/**
 * 根据点击显示终端内容
 */
function showDevDiv(devIndex) {
    $("#devDiv").css("display","block");
    $("#lineDiv").css("display","none");
    $("#subDiv").css("display","none");
    $("#devDiv").css("margin-top",($(window).height()-$("#devDiv").height())/2);
    loadDevice(devIndex);
}


/**
 * 根据点击显示监测点内容
 */
function showLineDiv(lineIndex,monitorID) {
    $("#lineDiv").css("display","block");
    $("#devDiv").css("display","none");
    $("#subDiv").css("display","none");
    $("#lineDiv").css("margin-top",($(window).height()-$("#lineDiv").height())/2);
    loadLine(lineIndex,monitorID);
}



function setHighlight(treeId, treeNode) {
    return (treeNode.highlight) ? {
        color: "#000000",
        "font-weight": "bold",
        "border-bottom": "dotted 1px #fff"
    } : {color: "#666", "font-weight": "normal"};
}

function getTree() {
    jQuery.ajax({
        type: 'POST',
        dataType: "json",
        url: '/pqs9900/device/gwDeviceTree',
        data: {},
        async: false,
        error: function () {},
        success: function (data) {
            var datalist = [];
            for (var i = 0; i < data.length; i++) {
                var str = '';
                str += '{' + "id:" + data[i].id;
                str += ',' + "name:'" + data[i].name + "'";
                str += ',' + "nodeIndex:" + data[i].nodeIndex;
                str += ',' + "nodeIndexStr:" + data[i].nodeIndexStr;
                str += ',' + "nodeType:" + data[i].nodeType;
                str += ',' + "ppId:" + data[i].ppId;
                if (data[i].nodeType == "0" || data[i].nodeType == "1" || data[i].nodeType == "2") {
                    str += ',' + "open:" + true;
                } else {
                    str += ',' + "open:" + false;
                }
                str += ',' + "state:" + data[i].state;
                str += ',' + "pId:" + data[i].pId + '}';
                datalist.push(str);
            }
            datalist = eval('[' + datalist + ']');// string->json数组
            var zNodes = datalist;
            treeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
        }
    });
}

$(document).ready(function () {
    getTree();
});


function loadSub(subIndex) {
    jQuery.ajax({
        type: 'POST',
        dataType: "json",
        url: '/pqs9900/device/loadgwSubstation',
        data: {
            subIndex: subIndex
        },
        success: function (data) {}
    })
}


function loadDevice(devIndex) {
    jQuery.ajax({
        type: 'POST',
        dataType: "json",
        url: '/pqs9900/device/loadgwDevice',
        data: {
            devIndex: devIndex
        },
        success: function (data) {}
    })
}


function loadLine(lineIndex,monitorID) {

    jQuery.ajax({
        type: 'POST',
        dataType: "json",
        url: '/pqs9900/device/loadgwLine',
        data: {
            lineIndex: lineIndex,
            monitorID:monitorID
        },
        success: function (data) {}
    })
}


//模糊搜索name满足条件的节点
function searchNode(value) {
    if (value == "")
        return;
    // 获取搜索的节点
    var nodes = treeObj.getNodesByParamFuzzy("name", value);
    // 关闭所有节点的高亮
    var allNodes = treeObj.transformToArray(treeObj.getNodes());
    for (var i = 0; i < allNodes.length; i++) {
        allNodes[i].highlight = false;
        treeObj.updateNode(allNodes[i]);
    }
    // 展开第一层节点
    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);
    }
}