var ITICImage,F47Image,imageName;
var picHeight;
var piWidth;
$(document).ready(function() {
	var height = $("body").height();
	piWidth = $("body").width()-550;
    picHeight = height - 60;
	var lineIndex=$("#lineIndex").val();
    lineIndex=filterXSS(lineIndex);
	var endTime=getEndTime();
	var startTime=getStartTime("年份", endTime);

	getEventData(lineIndex,startTime,endTime);
});
		
    function getEventData(lineIndex,startTime,endTime) {
    	jQuery.ajax({
		type : 'POST',
		dataType : "json",
		data : {
			lineIndex:lineIndex,
			startTime:startTime,
			endTime:endTime
		},
		cache : false,
		url : '/pqs9200/device/eventdatalist',
		error : function() {},
		success : function(data) {
			var pointI = [];
			var pointIun = [];
			var pointF = [];
			var pointFun = [];
			
			var top = new Array();
			top[0] = [0.001,200];
			top[1] = [0.003,140];
			top[2] = [0.003,120];
			top[3] = [0.5,120];
			top[4] = [0.5,110];
			top[5] = [10,110];
			top[6] = [1000,110];
			
			var bot = new Array();
			bot[0] = [0.02,0];
			bot[1] = [0.02,70];
			bot[2] = [0.5,70];
			bot[3] = [0.5,80];
			bot[4] = [10,80];
			bot[5] = [10,90];
			bot[6] = [1000,90];
			
			var part = new Array();
			part[0] = [0.05, 0];
			part[1] = [0.05, 50];
			part[2] = [0.2, 50];
			part[3] = [0.2, 70];
			part[4] = [0.5, 70];
			part[5] = [0.5, 80];
			part[6] = [10, 80];
			part[7] = [1000, 80];
			
			var sum1=0;
			var sum2=0;
			var sum3=0;
			var sum4=0;
			var sum5=0;
			var sum=[];
			
			var standI=0;
			var unstandI=0;
			
			var standF=0;
			var unstandF=0;
			
			var total=0;
			total = data.length;
			if(total == 0){
				
			}else{
			for ( var i = 0; i < data.length; i++) {
				var point=[];
				var xx=data[i].persisttime;
				var yy=data[i].eventvalue;
				var time=data[i].time;
				var index=data[i].eventDetailIndex;
				point = [xx,yy,time,index];
				
				if(xx<=0.003){
					var line=0;
					line = 230-30000*xx;
					if(yy>line){
						unstandI++;
						pointIun.push({value:point,itemStyle:{normal:{color:red}}});
					}else{
						standI++;
						pointI.push({value:point,itemStyle:{normal:{color:green}}});
					}
				}else if(xx<=0.02){
					if(yy>120){
						unstandI++;
						pointIun.push({value:point,itemStyle:{normal:{color:red}}});
					}else{
						standI++;
						pointI.push({value:point,itemStyle:{normal:{color:green}}});
					}
				}else if(xx<=0.5){
					if(yy>120 || yy<70){
						unstandI++;
						pointIun.push({value:point,itemStyle:{normal:{color:red}}});
					}else{
						standI++;
						pointI.push({value:point,itemStyle:{normal:{color:green}}});
					}
				}else if(xx<=10){
					if(yy>110 || yy<80){
						unstandI++;
						pointIun.push({value:point,itemStyle:{normal:{color:red}}});
					}else{
						standI++;
						pointI.push({value:point,itemStyle:{normal:{color:green}}});
					}
				}else{
					if(yy>110 || yy<90){
						unstandI++;
						pointIun.push({value:point,itemStyle:{normal:{color:red}}});
					}else{
						standI++;
						pointI.push({value:point,itemStyle:{normal:{color:green}}});
					}
				}
				
				if(xx<0.05){
					standF++;
					pointF.push({value:point,itemStyle:{normal:{color:green}}});
				}else if(xx<0.2){
					if(yy>50){
						standF++;
						pointF.push({value:point,itemStyle:{normal:{color:green}}});
					}else{
						unstandF++;
						pointFun.push({value:point,itemStyle:{normal:{color:red}}});
					}
				}else if(xx<0.5){
					if(yy>70){
						standF++;
						pointF.push({value:point,itemStyle:{normal:{color:green}}});
					}else{
						unstandF++;
						pointFun.push({value:point,itemStyle:{normal:{color:red}}});
					}
				}else{
					if(yy>80){
						standF++;
						pointF.push({value:point,itemStyle:{normal:{color:green}}});
					}else{
						unstandF++;
						pointFun.push({value:point,itemStyle:{normal:{color:red}}});
					}
				}
			}
			}
			
			
			document.getElementById("totalI").innerText = total;
			document.getElementById("standI").innerText = standI;
			document.getElementById("unstandI").innerText = unstandI;
			
			document.getElementById("totalF").innerText = total;
			document.getElementById("standF").innerText = standF;
			document.getElementById("unstandF").innerText = unstandF;
			
			Harmonic(top,bot,pointI,pointIun,part,pointF,pointFun,total,standI,unstandI,standF,unstandF);
		}
	});
    }

function Harmonic(top,bot,pointI,pointIun,part,pointF,pointFun,total,standI,unstandI,standF,unstandF){
    $("#event1").css("height", picHeight);
	var option = {
	        backgroundColor: canvasBG,//背景色,
	        title: {
	            text: 'ITIC曲线',
	            x: 'center'
	        },
	        grid:{
	        	left:'4%',
	        	right:'2%',
	        	bottom:'10%'
	        },
	        toolbox: {
	            show: true,
	            feature: {
	            	saveAsImage : {show: true},
	                restore : {
						show : true
					},
					dataZoom : {
						show : true,
						yAxisIndex:false
					}
	            }
	        },
			tooltip : {
				trigger : 'item',
	            formatter:function(a)
	            {
	    	        var relVal = "";
	    	        relVal = '<font style=\'color:'+white+'\'>发生时间:'+a.value[2]+'</font><br/>';
	    	        relVal +='<font style=\'color:'+white+'\'>持续时间:'+ a.value[0]+'s</font><br/>';
	    	        relVal += '<font style=\'color:'+white+'\'>特征幅值:'+a.value[1]+'%</font>';
	    	        return relVal;
	            }
			},
	        legend: {
	            data: ['上限', '下限', '可容忍事件','不可容忍事件'],
	            selectedMode: false,
	            x: 'left'
	        },
	        color:[ITICTop,ITICBottom,green,red],
	        xAxis: [{
	            type: 'log',
	            min: '0.001',
	            max: '1000',
	            splitLine: {
	                show: false
	            },
	            name: 's'
	        }],
	        yAxis: [{
	            type: 'value',
	            splitNumber: 10,
	            minInterval: 3,
	            name: '%'
	        }],
	        series: [
	            {
	                name: '上限',
	                type: 'line',
	                data: top,
	                showSymbol: false,
	                tooltips:{
	                	show:false
	                }
	            },
	            {
	                name: '下限',
	                type: 'line',
	                data: bot,
	                showSymbol: false,
	                tooltips:{
	                	show:false
	                }
	            },
	            {
	                name: '可容忍事件',
	                type: 'scatter',
	                symbol : 'circle',
	                data: pointI
	            },
	            {
	                name: '不可容忍事件',
	                type: 'scatter',
	                symbol : 'circle',
	                data: pointIun
	            }
	        ]
	    };
	
	$("#event2").css("height", picHeight);
	var option1 = {
	        backgroundColor: canvasBG,//背景色,
	        title: {
	            text: 'SEMI F47曲线',
	            x: 'center'
	        },
	        grid:{
	        	left:'4%',
	        	right:'2%',
	        	bottom:'10%'
	        },
	        tooltip: {
	            trigger: 'item',
	            showDelay: 0, 
	            axisPointer: {
	                type: 'shadow'
	            },
	            formatter:function(a)
	            {
	            	var relVal = "";
	     	        relVal = '<font style=\'color:'+white+'\'>发生时间:'+a.value[2]+'</font><br/>';
	     	        relVal +='<font style=\'color:'+white+'\'>持续时间:'+ a.value[0]+'s</font><br/>';
	     	        relVal += '<font style=\'color:'+white+'\'>特征幅值:'+a.value[1]+'%</font>';
	    	        return relVal;
	            }
	        },
	        toolbox: {
	            show: true,
	            feature: {
	            	saveAsImage : {show: true},
	                restore : {
						show : true
					},
					dataZoom : {
						show : true,
						yAxisIndex:false
					}
	            }
	        },
	        legend: {
	            data: ['分割线', '可容忍事件','不可容忍事件'],
	            selectedMode: false,
	            x: 'left'
	        },
	        color:[ITICTop,green,red],
	        xAxis: [{
	            type: 'log',
	            min: '0.001',
	            max: '1000',
	            splitLine: {
	                show: false
	            },
	            name: 's'
	        }],
	        yAxis: [{
	            type: 'value',
	            max: function(value) {
	                return value.max + 20;
	            },
	            splitNumber: 10,
	            minInterval: 0.1,
	            name: '%'
	        }],
	        series: [
	            {
	                name: '分割线',
	                type: 'line',
	                data: part,
	                showSymbol: false,
	                tooltips:{
	                	show:false
	                }
	            },
	            {
	                name: '可容忍事件',
	                type: 'scatter',
	                symbol : 'circle',
	                data: pointF
	            },
	            {
	                name: '不可容忍事件',
	                type: 'scatter',
	                symbol : 'circle',
	                data: pointFun
	            }
	        ]
	    };
    	
        var event1 = echarts.init(document.getElementById('event1'));
        event1.setOption(option);
        event1.on('click', eConsole);
        ITICImage=event1.getDataURL();

        var event2 = echarts.init(document.getElementById('event2'));
        event2.setOption(option1);
        event2.on('click', eConsole);
        F47Image=event1.getDataURL();

    	$("#chart1").css("margin-top", 0-picHeight);
    	$("#chart1").css("margin-left", piWidth);

    	$("#chart2").css("margin-top", 0-picHeight);
    	$("#chart2").css("margin-left", piWidth);

}

function eConsole(param) {    
	var data = param.value;
    if (typeof data[3] == 'undefined') {    
        return;    
    }    
    if (param.type == 'click') {    
    	parent.layer.open({
            type: 2,
            title: '波形分析',
            maxmin : false,
            shadeClose: true, //点击遮罩关闭层
            area : ['100%' , '100%'],
            content: 'vdwaveform?eventIndex='+data[3],
    	  	 scrollbar: false
          });
    }   
} 

/**	
 * 根据传递的图片数据，在画布上画图
 * name:图片名
 */
function drawOnCanvas(imageOne, imageTwo,name) {
    var canvas = document.getElementById("canvasipal");
    var Gc = canvas.getContext("2d");
    var drawImg = new Image();
    drawImg.src = imageOne;
    drawImg.onload = function () {
        draw(this);
    };
    function draw(obj) {
        Gc.drawImage(obj, 0, 0, 2000, 450);
    };
    var drawImg1 = new Image();
    drawImg1.src = imageTwo;
    drawImg1.onload = function () {
        draw1(this);
    };
    function draw1(obj) {
        Gc.drawImage(obj, 0,450, 2000, 450);
        var type = 'png';
        canvasToImageIpal(name,type);
    };
    
}


//点击保存图片
$('#saveImage').on('click',function(){
	imageName = '事件点图';
	drawOnCanvas(ITICImage, F47Image,imageName);
});