var windowId,sysUrl = top.sysUrl,toEdit;
var displayType = "list";
var toolbar = [];
$(function(){
	initDefault();	
	
	// 初始化 pushlet
	aqmgjPushlet.init();
	
});
//默认
function initDefault(){
	/*
	$('#xx5').combobox({  
		url:'xtclAction!query_Xtcl.do?csdl=xx5&page=1&rows=1000', 
		textField:"csmc",  
		valueField:"csdm",
		panelWidth:148,
		loadFilter:function(data){
			//data.unshift({"csmc":"选择","csdm":""});
			return data;
		}
	});
	$("#xx6").textbox("setValue",records.xx6);
	$("#xx7").combobox("setValue",records.xx7);
	$("#xx8").combobox("setValue",records.xx8);
	$("#xx9").datetimebox("setValue",$.getNowTime());
	$("#xx10").datetimebox("setValue",$.getTodayStart('00:00:00',-3));//往前推3天  +'00:00:00'
	$("#xx11").datetimebox("setValue",$.getTodayEnd());//当天 +'23:59:59'	
	$("#xx12").datetimebox("setValue",$.getDateNumToStr(records.xx12));
	$("#xx13").numberbox("setValue",records.xx13);
	*/
	initGrid();
}
//预置点弹框消息关闭
function monitorPresetClose(){
	$("#monitorPreset").hide();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	var startTime = $('#startTime').datetimebox("getValue");	
	var endTime = $('#endTime').datetimebox("getValue");	
	sParam.startTime = startTime;
	sParam.endTime = endTime;

	/*
	sParam.LIKEbjxxid = $('#LIKEbjxxid').textbox("getValue");
	sParam.LIKEbjbh = $('#LIKEbjbh').textbox("getValue");
	sParam.LIKEfxbjbh = $('#LIKEfxbjbh').textbox("getValue");
	sParam.LIKEnbid = $('#LIKEnbid').textbox("getValue");
	sParam.LIKEfxsbbh = $('#LIKEfxsbbh').textbox("getValue");
	sParam.LIKEfxtdbh = $('#LIKEfxtdbh').textbox("getValue");
	sParam.LIKEfxsbip = $('#LIKEfxsbip').textbox("getValue");
	sParam.LIKEspyxtbh = $('#LIKEspyxtbh').textbox("getValue");
	sParam.LIKEspytdbh = $('#LIKEspytdbh').textbox("getValue");
	sParam.LIKEbjlx = $('#LIKEbjlx').textbox("getValue");
	
	*/
	/*
	var xx1 = $("#xx1").textbox("getValue");//文本
	var xx2 = $("#xx2").combobox("getValue");//下拉
	var xx3 = $("#xx3").datetimebox("getValue");//日期
	var xx4 = $("#xx4").numberbox("getValue");//数字
	var LIKEtime1 = $('#LIKEtime1').datetimebox("getValue");	
	var LIKEtime2 = $('#LIKEtime1').datetimebox("getValue");	
	var tableparam = "";
	if(!(LIKEtime1==null||LIKEtime1==""||LIKEtime1==undefined)){
		tableparam += " AND time > '" +LIKEtime1 + "'";
	}
	if(!(LIKEtime2==null||LIKEtime2==""||LIKEtime2==undefined)){
		tableparam += " AND time < '" +LIKEtime2 + "'";
	}
	sParam.tableparam = tableparam;
	sParam.order = "order by time desc ";
	*/
	sParam.order = "order by fwdsdksbjsj desc "
	return sParam;
}
//加载列表
function initGrid(){	
	toolbar = [
	{
    	text	:'查询',
        iconCls	:'icon-search',
        handler	:doSearch
    },{
		text:'普通列表',
		iconCls:'icon-showList',
		handler:showList
	},{
		text:'图片列表',
		iconCls:'icon-showPic',
		handler:showPicture
	}];
	showList();
}

// 列表展示
function showList(){
	$("#searchGridListDiv").show();
	$("#searchGridPicDiv").hide();
	$("#searchGridStaticsDiv").hide();
	displayType = "list";
	$('#searchGridList').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'gdwdaqmxxAction!listGdwdaqmxxByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
		    
			{field:'bjbh',title:'报警编号',hidden:true,width:120},
			{field:'fxbjbh',title:'分析报警编号',hidden:true,width:120},
			{field:'fxtdbm',title:'设备名称',hidden:false,width:130},
			{field:'fxsbip',title:'设备IP',hidden:false,width:120},
			{field:'bjzwmc',title:'报警类型',hidden:false,width:130},
			{field:'fxsbbh',title:'分析设备编号',hidden:true,width:120},
			{field:'fxtdbh',title:'分析通道编号',hidden:true,width:120},
			{field:'yzwbh',title:'预置位编号',hidden:false,width:120},
			{field:'fwdsdksbjsj',title:'告警开始时间',hidden:false,width:130},
			{field:'fwdsdjsbjsj',title:'告警结束时间',hidden:false,width:130},
			{field:'bjxxid',title:'操作',hidden:false,width:60, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='showDetail(\""+value+"\")'>详情</a>";
				}
 			}
		]]
	});
}

//图片展示
function showPicture(){
	$("#searchGridPicDiv").show();
	$("#searchGridListDiv").hide();
	$("#searchGridStaticsDiv").hide();
	displayType = "picture";
	$('#searchGridPic').datagrid({
		toolbar : toolbar,
		bodyCls:'pic-datagrid',
		queryParams:getQueryParams(),
		url : 'gdwdaqmxxAction!listGdwdaqmxxByPage.do',
		onLoadSuccess:function(data){
			if(data){
				data.data=data.rows;
				data.totalSize=data.total;
				$('#searchGridPic').datagrid("renderMonitorData",data);
			}
		},
		rownumbers:true,
	});	
}

$.extend($.fn.datagrid.methods,
		{
	renderMonitorData:function(n, g, h){
		 var m = n.parent(".datagrid-view:eq(0)"),
         c;
         var k = m.prev("div").append("<table cellspacing=0 cellpadding=0 style='display:inline-block;height:26px;float:right;'><tbody><tr/></tbody></table>").children("table:eq(1)").find("tbody tr");
         var b = m.parent().height();
         var o = m.parent().width();
         if (!m.next("div.imgcontainer").html()) {
             c = $("<div class='imgcontainer' style='overflow-y:auto; width:99%;height:" + (b - 70) + "px;'></div>");
             m.after(c)
         } else {
             c = m.next("div.imgcontainer")
         }
         if (!g) {
             return
         }
         if(g.total<1){
        	 c.html('<div style="text-align:center;margin:80px 0 0 0;font-size:12px;" class="img-box">没有查询到符合条件的记录!</div>');
        	 return ;
         }
         var f = 0,
         l = [],
         e,
         d,
         j = this;
         var proName = window.location.pathname.split("/")[1];
         var sysUrl = "http://"+window.location.host+"/"+proName;
         for (; (g.data[f]); f++) {
        	 var row = g.data[f];
             g.data[f].oprStatusColor = 'style="color:' + (g.data[f].oprStatus == 0 ? "blue;": "red;") + '"';            
             var picProxyGetUrl = sysUrl+"/sourceProxy?fileType=picUrlProxy&picUrl="+g.data[f].bjtplj;
    	l.push('<div class="view-item" style="width:220px;position:relative;">'
    			+ '<img onclick="showDetail(\''+g.data[f].bjxxid+'\');"  src="' + picProxyGetUrl + '" onerror="javascript:$.loadErrorPic(this);" style="width:218px;"/><div style="width:218px;">' + "<p>设备名称:&nbsp;" + (g.data[f].fxtdbm || "无") + "</p>" + "<p style='line-height:20px;'>设备IP:&nbsp;" + (g.data[f].fxsbip || "") + "</p>" + "<p style='line-height:20px;'>告警时间:&nbsp;" + (g.data[f].fwdsdksbjsj || "") + "</p></div></div>")
         }
         c.html('<div class="img-box">' + l.join("") + "</div>")
     }
});


//查询
function doSearch(){	
	//$('#tableGrid').datagrid('load',getQueryParams());
	if(displayType=="list"){
		showList();
	}else if(displayType=="picture"){
		showPicture();
	}
}


function showDetail(id){
	console.log("详情：",id);
	var url = sysUrl+"/yw/aqmjc/detail.jsp?bjxxid="+id;
	windowid = top.$.createWin({
		 title	: 	'详情',
		 width  :  '750',
		 height :  '460',
		 data:id,
		 url	:  url,
		  
	 });
}


