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

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

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

	closeHigh();

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

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

		$('#pointInfo').text(
				'所选监测点:' + projectname.name + '->' + provincename.name + '->' + subname.name + '->'
						+ nodes[0].name);

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

		$("#allstime").val("");
		$("#alletime").val("");
		$("#checkpieces").val("");
		$("#checkpiecee").val("");

		var middlesrc = $("#middlesrc").val();
		if (middlesrc == "offwtqs") {
			document.getElementById("main_right").src = "offwtqs";
			document.getElementById("main_right").contentWindow.changeTree(pttype);
		} else if (middlesrc == "offxbpp") {
			document.getElementById("main_right").src = "offxbpp";
		} else if (middlesrc == "offword") {
			document.getElementById("main_right").src = "offword";
		}
	}
}

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 subname = childNodes[i].getParentNode();
			var provincename = childNodes[i].getParentNode().getParentNode();
			var projectname = childNodes[i].getParentNode().getParentNode().getParentNode();

			zTree.expandNode(projectname, true);
			zTree.expandNode(provincename, true);
			zTree.expandNode(subname, true);
			zTree.expandNode(childNodes[i], true);

			$('#pointInfo').text(
					'所选监测点:' + projectname.name + '->' + provincename.name + '->' + subname.name + '->'
							+ childNodes[i].name);

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

			// 关闭所有节点的高亮
			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]); // 更新节点，让高亮生效

			var middlesrc = $("#middlesrc").val();
			if (middlesrc == "offwtqs") {
				document.getElementById("main_right").contentWindow.changeTree(pttype);
			}

			break;
		}
	}

}

/*******************************************************************************
 * 选中之前验证是否超过约定上限 超过弹框提示并不选中节点
 ******************************************************************************/
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 (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 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);
	}
}

/*******************************************************************************
 * 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() {
	if (path.indexOf("offlinemonitor") != -1) {
		setting = {
				check : {
					enable : true,
					nocheckInherit : true
				},
				data : {
					simpleData : {
						enable : true
					}
				},
				view : {
					fontCss : setHighlight // 高亮一定要设置，setHighlight是自定义方法
				},
				callback : {
					onCheck : onVerify,
					beforeCheck : doVerify,
					onClick : pointSelected
				}
			};
		getData();
//		zTreeOnAsyncSuccess();
	} 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 initDiv() {
	document.getElementById("tree").style.height = document
			.getElementById("father").offsetHeight
			- document.getElementById("search").offsetHeight - 5 + "px";
}

//数据库获取离线终端树数据
function getData() {
	$.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 {
				initTree(data.body);
			}
		}
	});
}

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

//数据库获取离线终端树干扰源类型数据
function gettypeData() {
	$.ajax({
		url : "/pqs9000/device/loadtypetree",
		type : "post",
		async : false,
		dataType : "json",
		data : {},
		success : function(data) {
			if (data.code == 500 || data.body == null) {
				layer.msg("获取终端树内容失败.");
			} else {
				initTree(data.body);
			}
		}
	});
}

/*******************************************************************************
 * 是否显示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，则默认置为1 type-1:复选框 type-2:无复选框
 ******************************************************************************/
function setTreeSelected(type, selectNumber) {
	var count = 0;
	selectNumber = selectNumber <= 0 ? 1 : selectNumber;
	limitMax = selectNumber;
	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 (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 += ',' + "pttype:" + treeData[i].pttype;
		str += ',' + "pId:" + treeData[i].pId + '}';
		datalist.push(str);
	}

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

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