var lineIndex=window.parent.document.getElementById("lineIndex").value;
var lineName=window.parent.document.getElementById("lineName").value;
var table=null;
$(document).ready(function() {
	i=ityzl_SHOW_LOAD_LAYER();  
	var endTime=getEndTime();
	var startTime=getStartTime("年份", endTime);
	load(lineIndex,startTime,endTime);
    //***********关闭loading
	ityzl_CLOSE_LOAD_LAYER(i); 
});

function load(lineIndex,startTime,endTime){
	var titleheight=parent.document.getElementById("myTab").clientHeight;
	var headform=document.getElementById("headform").clientHeight;
	var yxdInfo=document.getElementById("yxdInfo").clientHeight;
	var picHeight=$(parent.document.body).height()-titleheight-headform-yxdInfo-115;
	var lengtha;
	lengtha = Math.floor(picHeight/31.5);
	$.ajax({
		method:"post",
		url:"/pqs9200/device/analyze",
		data:{
			lineIndex:lineIndex,
			startTime:startTime,
			endTime:endTime
		},
		beforeSend: function () {  
            i = ityzl_SHOW_LOAD_LAYER();  
        },
        error : function() {
    		layer.msg('无暂降事件',{icon:2,time:1000});
    	},
		success: function (data) {
			if(data == null){
	    		layer.msg('暂降事件加载失败',{icon:2,time:1000});
			}else{
				var info = JSON.parse(data);
				 if ($('#analyze').hasClass('DataTable')) {
					 $('#analyze').DataTable().fnClearTable();
					 $('#analyze').DataTable().fnDestroy();
				 }
				 table = $("#analyze").DataTable();
				 table.clear().draw();
				 table=$("#analyze").DataTable({
					scrollX : true,
					lengthChange : false,
					destroy : true,
					searching: true,
					bAutoWidth: false,
			        language: {
			            url: '/pqs9200/json/chine.json'
			        },
					data : info,
					pageLength:lengtha,
				    "columnDefs" : [ {
				        "targets": 1,
				        "visible": false,
				        "searchable": false
				      },{
					        "targets": 9,
					        "visible": false,
					        "searchable": false
					  },{
					    // 定义操作列,######以下是重点########
				    	"targets" : 10,//操作按钮目标列
					    	"data" : null,
					    	"orderable" : false,
				    	"render" : function(row) {
							return null;
				    	}
					    }],
				    "createdRow": function ( row, data, index ) {
				    	if(data[8]<=1){
				    		$('td', row).eq(7).css('color','#22B14C')
				    	}else if(data[8]<=2){
				    		$('td', row).eq(7).css('color','#3F48CC')
				    	}else if(data[8]<=5){
				    		$('td', row).eq(7).css('color','#FFF200')
				    	}else if(data[8]<=10){
				    		$('td', row).eq(7).css('color','#FF7F27')
				    	}else{
				    		$('td', row).eq(7).css('color','#ED1C24')
				    	}
				    	if(data[9]=="0"){
					    	$btnView = $('<input type="button" class="btn themeBGDisableColor" id="btn_view" value="暂无波形"></input>');
					    	$('td', row).eq(8).append($btnView);
				    	}else{
				    		$btnView = $('<input type="button" class="btn themeBGColor btn-view" id="btn_view" value="波形分析"></input>');
					    	$btnExp = $('<input type="button" class="btn themeBGColor  btn_exp" id="btn_exp" style="margin-left:10px" value="数据导出"></input>');
					    	$('td', row).eq(8).append($btnView,$btnExp);
				    	}
				    },
				    fnDrawCallback: function(table) { 
					    $("#analyze_paginate").append("<div style='float:right;padding-top:4px;padding-left:10px'>到第 <input type='text' id='changePage' class='input-text' style='width:50px;height:28px;'> 页  <button class='btn btn-primary' id='dataTable-btn'>确定</button></div>");    
					    var oTable = $("#analyze").dataTable();    
					    $('#dataTable-btn').click(function(e) {
					        if($("#changePage").val() && $("#changePage").val() > 0) {    
					            var redirectpage = $("#changePage").val() - 1;    
					        } else {    
					            var redirectpage = 0;    
					        }    
					        oTable.fnPageChange(redirectpage);
					    });    
					}
				});
				new $.fn.dataTable.Buttons($("#analyze").dataTable(), {
					buttons: [
						 {
					            extend: 'csv',//导出格式
					            title:lineName+'暂降事件列表',//导出csv名称
					            exportOptions:{ 
						        	columns :[0,2,3,4,5,6,7,8],//导出需要的列
						        	format:{
						        		body:function(data,rID,cID){//对导出内容的筛选 data 表格内容 rid 行索引 cid 列索引
						        			if(cID==0){
						        				return rID+1;
						        			}else if(cID==1){
						        				return data.replace("0","O");
						        			}else{
						        				return data;
						        			}
						        		}
						        	}
						        }
					     }
			        ]
				} );
				
				table.on('order.dt search.dt',
					    function() {
							table.column(0, {
					            search: 'applied',
					            order: 'applied'
					        }).nodes().each(function(cell, i) {
					            cell.innerHTML = i + 1;
					        });
			   }).draw();

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

function modifyCss(dataTable) {
    dataTable.buttons().container().css("display", "inline-block");
    dataTable.buttons().container().css("float", "right");
    dataTable.buttons().container().css("top", "4px");
    dataTable.buttons().container().find("a").prepend("<i class='glyphicon glyphicon-share'></i>");
    dataTable.buttons().container().appendTo('.header');
    dataTable.buttons().container().after($("#fliteData")[0]);
}

	var line_index = window.parent.document.getElementById("lineIndex").value;

	$('#analyze').on("click", ".btn-view", function() {
		objTable=$('#analyze').DataTable();
		if (objTable != null) {
			var item = objTable.row($(this).closest('tr')).data();
			showbximg(item[1]);
		}
	});    
	
	$("#btn_deal").click(function (){
		window.top.addTab('relevance','事件关联分析处理');
	});  
	
	$("#helper").click(function (){
		toHelper("#zjyzd");
	});  
		
	$('#analyze').on("click", ".btn_exp", function() {
		objTable=$('#analyze').DataTable();
		if (objTable != null) {
			var item = objTable.row($(this).closest('tr')).data();
			var eventIndex = item[1];
 			$.ajax({
 				type:'POST',
                dataType:"text",
 				data : {
 					eventIndex:eventIndex
 				},
 				url : '/pqs9200/device/getZIPFile',
 				success : function(data) {
 					var info=$.parseJSON(data);
                    if(info.code===500){
                    	layer.msg('下载失败！',{icon:2,time:1000});
                    }else{
                    	window.location.href='/pqs9200/device/downZIPFile?eventIndex='+eventIndex;
                    }
 				}
 			})
		}
	}); 
	
	function showbximg(eventIndex){
		layer.open({
	        type: 2,
	        title: '波形分析',
	        maxmin : false,
	        shadeClose: true, //点击遮罩关闭层
	        area : ['100%' , '100%'],
	        content: 'vdwaveform?eventIndex='+eventIndex,
		  	 scrollbar: false
	      });
	}

	$("#query").click(function (){
		var lineIndex=window.parent.document.getElementById("lineIndex").value;
		var startTime = $("#startTime").val();
		var endTime = $("#endTime").val();
		
		load(lineIndex,startTime,endTime);
	})
	
function yzdMouseOver(){
	var divinfo= document.getElementById("yzdfx");
	divinfo.style.display = "";
}

function yzdMouseOut(){
	var divinfo= document.getElementById("yzdfx"); 
	divinfo.style.display = "none";
}