var dataTable = null;
var treeObj = null;
var setting = {
    check: {
        enable: true,
        chkDisabledInherit: true
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    view: {
        fontCss: setHighlight // 高亮一定要设置，setHighlight是自定义方法
    },
    callback: {
        beforeCheck: beforeCheck,
        onClick: zTreeOnClick
    }
};
var getshows,getServerDetails,sename;
var nowIndex,sevname;
// tree节点选中之前进行判断
var className = "dark";

function beforeCheck(treeId, treeNode) {
    className = (className === "dark" ? "" : "dark");
    return (treeNode.doCheck !== false);
}

var typeId = "";

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]); // 更新节点，让高亮生效
        }

        nowIndex = nodes[0].rd_Index;
        sevname = nodes[0].name;
        if(typeId!="0"){
            var startTime=$("#startTime").val();
            var endTime=$("#endTime").val();
            getServerDetail(nowIndex,sevname,startTime,endTime);
            getServerDetailss(getServerDetails);
            getShow(getshows)
        }
    }
}

function getname() {
    return typeId;
}

function getTree() {
    jQuery.ajax({
        type: 'POST',
        dataType: "json",
        url: '/pqs9900/serverstate/getTree',
        data: {},
        async: false,
        error: function () {
            console.log("获取终端树请求发送失败！");
        },
        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 += ',' + "nodeType:" + data[i].nodeType;
                str += ',' + "ppId:" + data[i].ppId;
                if (data[i].nodeType == "0") {
                    str += ',' + "open:" + true;
                } else {
                    str += ',' + "open:" + false;
                }
                str += ',' + "describe:" + data[i].describe;
                str += ',' + "rd_Index:'" + data[i].rdIndex + "'";
                str += ',' + "pId:" + data[i].pId + '}';
                datalist.push(str);
            }
            datalist = eval('[' + datalist + ']');// string->json数组

            var zNodes = datalist;

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


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

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

    var nodes = zTree.getNodes();
    zTree.expandNode(nodes[0], true);

    var childNodes = zTree.transformToArray(nodes[0]);


        var i = 0;
        for (i = 0; i < childNodes.length; i++) {
            var type = childNodes[i].level;
            if (type == 1) {

                var subvname = childNodes[i].nodeType;
                zTree.expandNode(subvname, true);
                zTree.expandNode(childNodes[i], true);

                var sevIndex = childNodes[i].rd_Index;
                sevname=childNodes[i].name;
                nowIndex = sevIndex;
                // 关闭所有节点的高亮
                var allNodes = treeObj.transformToArray(treeObj.getNodes());
                for (var n = 0; n < allNodes.length; n++) {
                    allNodes[n].highlight = false;
                    treeObj.updateNode(allNodes[n]);
                }

                // 高亮并展开搜索到的节点
                childNodes[i].highlight = true;
                treeObj.updateNode(childNodes[i]); // 更新节点，让高亮生效

                break;
            }
        }
    var startTime=$("#startTime").val();
    var endTime=$("#endTime").val();
     getServerDetail(nowIndex,sevname,startTime,endTime);
}

function getServerDetail(sevIndex,sevname,startTime,endTime) {
    $.ajax({
        type: "POST",
        url: "/pqs9900/serverstate/getShow",
        data: {
            sevIndex:sevIndex,
            startTime:startTime,
            endTime:endTime
        },
        async: false,
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        success: function (data) {
            getshows=data;
            sename=sevname;
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        }
    })
    $.ajax({
        type: "POST",
        url: "/pqs9900/serverstate/getServerDetail",
        data: {
            sevIndex:sevIndex
        },
        async: false,
        success: function (data) {
            getServerDetails=data.body;
        }
    })
}