var path = window.location.href;// 获取界面路径
/*******************************************************************************
 * zTree操作deviceTree代码段
 ******************************************************************************/
// zTree属性配置
var setting = {};
var treeAllData;

function addDiyDom(treeId, treeNode) {
	if (treeNode.level == 0) {
		return;
	}
	if (treeNode.qualityOff == 1) {
		return;
	}
	var quality = treeNode.quality;
	if (quality == 0) {
		var aObj = $("#" + treeNode.tId + "_a");
		var editStr = "<input type='image' src='../images/img/none.png'></input>";
		aObj.before(editStr);
		treeNode.qualityOff = 1;
	} else if (quality == 1) {
		var aObj = $("#" + treeNode.tId + "_a");
		var editStr = "<input type='image' src='../images/img/bad.png'></input>";
		aObj.before(editStr);
		treeNode.qualityOff = 1;
	} else if (quality == 2) {
		var aObj = $("#" + treeNode.tId + "_a");
		var editStr = "<input type='image' src='../images/img/medium.png'></input>";
		aObj.before(editStr);
		treeNode.qualityOff = 1;
	} else if (quality == 3) {
		var aObj = $("#" + treeNode.tId + "_a");
		var editStr = "<input type='image' src='../images/img/good.png'></input>";
		aObj.before(editStr);
		treeNode.qualityOff = 1;
	} else if (quality == 4) {
		var aObj = $("#" + treeNode.tId + "_a");
		var editStr = "<input type='image' src='../images/img/excellent.png'></input>";
		aObj.before(editStr);
		treeNode.qualityOff = 1;
	}
}
function pointSelected() {
	var zTree = $.fn.zTree.getZTreeObj("tree");
	var nodes = zTree.getSelectedNodes();

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

	closeHigh();

	if (nodes[0].level == 4 && path.indexOf("monitor") == -1) {
		nodes[0].highlight = !nodes[0].highlight;
		zTree.updateNode(nodes[0]);

		var line_index = nodes[0].nodeIndex;
		$("#lineIndex").val(line_index);
		$("#lineName").val(nodes[0].name);
	}else if (path.indexOf("monitor") != -1 && nodes[0].level == 4) {
		nodes[0].highlight = !nodes[0].highlight;
		zTree.updateNode(nodes[0]);

		// 修改选中监测点的信息展示
		var devname = nodes[0].getParentNode();
		var bdname = nodes[0].getParentNode().getParentNode();
		var gdname = nodes[0].getParentNode().getParentNode().getParentNode();

		$('#pointInfo').text(
				'所选监测点:' + gdname.name + '->' + bdname.name + '->' + devname.name + '->'
						+ nodes[0].name);

		var line_index = nodes[0].nodeIndex;
		$("#lineIndex").val(line_index);
		$("#lineName").val(nodes[0].name);

		var status = nodes[0].status;
		var state = nodes[0].state;
		if (state == 2) {
			$("#image").val("gray");
		} else if (state == 1) {
			$("#image").val("yellow");
		} else {
			if (status == 1) {
				$("#image").val("green");
			} else {
				$("#image").val("red");
			}
		}

		var endTime = getEndTime();
		var startTime = getStartTime("年份", endTime);
		var middlesrc = $("#middlesrc").val();
		if (middlesrc == "yxzt") {
			document.getElementById("main_right").src = "yxzt";
		} else if (middlesrc == "wtzb") {
			document.getElementById("main_right").src = "wtzb";
		} else if (middlesrc == "wtqs") {
//			document.getElementById("main_right").src = "wtqs";
		} else if (middlesrc == "zlpg") {
			document.getElementById("main_right").src = "zlpg";
		} else if (middlesrc == "sssj") {
			document.getElementById("main_right").src = "sssj";
		}
	}
}

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 == 4) {
			var devname = childNodes[i].getParentNode();
			var bdname = childNodes[i].getParentNode().getParentNode();
			var gdname = childNodes[i].getParentNode().getParentNode()
					.getParentNode();

			zTree.expandNode(gdname, true);
			zTree.expandNode(bdname, true);
			zTree.expandNode(devname, true);
			zTree.expandNode(childNodes[i], true);

			$('#pointInfo').text(
					'所选监测点:'+ gdname.name + '->' + bdname.name + '->' + devname.name + '->'
							+ childNodes[i].name);

			var line_index = childNodes[i].nodeIndex;
			$("#lineIndex").val(line_index);
			$("#lineName").val(childNodes[i].name);

			var status = childNodes[i].status;
			var state = childNodes[i].state;
			if (state == 2) {
				$("#image").val("gray");
			} else if (state == 1) {
				$("#image").val("yellow");
			} else {
				if (status == 1) {
					$("#image").val("green");
				} else {
					$("#image").val("red");
				}
			}

			// 关闭所有节点的高亮
			var treeObj = $.fn.zTree.getZTreeObj("tree");
			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;
		}
	}

	if (path.indexOf("monitor") != -1) {
		var rsrc = $("#middlesrc").val();
		document.getElementById("main_right").src = rsrc;
	}else{

	}

}

/*******************************************************************************
 * 选中之前验证是否超过约定上限 超过弹框提示并不选中节点
 ******************************************************************************/
var limitMax = 1;
var arrVerify;

function doVerify() {
	arrVerify = getAllSelected();
}

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

function onVerify() {
	var arrPoint = getTreeSelected(1);

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

	var arr = getAllSelected();
	var zTree = $.fn.zTree.getZTreeObj("tree");

	closeHigh();

    if (limitMax > 0 && arrPoint.length > limitMax) {
        if (arrVerify.length == 0) {
            zTree.checkAllNodes(false);
        } else {
            for (var i = 0; i < arr.length; i++) {
                var index = $.inArray(arr[i], arrVerify);

				if (index === -1) {
					zTree.checkNode(arr[i], false, false);
				}
			}
		}

		layer.msg("最多选择" + limitMax + "个监测点.");
	} else {
		for (var j = 0; j < arrVerify.length; j++) {
			arrVerify[j].highlight = false;
			zTree.updateNode(arrVerify[j]);
		}

		for (var i = 0; i < arrPoint.length; i++) {
			arrPoint[i].highlight = true;
			zTree.updateNode(arrPoint[i]);
		}
	}
}

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

// 关闭所有高亮节点
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 allNodes = treeObj.getNodes();
//	treeObj.hideNodes(allNodes);
//	var nodes = treeObj.getNodesByParamFuzzy("name", value);// 获取搜索的节点
//
//	closeHigh();
//
//	treeObj.expandAll(false);// 展开第一层节点
//	treeObj.expandNode(treeObj.getNodes()[0], true);
//
//	// 高亮并展开搜索到的节点
//	for (var i = 0; i < nodes.length; i++) {
//		if(nodes[i].nodeType == 5){
//			treeObj.showNode(nodes[i].getParentNode().getParentNode().getParentNode().getParentNode().getParentNode());
//			treeObj.showNode(nodes[i].getParentNode().getParentNode().getParentNode().getParentNode());
//			treeObj.showNode(nodes[i].getParentNode().getParentNode().getParentNode());
//			treeObj.showNode(nodes[i].getParentNode().getParentNode());
//			treeObj.showNode(nodes[i].getParentNode());
//			treeObj.showNode(nodes[i]);
//		}else if(nodes[i].nodeType == 4){
//			treeObj.showNode(nodes[i].getParentNode().getParentNode().getParentNode().getParentNode());
//			treeObj.showNode(nodes[i].getParentNode().getParentNode().getParentNode());
//			treeObj.showNode(nodes[i].getParentNode().getParentNode());
//			treeObj.showNode(nodes[i].getParentNode());
//			treeObj.showNode(nodes[i]);
//			treeObj.showNode(nodes[i].children);
//		}else if(nodes[i].nodeType == 3){
//			var a = nodes[i].getParentNode().getParentNode().getParentNode();
//			treeObj.showNode(nodes[i].getParentNode().getParentNode().getParentNode());
//			treeObj.showNode(nodes[i].getParentNode().getParentNode());
//			treeObj.showNode(nodes[i].getParentNode());
//			treeObj.showNode(nodes[i]);
//			treeObj.showNode(nodes[i].children);
//		}else if(nodes[i].nodeType == 2){
//			treeObj.showNode(nodes[i].getParentNode().getParentNode());
//			treeObj.showNode(nodes[i].getParentNode());
//			treeObj.showNode(nodes[i]);
//			treeObj.showNode(nodes[i].children);
//		}else if(nodes[i].nodeType == 1){
//			treeObj.showNode(nodes[i].getParentNode());
//			treeObj.showNode(nodes[i]);
//			treeObj.showNode(nodes[i].children);
//		}else if(nodes[i].nodeType == 0){
//			treeObj.showNode(nodes[i]);
//			treeObj.showNode(nodes[i].children);
//		}
//		nodes[i].highlight = true;
//		treeObj.updateNode(nodes[i]); // 更新节点，让高亮生效
//		treeObj.expandNode(nodes[i].getParentNode(), true);
//	}

    var hiddenNodes = [];
    var ztreeObj = $.fn.zTree.getZTreeObj("tree");
    var keyword=$("#search_value").val();
    //显示上次搜索后隐藏的结点，注意：使用hideNodes（）方法时需要引包jquery.ztree.exhide-3.5.min.js
    //ztreeObj.showNodes(hiddenNodes);
    //获取不符合条件的子父结点
    hiddenNodes=ztreeObj.getNodesByFilter(filterFunc);
    //隐藏不符合条件的子父结点，注意：使用hideNodes（）方法时需要引包jquery.ztree.exhide-3.5.min.js
    ztreeObj.hideNodes(hiddenNodes);

	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);
	}

    $("#serchB").css("display", "none");
    $("#resetB").css("display", "");
}
/**
 * 查找子结点，如果匹配模糊查询的关键字则返回false，否则返回true
 */
function searchChildren(pNode,keyword){
    var childs=pNode.children;
    var isexit=true;

    for(var i = 0;i < childs.length;i++){
    	if(childs[i].name.indexOf(keyword)!=-1){
    		return false;
    	}
    }
    for(var i = 0;i < childs.length;i++){
      if(childs[i].isParent){
         isexit=searchChildren(childs[i],keyword);
             if(!isexit){
                 return isexit;
             }
       }else{
         if(childs[i].name.indexOf(keyword)!=-1){
             return false;
             }
        }
      }
    return isexit;
}

function searchParent(pNode,keyword){
    var parent=pNode.getParentNode();
    var isexit=true;
    if(parent.name.indexOf(keyword)!=-1){
		return false;
	}

	if(parent.getParentNode()){
        isexit=searchParent(parent,keyword);
            if(!isexit){
                return isexit;
            }
      }else{
         if(parent.name.indexOf(keyword)!=-1){
             return false;
         }
      }
    return isexit;
}

  //查找不符合条件的结点
  //返回true表示需要隐藏，返回false表示不需要隐藏 。
  function filterFunc(node){
      var keyword=$("#search_value").val();
     //如果当前结点或其子节点匹配模糊查询的关键字，则该结点不隐藏
     if(node.name.indexOf(keyword)!=-1){
       return false;
     }
     if(node.isParent){
       var b=searchChildren(node,keyword);
       if(!b){
           return b;
       }
     }
     if(node.getParentNode() !=null){
       var b=searchParent(node,keyword);
       if(!b){
           return b;
       }
     }
       return true;
  }

/*******************************************************************************
 * zTree操作deviceTree代码段结束
 ******************************************************************************/
// type-1:getCheckedNodes方法,type-2:getSelectedNodes方法
function getTreeSelected(type) {
	var pointNodes = [];

	if (type == 1) {
		var nodes = getAllSelected();

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

		for (var i = 0; i < nodes.length; i++) {
			if (nodes[i].level == 4) {
				pointNodes.push(nodes[i]);
			}
		}
	} else if (type == 2) {
		var zTree = $.fn.zTree.getZTreeObj("tree");
		var nodes = zTree.getSelectedNodes();

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

		if (nodes[0].level == 4) {
			pointNodes.push(nodes[0]);
		}
	}

	return pointNodes;
}

function valueChange(value) {
	if (value == '') {
		closeHigh();
	}
}

$(function() {
	var treeHeight = $(window).height() - 70;
	$("#tree").css("min-height", treeHeight);
	$("#tree").css("max-height", treeHeight);

	if (path.indexOf("monitor") != -1 || path.indexOf("expmodel") != -1) {
		setting = {
				check : {
					enable : true,
					nocheckInherit : true
				},
				data : {
					simpleData : {
						enable : true
					}
				},
				view : {
					fontCss : setHighlight, // 高亮一定要设置，setHighlight是自定义方法
					addDiyDom : addDiyDom
				},
				callback : {
					onCheck : onVerify,
					beforeCheck : doVerify,
					onClick : pointSelected
				}
			};
		getNodeData(0);
	} else {
		setting = {
				check : {
					enable : true,
					nocheckInherit : true
				},
				data : {
					simpleData : {
						enable : true
					}
				},
				view : {
					fontCss : setHighlight // 高亮一定要设置，setHighlight是自定义方法
				},
				callback : {
					onCheck : onVerify,
					beforeCheck : doVerify,
					onClick : pointSelected
				}
			};
		initDiv();
		getData();
		triggerCheckBox(2);
		setTreeSelected(2, 1);
	}
});

function resetTree(){
	document.getElementById("search_value").value = "";
	var endTime = getEndTime();
	var startTime = getStartTime("月份", endTime);

	initTree(treeAllData);
	zTreeOnAsyncSuccess();

    $("#serchB").css("display", "");
    $("#resetB").css("display", "none");
}

function initDiv() {
	document.getElementById("tree").style.height = document
			.getElementById("father").offsetHeight
			- document.getElementById("search").offsetHeight - 5 + "px";
}

// 数据库获取终端树数据
function getNodeData(type) {
	var endTime = getEndTime();
	var startTime = getStartTime("月份", endTime);
	$.ajax({
		url : "/pqs9000/device/nodetree",
		type : "post",
		async : true,
		dataType : "json",
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
		data : {
			startTime : startTime,
			endTime : endTime
		},
		success : function(data) {
			if (data.code == 500 || data.body == null) {
				layer.msg("获取终端树内容失败.");
			} else {
				treeAllData = data.body;
				initTree(data.body);
				if(type == 0){
					zTreeOnAsyncSuccess();
				}else if(type ==1){
					triggerCheckBox(2);
					setTreeSelected(2, 1);
				}else if(type ==2){
				    triggerCheckBox(1);
				    setTreeSelected(1, 10);
				}else if(type ==3){
					triggerCheckBox(2);
					setTreeSelected(2, 1);
				    triggerCheckBox(1);
				    setTreeSelected(1, -1);
				}
			}

            ityzl_CLOSE_LOAD_LAYER(i);
		}
	});
}

//数据库获取厂家终端树数据
function getmancTree(type) {
	var endTime = getEndTime();
	var startTime = getStartTime("月份", endTime);

	$.ajax({
		url : "/pqs9000/device/mancTree",
		type : "post",
		async : true,
		dataType : "json",
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
		data : {
			startTime : startTime,
			endTime : endTime
		},
		success : function(data) {
			if (data.code == 500 || data.body == null) {
				layer.msg("获取终端树内容失败.");
			} else {
				treeAllData = data.body;
				initTree(data.body);
				if(type == 0){
					zTreeOnAsyncSuccess();
				}else if(type ==1){
					triggerCheckBox(2);
					setTreeSelected(2, 1);
				}else if(type ==2){
				    triggerCheckBox(1);
				    setTreeSelected(1, 10);
				}else if(type ==3){
					triggerCheckBox(2);
					setTreeSelected(2, 1);
				    triggerCheckBox(1);
				    setTreeSelected(1, -1);
				}
			}

            ityzl_CLOSE_LOAD_LAYER(i);
		}
	});
}

//数据库获取电压等级终端树数据
function getscaleTree(type) {
	var endTime = getEndTime();
	var startTime = getStartTime("月份", endTime);

	$.ajax({
		url : "/pqs9000/device/scaleTree",
		type : "post",
		async : true,
		dataType : "json",
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
		data : {
			startTime : startTime,
			endTime : endTime
		},
		success : function(data) {
			if (data.code == 500 || data.body == null) {
				layer.msg("获取终端树内容失败.");
			} else {
				treeAllData = data.body;
				initTree(data.body);
				if(type == 0){
					zTreeOnAsyncSuccess();
				}else if(type ==1){
					triggerCheckBox(2);
					setTreeSelected(2, 1);
				}else if(type ==2){
				    triggerCheckBox(1);
				    setTreeSelected(1, 10);
				}else if(type ==3){
					triggerCheckBox(2);
					setTreeSelected(2, 1);
				    triggerCheckBox(1);
				    setTreeSelected(1, -1);
				}

			}

            ityzl_CLOSE_LOAD_LAYER(i);
		}
	});
}

//数据库获取干扰源类型终端树数据
function getloadTypeTree(type) {
	var endTime = getEndTime();
	var startTime = getStartTime("月份", endTime);

	$.ajax({
		url : "/pqs9000/device/loadTypeTree",
		type : "post",
		async : true,
		dataType : "json",
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
		data : {
			startTime : startTime,
			endTime : endTime
		},
		success : function(data) {
			if (data.code == 500 || data.body == null) {
				layer.msg("获取终端树内容失败.");
			} else {
				treeAllData = data.body;
				initTree(data.body);
				if(type == 0){
					zTreeOnAsyncSuccess();
				}else if(type ==1){
					triggerCheckBox(2);
					setTreeSelected(2, 1);
				}else if(type ==2){
				    triggerCheckBox(1);
				    setTreeSelected(1, 10);
				}else if(type ==3){
					triggerCheckBox(2);
					setTreeSelected(2, 1);
				    triggerCheckBox(1);
				    setTreeSelected(1, -1);
				}
			}

            ityzl_CLOSE_LOAD_LAYER(i);
		}
	});
}

//数据库获取监测点性质终端树数据
function getmonitorTree(type) {
	var endTime = getEndTime();
	var startTime = getStartTime("月份", endTime);

	$.ajax({
		url : "/pqs9000/device/monitorTree",
		type : "post",
		async : true,
		dataType : "json",
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
		data : {
			startTime : startTime,
			endTime : endTime
		},
		success : function(data) {
			if (data.code == 500 || data.body == null) {
				layer.msg("获取终端树内容失败.");
			} else {
				treeAllData = data.body;
				initTree(data.body);
				if(type == 0){
					zTreeOnAsyncSuccess();
				}else if(type ==1){
					triggerCheckBox(2);
					setTreeSelected(2, 1);
				}else if(type ==2){
				    triggerCheckBox(1);
				    setTreeSelected(1, 10);
				}else if(type ==3){
					triggerCheckBox(2);
					setTreeSelected(2, 1);
				    triggerCheckBox(1);
				    setTreeSelected(1, -1);
				}
			}

            ityzl_CLOSE_LOAD_LAYER(i);
		}
	});
}

function getData() {
	var endTime = getEndTime();
	var startTime = getStartTime("月份", endTime);
	$.ajax({
		url : "/pqs9000/device/tree",
		type : "post",
		async : false,
		dataType : "json",
		data : {
			startTime : startTime,
			endTime : endTime
		},
		success : function(data) {
			if (data.code == 500 || data.body == null) {
				layer.msg("获取终端树内容失败.");
			} else {
				treeAllData = data.body;
				initTree(data.body);
			}
		}
	});
}

//数据库获取离线终端树数据
function getOffData() {
	var endTime = getEndTime();
	var startTime = getStartTime("月份", endTime);
	$.ajax({
		url : "/pqs9000/device/offLinetree",
		type : "post",
		async : false,
		dataType : "json",
		data : {},
		success : function(data) {
			if (data.code == 500 || data.body == null) {
				layer.msg("获取终端树内容失败.");
			} else {
				treeAllData = data.body;
				initTree(data.body);
			}
		}
	});
}

//获取装置类型设备树
$("#datatype").on("change",function() {
	var i;
	var endTime = getEndTime();
	var startTime = getStartTime("月份", endTime);
	if (path.indexOf("monitor") != -1 || path.indexOf("expmodel") != -1) {
		if($("option:selected",this).val() == 0){
			getNodeData(0);
		}else if($("option:selected",this).val() == 1){
			getmancTree(0);
		}else if($("option:selected",this).val() == 2){
			getscaleTree(0);
		}else if($("option:selected",this).val() == 3){
			getloadTypeTree(0);
		}else if($("option:selected",this).val() == 4){
			getmonitorTree(0);
		}
	}else if(path.indexOf("percent") != -1){
		if($("option:selected",this).val() == 0){
			getNodeData(2);
		}else if($("option:selected",this).val() == 1){
			getmancTree(2);
		}else if($("option:selected",this).val() == 2){
			getscaleTree(2);
		}else if($("option:selected",this).val() == 3){
			getloadTypeTree(2);
		}else if($("option:selected",this).val() == 4){
			getmonitorTree(2);
		}
	}else if(path.indexOf("normlimit") != -1){
		if($("option:selected",this).val() == 0){
			getNodeData(3);
		}else if($("option:selected",this).val() == 1){
			getmancTree(3);
		}else if($("option:selected",this).val() == 2){
			getscaleTree(3);
		}else if($("option:selected",this).val() == 3){
			getloadTypeTree(3);
		}else if($("option:selected",this).val() == 4){
			getmonitorTree(3);
		}
	}else{
		if($("option:selected",this).val() == 0){
			getNodeData(1);
		}else if($("option:selected",this).val() == 1){
			getmancTree(1);
		}else if($("option:selected",this).val() == 2){
			getscaleTree(1);
		}else if($("option:selected",this).val() == 3){
			getloadTypeTree(1);
		}else if($("option:selected",this).val() == 4){
			getmonitorTree(1);
		}
	}
})

/*******************************************************************************
 * 是否显示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]);
	}
}

/*******************************************************************************
 * 初始化终端树时，设置其选中状态 如果传入的参数<=0，则全选 type-1:复选框 type-2:无复选框
 ******************************************************************************/
function setTreeSelected(type, selectNumber) {
    var count = 0;
    //selectNumber = selectNumber <= 0 ? 1 : selectNumber;
    limitMax = selectNumber;

    if (selectNumber < 0) {
        return;
    }

    var zTree = $.fn.zTree.getZTreeObj("tree");
    var nodes = zTree.transformToArray(zTree.getNodes());

	for (var i = 0; i < nodes.length; i++) {
		if (nodes[i].level == 4) {
			zTree.selectNode(nodes[i]);

			if (type == 1) {
				zTree.checkNode(nodes[i], true, true);
			}

			nodes[i].highlight = true;
			zTree.updateNode(nodes[i]); // 更新节点，让高亮生效
			count++;

            if (selectNumber > 0 && count == selectNumber) {
                break;
            }
        }
    }
}

// 初始化终端树
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 += ',' + "quality:" + treeData[i].qualityAss;
		str += ',' + "qualityOff:0";
		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) {
			zNodes[index].icon = "../images/img/base.png";
		} else if (zNodes[index].nodeType == 1) {
			zNodes[index].icon = "../images/img/province.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) {
			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";
				}
			}
		}
	}

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