var lineIndex=$("#lineIndex").val();
lineIndex=filterXSS(lineIndex);
var endTime=getEndTime();
var startTime=getStartTime("年份", endTime);

$(document).ready(function() {
	var height = ($(window).height() - 60);

	$("#time").css("height", height);
	$("#cause").css("height", height/2);
	$("#type").css("height", height/2);
	getEventTime(lineIndex,startTime,endTime);
	getStatistic(lineIndex,startTime,endTime);
});

$("#query").click(function (){
    var start = $("#startTime").val();
    var end = $("#endTime").val();
	getEventTime(lineIndex,start,end);
	getStatistic(lineIndex,start,end);
});

    function getEventTime(lineIndex,startTime,endTime) {
    	jQuery.ajax({
		type : 'POST',
		dataType : "json",
		data : {
			lineIndex:lineIndex,
			startTime:startTime,
			endTime:endTime
		},
		cache : false,
		url : '/pqs9200/device/eventTime',
		error : function() {},
		success : function(data) {
			Harmonic(data);
		}
	});
    }

function Harmonic(interval){
	var months = new Array();
	var handle = new Array();
	if (interval != null) {
		for (var i = 0; i < interval.length; i++) {
			if (interval[i].month == "01" | i == 0) {
				months[i] = interval[i].month + "\n(" + interval[i].year
						+ ")";
			} else {
				months[i] = interval[i].month;
			}
			handle[i] = interval[i].times;
		}
	}
	var option = {
		backgroundColor : canvasBG,//背景色,
		title : {
			text : '月份统计',
			x : 'center'
		},
        tooltip : {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            },
            formatter: function(params){
                var tips="";
                tips +='<font>月份:'+params[0].name+'</font><br/>';
                tips +='<font>暂降次数:'+params[0].data+'</font>';
                return tips;
            }
        },
        toolbox: {
        	show : false
        },
		color : [ orange ],
		legend : {
			data : [ '暂降次数' ],
			x : 'left'
		},
		grid: {
	        left: '3%',
	        bottom: '3%',
	        right:'7%',
	        containLabel: true
		},
		xAxis : [ {
			type : 'category',
			data : months,
			name : '月份',
			splitLine : {
				show : false
			},
			axisLabel : {
				show : true,
				interval : 0
			}
		} ],
		yAxis : [ {
			type : 'value',
			name : '次'
		} ],
		series : [ {
			name : '暂降次数',
			type : 'bar',
			barMaxWidth : barMax,
			itemStyle : {
				normal : {
					label : {
						show : true,
						textStyle : {
							label: {show: true, position: 'top'},
							color : purple
						}
					}
				}
			},
			data : handle
		} ]
	};
	
    var time = echarts.init(document.getElementById('time'));
    time.setOption(option);
    time.on('click', eConsole);
}

function eConsole(param) {    
	var data = param.name;
    if (typeof data == 'undefined') {    
        return;    
    }    
    if (param.type == 'click') {    
    	var start = startTime.substring(0,4) + "-" + data.substring(0,2) + "-" + "01";
    	var end = startTime.substring(0,4) + "-" + data.substring(0,2) + "-" + "31";
    	getStatistic(lineIndex,start,end);
    }   
} 

function getStatistic(lineIndex,startTime,endTime){
	$.ajax({
        type: "POST",
        url: "/pqs9200/device/eventStatistic",
        dataType: "json",
        data: {
        	lineIndex:lineIndex,
            startTime: startTime,
            endTime: endTime
        },
        success: function (data) {
            initCause(data.reasons);
            initType(data.types);
        }
    });
}

var causeImage, typeImage;
function initCause(reasons) {
    var reaArray = [];
    var valueArray = [];
    if (null === reasons) {
        reaArray = ['组合暂降', '变压器激磁', '短路故障', '大型感应电动机启动', '未明'];
        valueArray = [{name: '组合暂降', value: 0}, {name: '变压器激磁', value: 0}, {
            name: '短路故障',
            value: 0
        }, {name: '大型感应电动机启动', value: 0}, {name: '未明', value: 0}];
    } else {
        reasons.reverse();
        for (var i = 0; i < reasons.length; i++) {
            reaArray.push(reasons[i].reason);
            var causeValue = {};
            causeValue.name = reasons[i].reason;
            causeValue.value = reasons[i].times;
            valueArray.push(causeValue)
        }
    }
    var option = {
        backgroundColor: '#F9F9F9',
        animation: false,
        title: {
            text: '暂降原因',
            x: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            data: reaArray
        },
        series: [
            {
                name: '暂降原因',
                type: 'pie',
                radius: '70%',
                center: ['50%', '53%'],
                label: {
                    normal: {
                        show: false
                    },
                    emphasis: {
                        show: true
                    }
                },
                lableLine: {
                    normal: {
                        show: false
                    },
                    emphasis: {
                        show: true
                    }
                },
                data: valueArray,
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    echarts.dispose($("#cause")[0]);
    var cause = echarts.init($("#cause")[0]);
    cause.setOption(option);
    causeImage = cause.getDataURL();
}

function initType(types) {
    var typeArray = [];
    var valueArray = [];
    if (null === types) {
        typeArray = ['单相接地', '两相接地', '三相短路',  '相间故障', '未知'];
        valueArray = [{name: '单相接地', value: 0}, {name: '两相接地', value: 0}, {name: '三相短路', value: 0}, {
            name: '相间故障',
            value: 0
        }, {name: '未知', value: 0}];
    } else {
        for (var i = 0; i < types.length; i++) {
            typeArray.push(types[i].type)
            var typeValue = {};
            typeValue.name = types[i].type;
            typeValue.value = types[i].times;
            valueArray.push(typeValue)
        }
    }
    var option = {
        backgroundColor: '#F9F9F9',
        animation: false,
        title: {
            text: '暂降类型',
            x: 'center',
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            data: typeArray
        },
        series: [
            {
                name: '暂降类型',
                type: 'pie',
                radius: '70%',
                center: ['50%', '60%'],
                label: {
                    normal: {
                        show: false
                    },
                    emphasis: {
                        show: true
                    }
                },
                lableLine: {
                    normal: {
                        show: false
                    },
                    emphasis: {
                        show: true
                    }
                },
                data: valueArray,
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    echarts.dispose($("#type")[0]);
    var type = echarts.init($("#type")[0]);
    type.setOption(option);
    typeImage = type.getDataURL();
}