/**
 * 首页暂降事件
 */
$(document).ready(function () {
	$("#query").click(function(){
		parent.layer.closeAll();//关闭窗口
	})
	var i;
	$.ajax({
		type : "POST",
		url : "/pqs9200/area/getZjsj",
		dataType : "text",
		beforeSend: function () {  
            i = ityzl_SHOW_LOAD_LAYER();  
        },
		success : function(data) {
			//没有权限的请求
			if(data==300){
				location.href='/pqs9200/auth/unauthorized';
			//没有登陆的请求
			}else if(data==301){
				location.href='/pqs9200';
			}else{
				var info = $.parseJSON(data);
				/**
				 * 处理数据写表格
				 */
				var JSONObj = '';
				
				if (info.code == 500){
					JSONObj += '[]';
				}else{
					JSONObj += '[';
				}
				
				$.each(info.body,function(index,item){
					if(index + 1 == info.body.length){
						JSONObj += str2JSON(index + 1,item[0],item[1],item[2]
						,item[3],item[4],item[5]
						,item[6],item[7]) + ']';
						return false;
					}
					
					JSONObj += str2JSON(index + 1,item[0],item[1],item[2]
					,item[3],item[4],item[5]
					,item[6],item[7]) + ',';
				});
				
				initTable($.parseJSON(JSONObj));
			}
			//***********关闭loading
        	ityzl_CLOSE_LOAD_LAYER(i);
		}
	});
});
var table=null;
function getKfkaData(data){
	var tableData = table.cache[1];//获取表格数据
	var length = tableData.length;
	
	//处理kfka数据
	$.each(data,function(index,item){
		var obj = new Object();//定义新的对象按照格式存放数据
		obj.id = index;
		obj.name = item.pointname;
		obj.time = item.timeid + '.' + item.ms;
		obj.type = item.eventtype;
		obj.persisttime = item.persisttime;
		obj.eventvalue = item.eventvalue;
		obj.namegd = item.gdname;
		obj.namebd = item.bdname;
		
		tableData.unshift(obj);
		
		if(length == 100){
			tableData.pop();
		}
		
		obj = null; //销毁new出来的对象
	});
	
	//重新对id进行排序
	$.each(tableData,function(index,item){
		item.id = index + 1;
	});
	
	initTable(tableData);
}

/**
 * @param data
 * @returns
 * 初始化表格
 */
function initTable(data){
	layui.use('table', function(){
	  table = layui.table;
	  
	  //展示已知数据
	  table.render({
	    elem: '#sagEvents'
	    ,data: data
	    ,height: 170
	    ,cols: [[ //标题栏
	       {field: 'id', title: '序号', width: window.screen.width*0.08,align:'center'}
	      ,{field: 'name', title: '监测点', width: window.screen.width*0.15,align:'center'}
	      ,{field: 'time', title: '发生时刻', width: window.screen.width*0.16, sort: true,align:'center'}
	      ,{field: 'persisttime', title: '持续时间(s)', width: window.screen.width*0.11, sort: true,align:'center'}
	      ,{field: 'eventvalue', title: '暂降(骤升)幅值(%)', width: window.screen.width*0.11, sort: true,align:'center'}
	      ,{field: 'namegd', title: '供电公司', width: window.screen.width*0.13, sort: true,align:'center'}
	      ,{field: 'namebd', title: '变电站', width: window.screen.width*0.13, sort: true,align:'center'}
	      ,{field: 'type', title: '暂降类型', width: window.screen.width*0.12,align:'center'}
	    ]] 
	    ,skin: 'line,row' //表格风格
	    ,page: false //是否显示分页
	    ,limit: data.length,
	    even:true
	  });
	});
}

/**
 * @param str1
 * @param str2
 * @returns
 * string转JSON
 */
function str2JSON(str1,str2,str3,str4,str5,str6,str7,str8){
	var JSONstr = '{"id":"' + str1 + '","name":"' + str2 
	+ '","time":"' + str3 + '","persisttime":"' + str4
	+ '","eventvalue":"'+str5 + '","namegd":"' + str6 + '","namebd":"' + str7 + '","type":"' + str8
	+ '"}';
	return JSONstr;
}