
var picHeight;
var endTime=getEndTime();
var startTime=getStartTime("月份",endTime);
var lineIndex=window.parent.document.getElementById("lineIndex").value;
startTime=filterXSS(startTime);
endTime=filterXSS(endTime);
lineIndex=filterXSS(lineIndex);
window.parent.parent.thisUrl = "wtzb"
var legend1,legend2;
$("#interval").val("月份");

$(function () {
	$("#startTime").val(startTime);
	picHeight=$(window).height()-135;
	getSteady(startTime,endTime,lineIndex);
})

$("#interval").change(function(){
	if($("#interval").val() == "年份" || $("#interval").val() == "周" || $("#interval").val() == "自定义"){
		document.getElementById("hb").style.display="none";
	}else{
		document.getElementById("hb").style.display="";
	}

	if($("#interval").val() == "周" || $("#interval").val() == "自定义"){
		document.getElementById("tb").style.display="none";
	}else{
		document.getElementById("tb").style.display="";
	}
})
$("#query").click(function (){
	document.getElementById("res").style.display="none";
	startTime=$("#startTime").val();
	endTime=$("#endTime").val();
	getSteady(startTime,endTime,lineIndex);
})

$("#tb").click(function (){
	document.getElementById("res").style.display="";
	endTime=$("#endTime").val();
	startTime=$("#startTime").val();

	var year = parseInt(startTime.substring(0, 4));
	var month = parseInt(startTime.substring(5, 7));

	var syear=year - 1;
	var laststartTime=syear + '-' + month + '-01';

	var emonth = parseInt(endTime.substring(5, 7));
	var eday=new Date(syear, emonth, 0).getDate();
	var lastendTime=syear + '-' + emonth + '-' + eday;

	if(month==emonth){
		legend1 = year+'年'+month+'月';
		legend2 = syear+'年'+month+'月';
	}else{
		legend1 = year+'年'+month+'-'+emonth+'月';
		legend2 = syear+'年'+month+'-'+emonth+'月';
	}

	getSteadyTB(startTime,endTime,laststartTime,lastendTime,lineIndex,"同比");
})

$("#hb").click(function (){
	document.getElementById("res").style.display="";
	var interval = $("#interval").val();
	endTime=$("#endTime").val();
	startTime=$("#startTime").val();

	if(interval == "月份"){
		var year = parseInt(startTime.substring(0, 4));
		var month = parseInt(startTime.substring(5, 7));
		if (month == 1) {
			var lastyear = year-1;
			var laststartTime=lastyear  + "-12-01";

			var lastendTime=lastyear  + "-12-31";
			legend1 =year+'年'+ month+'月';
			legend2 = lastyear+'年12月';
		}else if (month <= 10) {
			var smonth=month-1;
			var laststartTime=year + '-0' + smonth + '-01';

			var eday=new Date(year, smonth, 0).getDate();
			var lastendTime=year + '-0' + smonth + '-' + eday;
			legend1 =year+'年'+ month+'月';
			legend2 = year+'年'+ smonth+'月';
		}else{
			var smonth=month-1;
			var laststartTime=year + '-' + smonth + '-01';

			var eday=new Date(year, smonth, 0).getDate();
			var lastendTime=year + '-' + smonth + '-' + eday;
			legend1 = year+'年'+ month+'月';
			legend2 = year+'年'+ smonth+'月';
		}
		getSteadyTB(startTime,endTime,laststartTime,lastendTime,lineIndex,"环比");
	}else if(interval == "季度"){
		var year = parseInt(startTime.substring(0, 4));
		var month = parseInt(startTime.substring(5, 7));
		if (month == 1) {
			var lastyear = year-1;
			var laststartTime=year  + "-10-01";

			var lastendTime=year  + "-12-31";
			legend1 = year+'年1-3月';
			legend2 = lastyear+'年10-12月';
		}else{
			var month1=month+2;
			var smonth=month-3;
			var laststartTime=year + '-' + smonth + '-01';

			var emonth=smonth+2;
			var eday=new Date(year, emonth, 0).getDate();
			var lastendTime=year + '-' + emonth + '-' + eday;
			legend1 = year+'年'+ month+'-'+month1+'月';
			legend2 = year+'年'+ smonth+'-'+emonth+'月';
		}
		getSteadyTB(startTime,endTime,laststartTime,lastendTime,lineIndex,"环比");
	}
})

$("#res").click(function (){
	document.getElementById("res").style.display="none";
	startTime=$("#startTime").val();
	endTime=$("#endTime").val();
	getSteady(startTime,endTime,lineIndex);
})

function getSteady(startTime,endTime,lineIndex){
	var i;
	$.ajax({
		url:'/pqs9000/device/steady',
		data:{
			startTime:startTime,
			endTime:endTime,
			lineIndex:lineIndex
		},
		type:'post',
		dataType:'json',
		beforeSend: function () {
			i = ityzl_SHOW_LOAD_LAYER();
		},
		success:function (data) {
			if (data.code===500|data.body===null){
				initWtzb(null);
			}else{
				var info = data.body;
				console.log(info)
				var frequency={};var frequencyitem={};
				var flicker={};var flickeritem={};
				var unbalanceVol={};var unbalanceVolitem={};
				var harmVoltage={};var harmVoltageUitem={};
				var deviationVol={};var deviationVolitem={};
				var harmElec={};var harmElecitem={};
				var iNeg={};var iNegitem={};
				var inUharm={};var inUharmitem={};

				frequency.value=info.frequency;
				if(frequency.value===3.14159){
					frequencyitem.color=noData;
					frequency.value=1.555;
				}else if(frequency.value>100){
					frequencyitem.color=green;
					frequency.value=100;
				}else if (frequency.value>90){
					frequencyitem.color=green;
				}else if (frequency.value<60){
					frequencyitem.color=red;
				}else{
					frequencyitem.color=yellow;
				}
				frequency.itemStyle=frequencyitem;

				flicker.value=info.flicker;
				if(flicker.value===3.14159){
					flickeritem.color=noData;
					flicker.value=1.555;
				}else if(flicker.value>100){
					flickeritem.color=green;
					flicker.value=100;
				}else if (flicker.value>90){
					flickeritem.color=green;
				}else if (flicker.value<60){
					flickeritem.color=red;
				}else{
					flickeritem.color=yellow;
				}
				flicker.itemStyle=flickeritem;

				unbalanceVol.value=info.uBalance;
				if(unbalanceVol.value===3.14159){
					unbalanceVolitem.color=noData;
					unbalanceVol.value=1.555;
				}else if(unbalanceVol.value>100){
					unbalanceVolitem.color=green;
					unbalanceVol.value=100;
				}else if (unbalanceVol.value>90){
					unbalanceVolitem.color=green;
				}else if (unbalanceVol.value<60){
					unbalanceVolitem.color=red;
				}else{
					unbalanceVolitem.color=yellow;
				}
				unbalanceVol.itemStyle=unbalanceVolitem;

				//谐波电压
				// harmVoltage.value=getHarmVoltageValue(info.uAberrance,info.evenVoltage,info.oddVoltage);
				harmVoltage.value=info.harmVoltage;
				if(harmVoltage.value===3.14159){
					harmVoltageUitem.color=noData;
					harmVoltage.value=1.555;
				}else if(harmVoltage.value>100){
					harmVoltageUitem.color=green;
					harmVoltage.value=100;
				}else if (harmVoltage.value>90){
					harmVoltageUitem.color=green;
				}else if (harmVoltage.value<60){
					harmVoltageUitem.color=red;
				}else{
					harmVoltageUitem.color=yellow;
				}
				harmVoltage.itemStyle=harmVoltageUitem;

				deviationVol.value=info.voltageDeviation;
				if(deviationVol.value===3.14159){
					deviationVolitem.color=noData;
					deviationVol.value=1.555;
				}else if(deviationVol.value>100){
					deviationVolitem.color=green;
					deviationVol.value=100;
				}else if (deviationVol.value>90){
					deviationVolitem.color=green;
				}else if (deviationVol.value<60){
					deviationVolitem.color=red;
				}else{
					deviationVolitem.color=yellow;
				}
				deviationVol.itemStyle=deviationVolitem;

				//谐波电流
				// harmElec.value=getHarmElecValue(info.oddElectric,info.evenElectric);
				harmElec.value=info.harmElec;
				if(harmElec.value===3.14159){
					harmElecitem.color=noData;
					harmElec.value=1.555;
				}else if(harmElec.value>100){
					harmElecitem.color=green;
					harmElec.value=100;
				}else if (harmElec.value>90){
					harmElecitem.color=green;
				}else if (harmElec.value<60){
					harmElecitem.color=red;
				}else{
					harmElecitem.color=yellow;
				}
				harmElec.itemStyle=harmElecitem;


				iNeg.value=info.iNeg;
				if(iNeg.value===3.14159){
					iNegitem.color=noData;
					iNeg.value=1.555;
				}else if(iNeg.value>100){
					iNegitem.color=green;
					iNeg.value=100;
				}else if (iNeg.value>90){
					iNegitem.color=green;
				}else if (iNeg.value<60){
					iNegitem.color=red;
				}else{
					iNegitem.color=yellow;
				}
				iNeg.itemStyle=iNegitem;

				inUharm.value=info.inUharm;
				if(inUharm.value===3.14159){
					inUharmitem.color=noData;
					inUharm.value=1.555;
				}else if(inUharm.value>100){
					inUharmitem.color=green;
					inUharm.value=100;
				}else if (inUharm.value>90){
					inUharmitem.color=green;
				}else if (inUharm.value<60){
					inUharmitem.color=red;
				}else{
					inUharmitem.color=yellow;
				}
				inUharm.itemStyle=inUharmitem;


				initWtzb(frequency,flicker,unbalanceVol,harmVoltage,deviationVol,harmElec,iNeg,inUharm);

				if(info.frequency===3.14159){
					document.getElementById("data1").innerText = "(暂无数据)";
				}else if(info.frequency>100){
					document.getElementById("data1").innerText = "100";
				}else{
					document.getElementById("data1").innerText = info.frequency;
				}

				if(info.flicker===3.14159){
					document.getElementById("data2").innerText = "(暂无数据)";
				}else if(info.flicker>100){
					document.getElementById("data2").innerText = "100";
				}else{
					document.getElementById("data2").innerText = info.flicker;
				}

				if(info.uBalance===3.14159){
					document.getElementById("data3").innerText = "(暂无数据)";
				}else if(info.uBalance>100){
					document.getElementById("data3").innerText = "100";
				}else{
					document.getElementById("data3").innerText = info.uBalance;
				}

				//计算谐波电压
				if(info.harmVoltage===3.14159){
					document.getElementById("data4").innerText = "(暂无数据)";
				}else if(info.harmVoltage>100){
					document.getElementById("data4").innerText = "100";
				}else{
					document.getElementById("data4").innerText = info.harmVoltage;
				}

				if(info.voltageDeviation===3.14159){
					document.getElementById("data5").innerText = "(暂无数据)";
				}else if(info.voltageDeviation>100){
					document.getElementById("data5").innerText = "100";
				}else{
					document.getElementById("data5").innerText = info.voltageDeviation;
				}

				//计算谐波电流
				if(info.harmElec===3.14159){
					document.getElementById("data6").innerText = "(暂无数据)";
				}else if(info.harmElec>100){
					document.getElementById("data6").innerText = "100";
				}else{
					document.getElementById("data6").innerText = info.harmElec;
				}

				if(info.iNeg===3.14159){
					document.getElementById("data7").innerText = "(暂无数据)";
				}else if(info.iNeg>100){
					document.getElementById("data7").innerText = "100";
				}else{
					document.getElementById("data7").innerText = info.iNeg;
				}

				if(info.inUharm===3.14159){
					document.getElementById("data8").innerText = "(暂无数据)";
				}else if(info.inUharm>100){
					document.getElementById("data8").innerText = "100";
				}else{
					document.getElementById("data8").innerText = info.inUharm;
				}


			}
			//***********关闭loading
			ityzl_CLOSE_LOAD_LAYER(i);
		}
	})
}

//初始化通信状态
function initWtzb(frequency,flicker,unbalanceVol,harmVoltage,deviationVol,harmElec,iNeg,inUharm) {
	$("#wtzb").css("height",picHeight);
	echarts.dispose($("#wtzb")[0]);
	var wtzbOption = echarts.init($("#wtzb")[0]);
	var max=100;
	var labels = ['频率偏差', '闪变','电压不平衡', '谐波电压',  '电压偏差', '谐波电流', '负序电流', '间谐波电压含有率'];
	var option = {
		backgroundColor: canvasBG,//背景色,
		title: {
			text: '稳态指标合格率',
			x: 'center'
		},
		grid:{
			left:'4%',
			right:'2%',
			bottom:'10%'
		},
		tooltip: {
			trigger: 'axis',
			axisPointer: {
				type: 'shadow'
			},
			formatter: function (params) {
				var color=params[0].color;
				var tips="";
				if(color===noData){
					tips+='(暂无数据)';
				}else{
					tips+=params[0].value;
				}
				return tips;
			}
		},
		legend: {
			data: ['小于60','60~90','大于90'],
			selectedMode: false,
			x: 'left'
		},
		toolbox: {
			show : true,
			feature : {
				saveAsImage : {show: true}
			},
			right:'2%'
		},
		xAxis: [{
			type: 'category',
			data: labels,
			splitLine: {
				show: false
			}
		}],
		yAxis: [{
			type: 'value',
			name: '%',
			max:max
		}],
		series: [{
			name: '小于60',
			type: 'bar',
			data: [],
			barMaxWidth: barMax
		},{
			name: '60~90',
			type: 'bar',
			data: [frequency,flicker,unbalanceVol,harmVoltage,deviationVol,harmElec,iNeg,inUharm],
			barMaxWidth: barMax
		},{
			name: '大于90',
			type: 'bar',
			data: [],
			barMaxWidth: barMax
		}],
		color:[red,yellow,green]
	};

	wtzbOption.setOption(option);
}

function getSteadyTB(startTime,endTime,laststartTime,lastendTime,lineIndex,selector){
	var i;
	$.ajax({
		url:'/pqs9000/device/steadyTB',
		data:{
			startTime:startTime,
			endTime:endTime,
			laststartTime:laststartTime,
			lastendTime:lastendTime,
			lineIndex:lineIndex
		},
		type:'post',
		dataType:'json',
		beforeSend: function () {
			i = ityzl_SHOW_LOAD_LAYER();
		},
		success:function (data) {
			if (data.code===500|data.body===null){
				initWtzbTB(null);
			}else{
				var info = data.body;
				var thisvalue0,thisvalue1,thisvalue2,thisvalue3,thisvalue4,thisvalue5,thisvalue6,thisvalue7;
				var lastvalue0,lastvalue1,lastvalue2,lastvalue3,lastvalue4,lastvalue5,lastvalue6,lastvalue7;

				var thisTime=[];
				if(info[0].frequency===3.14159){
					thisTime.push(1.555);
					thisvalue0="暂无数据";
				}else if(info[0].frequency>100){
					thisTime.push(100);
					thisvalue0="100";
				}else{
					thisTime.push(info[0].frequency);
					thisvalue0 = info[0].frequency;
				}
				if(info[0].flicker===3.14159){
					thisTime.push(1.555);
					thisvalue1="暂无数据";
				}else if(info[0].flicker>100){
					thisTime.push(100);
					thisvalue1="100";
				}else{
					thisTime.push(info[0].flicker);
					thisvalue1 = info[0].flicker;
				}
				if(info[0].uBalance===3.14159){
					thisTime.push(1.555);
					thisvalue2="暂无数据";
				}else if(info[0].uBalance>100){
					thisTime.push(100);
					thisvalue2="100";
				}else{
					thisTime.push(info[0].uBalance);
					thisvalue2 = info[0].uBalance;
				}
				//获取谐波电压
				// var harmVoltage =getHarmVoltageValue(info[0].uAberrance,info[0].evenVoltage,info[0].oddVoltage);
				var harmVoltage = info[0].harmVoltage;
				if(harmVoltage===3.14159){
					thisTime.push(1.555);
					thisvalue3="暂无数据";
				}else if(harmVoltage>100){
					thisTime.push(100);
					thisvalue3="100";
				}else{
					thisTime.push(harmVoltage);
					thisvalue3 = harmVoltage;
				}

				if(info[0].voltageDeviation===3.14159){
					thisTime.push(1.555);
					thisvalue4="暂无数据";
				}else if(info[0].voltageDeviation>100){
					thisTime.push(100);
					thisvalue4="100";
				}else{
					thisTime.push(info[0].voltageDeviation);
					thisvalue4 = info[0].voltageDeviation;
				}
				//获取谐波电流
				// var harmElec=getHarmElecValue(info[0].oddElectric,info[0].evenElectric);
				var harmElec = info[0].harmElec;
				if(harmElec===3.14159){
					thisTime.push(1.555);
					thisvalue5="暂无数据";
				}else if(harmElec>100){
					thisTime.push(100);
					thisvalue5="100";
				}else{
					thisTime.push(harmElec);
					thisvalue5 = harmElec;
				}

				if(info[0].iNeg===3.14159){
					thisTime.push(1.555);
					thisvalue6="暂无数据";
				}else if(info[0].iNeg>100){
					thisTime.push(100);
					thisvalue6="100";
				}else{
					thisTime.push(info[0].iNeg);
					thisvalue6 = info[0].iNeg;
				}
				if(info[0].inUharm===3.14159){
					thisTime.push(1.555);
					thisvalue7="暂无数据";
				}else if(info[0].inUharm>100){
					thisTime.push(100);
					thisvalue7="100";
				}else{
					thisTime.push(info[0].inUharm);
					thisvalue7 = info[0].inUharm;
				}


				var lastTime=[];
				if(info[1].frequency===3.14159){
					lastTime.push(1.555);
					lastvalue0="暂无数据";
				}else if(info[1].frequency>100){
					lastTime.push(100);
					lastvalue0="100";
				}else{
					lastTime.push(info[1].frequency);
					lastvalue0 = info[1].frequency;
				}
				if(info[1].flicker===3.14159){
					lastTime.push(1.555);
					lastvalue1="暂无数据";
				}else if(info[1].flicker>100){
					lastTime.push(100);
					lastvalue1="100";
				}else{
					lastTime.push(info[1].flicker);
					lastvalue1 = info[1].flicker;
				}
				if(info[1].uBalance===3.14159){
					lastTime.push(1.555);
					lastvalue2="暂无数据";
				}else if(info[1].uBalance>100){
					lastTime.push(100);
					lastvalue2="100";
				}else{
					lastTime.push(info[1].uBalance);
					lastvalue2 = info[1].uBalance;
				}
				//获取谐波电压
				// var harmVoltage =getHarmVoltageValue(info[1].uAberrance,info[1].evenVoltage,info[1].oddVoltage);
				var harmVoltage = info[1].harmVoltage;
				if(harmVoltage===3.14159){
					lastTime.push(1.555);
					lastvalue3="暂无数据";
				}else if(harmVoltage>100){
					lastTime.push(100);
					lastvalue3="100";
				}else{
					lastTime.push(harmVoltage);
					lastvalue3 = harmVoltage;
				}

				if(info[1].voltageDeviation===3.14159){
					lastTime.push(1.555);
					lastvalue4="暂无数据";
				}else if(info[1].voltageDeviation>100){
					lastTime.push(100);
					lastvalue4="100";
				}else{
					lastTime.push(info[1].voltageDeviation);
					lastvalue4 = info[1].voltageDeviation;
				}
				//获取谐波电流
				// var harmElec=getHarmElecValue(info[1].oddElectric,info[1].evenElectric);
				var harmElec = info[1].harmElec;
				if(harmElec===3.14159){
					lastTime.push(1.555);
					lastvalue5="暂无数据";
				}else if(harmElec>100){
					lastTime.push(100);
					lastvalue5="100";
				}else{
					lastTime.push(harmElec);
					lastvalue5 = harmElec;
				}
				if(info[1].iNeg===3.14159){
					lastTime.push(1.555);
					lastvalue6="暂无数据";
				}else if(info[1].iNeg>100){
					lastTime.push(100);
					lastvalue6="100";
				}else{
					lastTime.push(info[1].iNeg);
					lastvalue6 = info[1].iNeg;
				}
				if(info[1].inUharm===3.14159){
					lastTime.push(1.555);
					lastvalue7="暂无数据";
				}else if(info[1].inUharm>100){
					lastTime.push(100);
					lastvalue7="100";
				}else{
					lastTime.push(info[1].inUharm);
					lastvalue7 = info[1].inUharm;
				}

				initWtzbTB(thisTime,lastTime,selector);
				document.getElementById("data1").innerText = thisvalue0+"/"+lastvalue0;
				document.getElementById("data2").innerText = thisvalue1+"/"+lastvalue1;
				document.getElementById("data3").innerText = thisvalue2+"/"+lastvalue2;
				document.getElementById("data4").innerText = thisvalue3+"/"+lastvalue3;
				document.getElementById("data5").innerText = thisvalue4+"/"+lastvalue4;
				document.getElementById("data6").innerText = thisvalue5+"/"+lastvalue5;
				document.getElementById("data7").innerText = thisvalue6+"/"+lastvalue6;
				document.getElementById("data8").innerText = thisvalue7+"/"+lastvalue7;
			}
			//***********关闭loading
			ityzl_CLOSE_LOAD_LAYER(i);
		}
	})
}

function initWtzbTB(thisTime,lastTime,selector) {
	$("#wtzb").css("height",picHeight);
	echarts.dispose($("#wtzb")[0]);
	var wtzbOption = echarts.init($("#wtzb")[0]);

	var max=100;

	var labels = ['频率偏差','闪变', '电压不平衡', '谐波电压',  '电压偏差', '谐波电流', '负序电流', '间谐波电压含有率'];
	var option = {
		backgroundColor: canvasBG,//背景色,
		title: {
			text: '稳态指标合格率('+selector+')',
			x: 'center'
		},
		grid:{
			left:'4%',
			right:'2%',
			bottom:'10%'
		},
		tooltip: {
			trigger: 'axis',
			axisPointer: {
				type: 'shadow'
			},
			formatter: function (params) {
				console.log(params)
				var value0=params[0].value;
				var value1=params[1].value;
				var tips="";
				if(value0===1.555){
					tips+=legend1+':'+'暂无数据'+'<br/>';
				}else{
					tips+=legend1+':'+value0+'%'+'<br/>';
				}
				if(value1===1.555){
					tips+=legend2+':'+'暂无数据';
				}else{
					tips+=legend2+':'+value1+'%';
				}
				return tips;
			}
		},
		legend: {
			data: [legend1,legend2],
			selectedMode: false,
			x: 'left'
		},
		toolbox: {
			show : true,
			feature : {
				saveAsImage : {show: true}
			},
			right:'2%'
		},
		xAxis: [{
			type: 'category',
			data: labels,
			splitLine: {
				show: false
			}
		}],
		yAxis: [{
			type: 'value',
			name: '%',
			max:max
		}],
		series: [{
			name: legend1,
			type: 'bar',
			data: thisTime,
			barMaxWidth: barMax
		},{
			name: legend2,
			type: 'bar',
			data: lastTime,
			barMaxWidth: barMax
		}],
		color:[yellow,blue]
	};

	wtzbOption.setOption(option);
}

function getHarmVoltageValue(uAberrance,oddVoltage,evenVoltage) {
	var value=uAberrance+oddVoltage+evenVoltage;
	value=value/3;
	return Number(value.toFixed(5));

}

function getHarmElecValue(oddElectric,evenElectric) {
	var value=oddElectric+evenElectric;
	value=value/2;
	return Number(value.toFixed(5));
}
