var picHeight,color;
var endTime=getEndTime();
var startTime=getStartTime("月份",endTime);
var lineIndex=window.parent.document.getElementById("lineIndex").value;
$("#interval").val("月份");
startTime=filterXSS(startTime);
endTime=filterXSS(endTime);
lineIndex=filterXSS(lineIndex);
window.parent.parent.thisUrl = "yxzt"
$(function () {
	getThemeColor();
	$("#startTime").val(startTime);
    picHeight=$(window).height()-40;

    getYxzt(startTime,endTime,lineIndex);
    getRun(lineIndex,startTime,endTime);
})

$("#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();
    getYxzt(startTime,endTime,lineIndex);
    getRun(lineIndex,startTime,endTime);
})

$("#tb").click(function (){
	document.getElementById("res").style.display="";
	var legend1,legend2;
	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+'月';
	}
	getYxztTB(startTime,endTime,laststartTime,lastendTime,lineIndex,legend1,legend2);
})

$("#hb").click(function (){
	document.getElementById("res").style.display="";
	var legend1,legend2;
	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) {
			legend1 =year+'年'+ month+'月';
			year = year-1;
			var laststartTime=year  + "-12-01";

			var lastendTime=year  + "-12-31";
			legend2 = year+'年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+'月';
		}
		getYxztTB(startTime,endTime,laststartTime,lastendTime,lineIndex,legend1,legend2);
	}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+'月';
		}
		getYxztTB(startTime,endTime,laststartTime,lastendTime,lineIndex,legend1,legend2);
	}
})

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

function getYxzt(startTime,endTime,lineIndex){
	$.ajax({
        url:'/pqs9000/device/yxzt',
        data:{
            startTime:startTime,
            endTime:endTime,
            lineIndex:lineIndex
        },
        type:'post',
        dataType:'json',
        success:function (data) {
        	var onlineRate=[]
        	var company=[];
        	if (data.code===500|data.body===null){
        		onlineRate.push(0);
        		company.push(0);
        		initYxzt(onlineRate,company);
            }else{
            	if(data.body[0]>100){
                	onlineRate.push(100);
            	}else{
                	onlineRate.push(data.body[0]);
                }
            	if(data.body[1]>100){
                	company.push(100);
            	}else{
                	company.push(data.body[1]);
                }
            	initYxzt(onlineRate,company);
            }
        }
    })
}

function getRun(lineIndex,startTime,endTime){
	var layii=0;
	$.ajax({
		type : 'POST',
		dataType : "json",
		cache : false,
		data:{
			lineIndex:lineIndex,
			startTime:startTime,
			endTime:endTime
		},
        beforeSend: function () {
            layii = ityzl_SHOW_LOAD_LAYER_PARENT();
        },
		url : '/pqs9000/device/runInfo',
		success : function(data) {
			var list = [];
            if (data.code===200){
                for( var i = 0; i < data.body.length; i++){
                    var point=[];
                    var xx=data.body[i].time;
                    var yy=data.body[i].sbzt;
                    point = [xx,yy];
                    list.push(point);
                }
                initRun(list);
			}else {

			}
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER_PARENT(layii);

		}
	});

}

function getYxztTB(startTime,endTime,laststartTime,lastendTime,lineIndex,legend1,legend2){
	$.ajax({
        url:'/pqs9000/device/yxztTB',
        data:{
            startTime:startTime,
            endTime:endTime,
            laststartTime:laststartTime,
            lastendTime:lastendTime,
            lineIndex:lineIndex
        },
        type:'post',
        dataType:'json',
        success:function (data) {
        	var onlineRate=[];
        	var company=[];
        	if (data.code===500|data.body===null){
        		onlineRate.push(0);
        		company.push(0);
        		onlineRate.push(0);
        		company.push(0);
        		initYxztTB(onlineRate,company,legend1,legend2);
            }else{
            	if(data.body[0]>100){
                	onlineRate.push(100);
            	}else{
                	onlineRate.push(data.body[0]);
                }
            	if(data.body[2]>100){
                	onlineRate.push(100);
            	}else{
                	onlineRate.push(data.body[2]);
                }
            	if(data.body[1]>100){
                	company.push(100);
            	}else{
                	company.push(data.body[1]);
                }
            	if(data.body[3]>100){
                	company.push(100);
            	}else{
                	company.push(data.body[3]);
                }

            	initYxztTB(onlineRate,company,legend1,legend2);
            }
        }
    })
}

function initYxzt(onlineRate,company) {
	$("#yxzt").css("height",picHeight);
    echarts.dispose($("#yxzt")[0]);
    var yxztOption = echarts.init($("#yxzt")[0]);
    var lineName = $(window.parent.document).find("#lineName").val();

	var option = {
	        backgroundColor: canvasBG,//背景色,
	        legend: {
	            data: ['在线率','数据完整性'],
	            selectedMode: false,
	            x: 'left'
	        },
	        toolbox: {
	            show : true,
	            feature : {
	                saveAsImage : {
	                	name: '运行状态统计',
	                	show: true
	                	}
	            },
	            right:'5%'
	        },
	        xAxis: [{
	            type: 'category',
	            data: [lineName],
	            splitLine: {
	                show: false
	            }
	        }],
	        yAxis: [{
	            type: 'value',
	            max:'100',
	            name: '%'
	        }],
	        series: [{
	            name: '在线率',
	            type: 'bar',
	            data: onlineRate,
	            itemStyle: {
	            	normal: {
	            		label: {show: true, position: 'top'}
	            	}
	            },
	            barMaxWidth: barMax
	        },{
	            name: '数据完整性',
	            type: 'bar',
	            data: company,
	            itemStyle: {
	            	normal: {
	            		label: {show: true, position: 'top'}
	            	}
	            },
	            barMaxWidth: barMax
	        }],
			color:[blue,yellow]
	    };

		yxztOption.setOption(option);
}

function initRun(list) {
	var interval ;
	if(list.length > 2){
		interval = 1;
	}else{
		interval = 0;
	}
	$("#sszt").css('height',picHeight);
	Highcharts.setOptions({ global: { useUTC: false } });
	Highcharts.stockChart('sszt', {
        title: {
            text: '运行状态',
            floating:true,
            style:{fontSize: "14px"}
        },
        rangeSelector:{
            enabled:false,
        },
		chart:{
            backgroundColor:canvasBG
		},
		//导出选中背景色
		navigation: {
			menuItemHoverStyle: {
				background: color,
				color: "#ffffff"
			},
		},
        //导航器各项属性设置
        navigator: {
            margin: 1,
            xAxis: {
                labels: {
                    format: '{value:%Y-%m-%d %H:%M}'
                }
            },
            series: [{
                data: list
            }]
        },
        scrollbar : {
            enabled : false
        },
        plotOptions: {
            line: {
                dataGrouping: {
                    enabled: false
                }
            }
        },
		exporting:{
          enabled:true,
          filename:'运行状态'
		},
        credits:false,
        colors:[red],

        tooltip: {
			trigger : 'item',
            borderColor:grey,
            backgroundColor:'rgba(105,105,105,0.7)',
            style:{
                color:'white',
                fontSize: "15px",
                padding: 10,
            },
            formatter: function () {
				var s = '<b>时间：' + Highcharts.dateFormat('%Y-%m-%d %H:%M', this.x) + '</b>';
				var y=this.y;
				var yd;
				if (y===0){
                    yd='中断';
				}else if (y===-1){
                    yd='退出';
				}else{
                    yd='正常';
				}
                $.each(this.points, function () {
                    s += '<br/>' + this.series.name + ': ' +yd;
                });
                return s;
            },
            shared: true
        },
        xAxis:{
//            labels: {
//                format: '{value:%m-%d %H:%M}'
//              }
        	tickInterval: interval,
            type: 'datetime',
            dateTimeLabelFormats: {
                second: '%H:%M:%S',
                minute: '%H:%M',
                hour: '%H:%M',
                day: '%m-%d',
                week: '%m-%d',
                month: '%Y-%m',
                year: '%Y'
            }
        },
        yAxis: {
            opposite:false,
            allowDecimals:false,
			max:1.1,
			min:-1,
			labels:{
                formatter:function () {
                    var y=this.value;
                    var yd;
                    if (y===0){
                        yd='中断';
                    }else if (y===-1){
                        yd='退出';
                    }else{
                        yd='正常';
                    }
                    return yd;
                }
			}
        },
        legend: {
            align: 'left',
            verticalAlign: 'top',
            enabled:true
        },
        series: [{
            type:'line',
            turboThreshold:0,
            lineWidth:1,
            marker: {
                symbol: 'circle'
            },
            name: '运行状态',
            data: list
        }
        ]
    });
}

function initYxztTB(onlineRate,company,legend1,legend2) {
	$("#yxzt").css("height",picHeight);
    echarts.dispose($("#yxzt")[0]);
    var yxztOption = echarts.init($("#yxzt")[0]);

	var option = {
	        backgroundColor: canvasBG,//背景色,
	        legend: {
	            data: ['在线率','数据完整性'],
	            selectedMode: false,
	            x: 'left'
	        },
	        toolbox: {
	            show : true,
	            feature : {
	                saveAsImage : {show: true}
	            },
	            right:'2%'
	        },
	        xAxis: [{
	            type: 'category',
	            data: [legend1,legend2],
	            splitLine: {
	                show: false
	            }
	        }],
	        yAxis: [{
	            type: 'value',
	            max:'100',
	            name: '%'
	        }],
	        series: [{
	            name: '在线率',
	            type: 'bar',
	            data: onlineRate,
	            itemStyle: {
	            	normal: {
	            		label: {show: true, position: 'top'}
	            	}
	            },
	            barMaxWidth: barMax
	        },{
	            name: '数据完整性',
	            type: 'bar',
	            data: company,
	            itemStyle: {
	            	normal: {
	            		label: {show: true, position: 'top'}
	            	}
	            },
	            barMaxWidth: barMax
	        }],
			color:[blue,yellow]
	    };

		yxztOption.setOption(option);
}

//获取主题颜色
function getThemeColor() {
	$.ajax({
		type: "POST",
		url: "/pqs9000/theme/getThemeInfo",
		async: false,
		dataType: 'json',
		success: function (data) {
			color = data.body.value;
		}
	})
}
