var zNodes = [ {
	id : 1,
	pId : 0,
	name : "电压总有效值(kV)",
	checked : true
}, {
	id : 2,
	pId : 0,
	name : "电流总有效值(A)",
	checked : true
}, {
	id : 3,
	pId : 0,
	name : "有功功率(kW)",
}, {
	id : 4,
	pId : 0,
	name : "无功功率(kVar)",
}, {
	id : 5,
	pId : 0,
	name : "基波电压总有效值(kV)"
}, {
	id : 6,
	pId : 0,
	name : "基波电流总有效值(A)"
}, {
	id : 7,
	pId : 0,
	name : "频率(Hz)"
}, {
	id : 8,
	pId : 0,
	name : "电压不平衡度(%)",
}, {
	id : 9,
	pId : 0,
	name : "电流不平衡度(%)"
} ]

/*******************************************************************************
 * z-tree控件基本参数设置
 ******************************************************************************/
var settingRight = {
	check : {
		enable : true,
		chkDisabledInherit : true
	},
	data : {
		simpleData : {
			enable : true
		}
	},
	callback : {
		onExpand : onExpand,
		onClick : onClick,
		onCheck : checkTarget
	}
};

var checkCount = 1, totalcount = 4, nodes = '', node = null;
var isPt = true;
function checkTarget() {
	obj1 = null; obj2 = null; obj3 = null; obj4 = null; obj5 = null; obj6 = null; obj7 = null; obj8 = null; obj9 = null;
	var height = $("body").height();
	var picHeight = height - 26;
	$('#container1').html("");
	$('#container2').html("");
	$('#container3').html("");
	var zTree1 = $.fn.zTree.getZTreeObj("target");
	checkCount = zTree1.getCheckedNodes(true).length;
	node = zTree1.getCheckedNodes(true);
	totalcount = checkCount;
	nodes = '';
	var k = 0;
	for (var i = 0; i < totalcount; i++) {
		if (k < 3) {
			nodes += node[i].id + ',';
		} else {
			zTree1.checkNode(node[i], false, true);
		}
		k++;
	}
	if (nodes != '') {
		nodes = nodes.substring(0, nodes.lastIndexOf(','));
	}

	var legandData=[],seriesData=[];
	var xx = null, // 当前时间
		yy = null; // 随机值

	if(parent.parent.pttype == 0){
		legandData.push({
			data: ['A相', 'B相', 'C相'],
			selectedMode: true,
			x: 'left'
		})
		seriesData.push({
			name : "A相",
			data : [ [ xx, yy ] ]
		}, {
			name : "B相",
			data : [ [ xx, yy ] ]
		}, {
			name : "C相",
			data : [ [ xx, yy ] ]
		})
	}else if(parent.parent.pttype == 1){
		legandData.push({
			data: ['AB相', 'BC相', 'CA相'],
			selectedMode: true,
			x: 'left'
		})
		seriesData.push({
			name : "AB相",
			data : [ [ xx, yy ] ]
		}, {
			name : "BC相",
			data : [ [ xx, yy ] ]
		}, {
			name : "CA相",
			data : [ [ xx, yy ] ]
		})
	}else{
		legandData.push({
			data: ['AB相', 'BC相'],
			selectedMode: true,
			x: 'left'
		})
		seriesData.push({
			name : "AB相",
			data : [ [ xx, yy ] ]
		}, {
			name : "BC相",
			data : [ [ xx, yy ] ]
		})
		isPt = false;
	}

	if (k == 1) {
		document.getElementById('container1').style.display = "block";
		document.getElementById('container2').style.display = "none";
		document.getElementById('container3').style.display = "none";
		$("#container1").css("height", picHeight);
		var option = node[0].name;

		if (option == "电压总有效值(kV)" || option == "电流总有效值(A)" || option == "基波电压总有效值(kV)"
			|| option == "基波电流总有效值(A)") {
			draw3Pic(option, '1',legandData,seriesData);
		} else {
			drawPic(option, '1');
		}
	} else if (k == 2) {
		document.getElementById('container1').style.display = "block";
		document.getElementById('container2').style.display = "block";
		document.getElementById('container3').style.display = "none";
		$("#container1").css("height", picHeight / 2);
		$("#container2").css("height", picHeight / 2);
		var option1 = node[0].name;
		if (option1 == "电压总有效值(kV)" || option1 == "电流总有效值(A)" || option1 == "基波电压总有效值(kV)"
			|| option1 == "基波电流总有效值(A)") {
			draw3Pic(option1, '1',legandData,seriesData);
		} else {
			drawPic(option1, '1');
		}
		var option2 = node[1].name;
		if (option2 == "电压总有效值(kV)" || option2 == "电流总有效值(A)" || option2 == "基波电压总有效值(kV)"
			|| option2 == "基波电流总有效值(A)") {
			draw3Pic(option2, '2',legandData,seriesData);
		} else {
			drawPic(option2, '2');
		}
	} else if (k == 3) {
		document.getElementById('container1').style.display = "block";
		document.getElementById('container2').style.display = "block";
		document.getElementById('container3').style.display = "block";
		$("#container1").css("height", picHeight / 2);
		$("#container2").css("height", picHeight / 2);
		$("#container3").css("height", picHeight / 2);
		var option1 = node[0].name;
		if (option1 == "电压总有效值(kV)" || option1 == "电流总有效值(A)" || option1 == "基波电压总有效值(kV)"
			|| option1 == "基波电流总有效值(A)") {
			draw3Pic(option1, '1',legandData,seriesData);
		} else {
			drawPic(option1, '1');
		}
		var option2 = node[1].name;
		if (option2 == "电压总有效值(kV)" || option2 == "电流总有效值(A)" || option2 == "基波电压总有效值(kV)"
			|| option2 == "基波电流总有效值(A)") {
			draw3Pic(option2, '2',legandData,seriesData);
		} else {
			drawPic(option2, '2');
		}
		var option3 = node[2].name;
		if (option3 == "电压总有效值(kV)" || option3 == "电流总有效值(A)" || option3 == "基波电压总有效值(kV)"
			|| option3 == "基波电流总有效值(A)") {
			draw3Pic(option3, '3',legandData,seriesData);
		} else {
			drawPic(option3, '3');
		}
	}

	if (checkCount >= 3) {
		disabledNode(true);
	} else {
		disabledNode(false);
	}

	getData(node);
}

// 回调函数，展开tree节点
function onExpand(event, treeId, treeNode) {
	curExpandNode = treeNode;
}

// 回调函数，点击tree展开节点
function onClick(e, treeId, treeNode) {
	var zTree1 = $.fn.zTree.getZTreeObj("target");
	zTree1.expandNode(treeNode, null, null, null, true);
}

// 根据条件设置tree节点状态 可用/不可用
function disabledNode(e) {
	var zTree1 = $.fn.zTree.getZTreeObj("target"), l = zTree1
		.getCheckedNodes(false).length, nodes = zTree1
		.getCheckedNodes(false);
	var nodeAll = zTree1.getNodes();
	var m = zTree1.getNodes().length;
	if (e) {
		for (var i = 0; i < l; i++) {
			zTree1.setChkDisabled(nodes[i], e, false, false);
		}
	} else {
		for (var i = 0; i < m; i++) {
			zTree1.setChkDisabled(nodeAll[i], e, true, true);
		}
	}
}

var lineIndex = window.parent.parent.document.getElementById("lineIndex").value;
var webSocket = window.parent.parent.parent.webSocket;
var vList = [], iList = [];
var result1 = [], result2 = [], result3 = [];
var obj1 = null, obj2 = null, obj3 = null, obj4 = null, obj5 = null, obj6 = null, obj7 = null, obj8 = null, obj9 = null;
$(document).ready(function() {
	// 监测点指标 tree的初始化
	$.fn.zTree.init($("#target"), settingRight, zNodes);
	checkTarget();
	// loading = ityzl_SHOW_LOAD_LAYER();
	// queryAndShowData();
});

function getData(node) {
	webSocket.onmessage = function(event) {
		console.log(event.data)
		var aVRMSlist = [], bVRMSlist = [], cVRMSlist = [];
		var aV1list = [], bV1list = [], cV1list = [];
		var aIRMSlist = [], bIRMSlist = [], cIRMSlist = [];
		var aI1list = [], bI1list = [], cI1list = [];
		var VRMSlist = [], V1list = [], Vlist = [], FVlist = [];
		var IRMSlist = [], I1list = [], Ilist = [];
		var Plist = [], Qlist = [];
		var result = [];
		var json0 = JSON.parse(event.data);// 通过kafka收到消息的主体
		var json1 = json0['resultmsg'];

		var json2 = json1[lineIndex];// 监测点数据
		var timepoint = parseInt(json2['TIME']);// 发生时刻
		var time = timepoint;
		var s = "时刻：" + Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', timepoint);
		$("#timepoint").html(s);

		var vjson = json2['V'];// 电压相关数据,数组
		var ijson = json2['I'];// 电流相关数据,数组
		var pjson = json2['PQ'];// 电流相关数据,数组
		for (var i = 0; i < vjson.length; i++) {
			var tmplist = vjson[i];
			if (tmplist['SEQ'] == 'A') {
				aVRMSlist.push(time);
				aVRMSlist.push(parseFloat(tmplist['VRMS']));// 电压有效值
				aV1list.push(time);
				aV1list.push(parseFloat(tmplist['V1']));// 基波电压有效值
			}
			if (tmplist['SEQ'] == 'B') {
				bVRMSlist.push(time);
				bVRMSlist.push(parseFloat(tmplist['VRMS']));// 电压有效值
				bV1list.push(time);
				bV1list.push(parseFloat(tmplist['V1']));// 基波电压有效值
			}
			if (tmplist['SEQ'] == 'C') {
				cVRMSlist.push(time);
				cVRMSlist.push(parseFloat(tmplist['VRMS']));// 电压有效值
				cV1list.push(time);
				cV1list.push(parseFloat(tmplist['V1']));// 基波电压有效值
			}
			if (tmplist['SEQ'] == 'T') {
				Vlist.push(time);
				Vlist.push(parseFloat(tmplist['V_UNBAN']));// 电压不平衡
				FVlist.push(time);
				FVlist.push(parseFloat(tmplist['FREQ']));// 频率(Hz)
			}

		}
		VRMSlist.push(aVRMSlist);
		VRMSlist.push(bVRMSlist);
		VRMSlist.push(cVRMSlist);
		V1list.push(aV1list);
		V1list.push(bV1list);
		V1list.push(cV1list);

		for (var i = 0; i < ijson.length; i++) {
			var tmplist = ijson[i];
			if (tmplist['SEQ'] == 'A') {
				aIRMSlist.push(time);
				aIRMSlist.push(parseFloat(tmplist['IRMS']));// 电流有效值
				aI1list.push(time);
				aI1list.push(parseFloat(tmplist['I1']));// 基波电流有效值
			}
			if (tmplist['SEQ'] == 'B') {
				bIRMSlist.push(time);
				bIRMSlist.push(parseFloat(tmplist['IRMS']));// 电流有效值
				bI1list.push(time);
				bI1list.push(parseFloat(tmplist['I1']));// 基波电流有效值
			}
			if (tmplist['SEQ'] == 'C') {
				cIRMSlist.push(time);
				cIRMSlist.push(parseFloat(tmplist['IRMS']));// 电流有效值
				cI1list.push(time);
				cI1list.push(parseFloat(tmplist['I1']));// 基波电流有效值
			}
			if (tmplist['SEQ'] == 'T') {
				Ilist.push(time);
				Ilist.push(parseFloat(tmplist['I_UNBAN']));// 电流不平衡
			}

		}
		IRMSlist.push(aIRMSlist);
		IRMSlist.push(bIRMSlist);
		IRMSlist.push(cIRMSlist);
		I1list.push(aI1list);
		I1list.push(bI1list);
		I1list.push(cI1list);

		for (var i = 0; i < pjson.length; i++) {
			var tmplist = pjson[i];
			if (tmplist['SEQ'] == 'T') {
				Plist.push(time);
				Plist.push(parseFloat(tmplist['P']));// 有功功率(kW)
				Qlist.push(time);
				Qlist.push(parseFloat(tmplist['Q']));// 无功功率(kVar)
			}
		}

		for(var i=0;i<node.length;i++) {
			var option = node[i].name;
			if (option == "电压总有效值(kV)") {
				obj1.hcEvents.load[0](VRMSlist);
			}
			if (option == "电流总有效值(A)") {
				obj2.hcEvents.load[0](IRMSlist);// 电流总有效值(A)
			}
			if (option == "有功功率(kW)") {
				obj3.hcEvents.load[0](Plist);// 有功功率(kW)
			}
			if (option == "无功功率(kVar)") {
				obj4.hcEvents.load[0](Qlist);// 无功功率(kW)
			}
			if (option == "基波电压总有效值(kV)") {
				obj5.hcEvents.load[0](V1list);// 基波电压总有效值
			}
			if (option == "基波电流总有效值(A)") {
				obj6.hcEvents.load[0](I1list);// 基波电流总有效值(A)
			}
			if (option == "频率(Hz)") {
				obj7.hcEvents.load[0](FVlist);
				result.push(FVlist);// 频率(Hz)
			}
			if (option == "电压不平衡度(%)") {
				obj8.hcEvents.load[0](Vlist);// 电压不平衡
			}
			if (option == "电流不平衡度(%)") {
				obj9.hcEvents.load[0](Ilist);// 电流不平衡
			}
		}

	}
}

function drawPic(option, node) {
	var delFlag = 1;
	var unit="";

	Highcharts.setOptions({
		global : {
			useUTC : false
		}
	});
	var xx = (new Date()).getTime()-1000*60, // 当前时间
		yy = 0; // 随机值

	var pic = 'container' + node;
	$(pic).css("height", $(window).height() - 30);
	var chart = Highcharts.chart(pic, {
		chart : {
			type : 'spline',
			marginRight : 10,
			events : {
				load : function(data) {
					var series = null, chart = this;

					if (option == "有功功率(kW)") {
						unit = "kW";
						if (obj3 == null) {
							obj3 = chart;
						}else{
							chart=obj3;
						}
					} else if (option == "无功功率(kVar)") {
						unit = "kVar";
						if (obj4 == null) {
							obj4 = chart;
						}else{
							chart=obj4;
						}
					} else if (option == "频率(Hz)") {
						unit = "Hz";
						if (obj7 == null) {
							obj7 = chart;
						}else{
							chart=obj7;
						}
					} else if (option == "电压不平衡度(%)") {
						unit = "%";
						if (obj8 == null) {
							obj8 = chart;
						}else{
							chart=obj8;
						}
					} else if (option == "电流不平衡度(%)") {
						unit = "%";
						if (obj9 == null) {
							obj9 = chart;
						}else{
							chart=obj9;
						}
					}

					series=chart.series[0];
					if (data.length > 0) {
						if (series.data.length == 1 && delFlag == 1) {
							series.addPoint(data,false,true,true);
							delFlag = 0;
						} else if (series.data.length < 60 && delFlag == 0) {
							series.addPoint(data);
						} else {
							series.addPoint(data, true, true);
						}
					}
				}
			}
		},
		title : {
			text : option,
			style: {
				fontSize:'15px'
			}
		},
		credits: false,
		xAxis : {
			type : 'datetime',
			tickPixelInterval : 150
		},
		yAxis : {
			title : {
				text : null
			}
		},
		tooltip : {
			formatter : function() {
				return '<b>' + this.series.name + '</b><br/>'
					+ Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x)
					+ '<br/>' + Highcharts.numberFormat(this.y, 4) + unit;
			}
		},
		legend : {
			enabled : false
		},
		series : [ {
			name : option,
			data : [ [ xx, yy ] ]
		} ]
	});
}

function draw3Pic(option, nodeid, legendData,seriesData) {
	var delFlag = 1;

	Highcharts.setOptions({
		global : {
			useUTC : false
		}
	});
	var pic = 'container' + nodeid;
	$(pic).css("height", ($(window).height()-30)/3);
	var chart = Highcharts.chart(pic, {
		chart : {
			type : 'spline',
			marginRight : 10,
			events : {
				load : function(data) {
					var series = null,chart = this;

					if (option == "电压总有效值(kV)") {
						if (obj1 == null) {
							obj1 = chart;//.hcEvents.load;
						}else{
							chart = obj1;
						}
					} else if (option == "电流总有效值(A)") {
						if (obj2 == null) {
							obj2 = chart;//.hcEvents.load;
						}else{
							chart = obj2;
						}
					} else if (option == "基波电压总有效值(kV)") {
						if (obj5 == null) {
							obj5 = chart;//.hcEvents.load;
						}else{
							chart = obj5;
						}
					} else if (option == "基波电流总有效值(A)") {
						if (obj6 == null) {
							obj6 = chart;//.hcEvents.load;
						}else{
							chart = obj6;
						}
					}

					series = chart.series;

					if (data.length > 0) {
						var a = data[0];
						var b = data[1];
						var c = data[2];
						if(series[0].data.length == 1 && delFlag == 1){
							series[0].addPoint(a,false,true,true);
							series[1].addPoint(b,false,true,true);
							if(isPt){
								series[2].addPoint(c,true,true,true);
							}
							delFlag = 0;
						}else if (series[0].data.length < 60) {
							series[0].addPoint(a);
							series[1].addPoint(b);
							if(isPt){
								series[2].addPoint(c);
							}
						} else {
							series[0].addPoint(a, false, true, true);
							series[1].addPoint(b, false, true, true);
							if(isPt){
								series[2].addPoint(c, true, true, true);
							}
						}
					}
				}
			}
		},
		title : {
			text : option,
			style: {
				fontSize:'15px'
			}
		},
		credits: false,
		xAxis : {
			type : 'datetime',
			tickPixelInterval : 150
		},
		yAxis : {
			title : {
				text : null
			}
		},
//		tooltip : {
//			formatter : function() {
//				return '<b>' + this.series.name + '</b><br/>'
//						+ Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x)
//						+ '<br/>' + Highcharts.numberFormat(this.y, 2);
//			}
//		},
		legend : legendData,
		colors:[yellow,green,red],
		series : seriesData
	});
}
