var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
$(function(){
	$("#datetimeStart").datebox("setValue",$.getTodayStart(0,""));
	$("#datetimeEnd").datebox("setValue",$.getTodayEnd(""));
	initDefault();	
	initTreePanel();
	initSelect();
});
//默认
function initDefault(){
	
	initGrid();
}
function initSelect(){
	var ywRecordStatusArr = [
	    {"id":"","text":"全部"},
	    {"id":"1","text":"正常"},
	    {"id":"0","text":"异常"}];
	
	$("#recordstate").combobox( "loadData", ywRecordStatusArr);
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKEchannelname = $('#LIKEchannelname').textbox("getValue");
	sParam.LIKEvideonum = $('#LIKEvideonum').textbox("getValue");
	var xzbh = $("#xzbh").combotree("getValue");
	if(xzbh!="-1") {
		sParam.xzbh = xzbh;
	}
	sParam.recordstate = $("#recordstate").combotree("getValue");
	var datetimeStart = $('#datetimeStart').datebox("getValue");	
	var datetimeEnd = $('#datetimeEnd').datebox("getValue");
	var tableparam = "";
	if(!(datetimeStart==null||datetimeStart==""||datetimeStart==undefined)){
		tableparam += " AND countdate >='" +datetimeStart + "'";
	}
	if(!(datetimeEnd==null||datetimeEnd==""||datetimeEnd==undefined)){
		tableparam += " AND countdate <= '" +datetimeEnd + "'";
	}
	sParam.tableparam = tableparam;
	sParam.order = "order by countdate desc ";
	
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:false,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywrecordcurrentstateAction!listYwrecordhistorystateByPage.do',
		columns : [[
		    /*{
				field: 'A',
				checkbox:'false',
				width:30
			},*/
		    /*{field:'recordstateid',title:'recordstateid',hidden:true,width:120, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
 			},*/
			{field:'channelname',title:'设备名称',hidden:false,width:120},
			{field:'sbip',title:'设备IP',hidden:false,width:120},
			{field:'cjmc',title:'厂家名称',hidden:false,width:120},
			{field:'videocatalog',title:'所属区域',hidden:true,width:120},
			{field:'countdate',title:'检测日期',hidden:false,width:120},
			{field:'recordstate',title:'录像状态',hidden:false,width:60,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'twentyfourstate',title:'0',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'onestate',title:'1',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'twostate',title:'2',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'threestate',title:'3',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'fourstate',title:'4',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'fivestate',title:'5',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'sixstate',title:'6',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'sevenstate',title:'7',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'eightstate',title:'8',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'ninestate',title:'9',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'tenstate',title:'10',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'elevenstate',title:'11',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'twelvestate',title:'12',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'thirteenstate',title:'13',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'fourteenstate',title:'14',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'fifteenstate',title:'15',hidden:false,width:25,				
				formatter:function(value,record,num){ 
					return getBlock(value);
				}
			},
			{field:'sixteenstate',title:'16',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'seventeenstate',title:'17',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'eighteenstate',title:'18',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'nineteenstate',title:'19',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'twentystate',title:'20',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'twentyonestate',title:'21',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'twentytwostate',title:'22',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			{field:'twentythreestate',title:'23',hidden:false,width:25,
				formatter:function(value,record,num){ 
					return getBlock(value);
				}	
			},
			
			{field:'recordhisstateid',title:'操作',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					var videonum = record.videonum;			// 设备编号
					var channelnum = record.channelnum;		// 通道编号
        			return '<a href="javascript:;" style="background: #67c23a;color:white;padding: 2px,5px;" class="l-btn l-btn-small" onclick="jumpPage(\''+value+'\',\''+videonum+'\',\''+channelnum+'\')"  group="" id=""><span class="l-btn-left"><span class="l-btn-text">详情</span></span></a>';
				}
			},
		]]
	});
}
//查询
function doSearch(){	
	$('#tableGrid').datagrid('load',getQueryParams());
}

function getBlock(flag){		// 根据 状态码 1-正常  0-异常  返回红色或绿色块
	if(flag == 1){
		return "<div style='height:13px;width:13px;background-color:green;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
	}else if(flag == 0){
		return "<div style='height:13px;width:13px;background-color:red;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
	}else{
		return flag;
	}
}

function jumpPage(recordhisstateid, videonum, channelnum){			// 点击操作跳转页面
	windowId = top.$.createWin({
		title	: 	'详情',
		width	:  	790,
		height	: 	500,
		url	:	sysUrl+"/yw/history/record/ywrecordhistorystateinfo.jsp?recordhisstateid="+recordhisstateid+"&videonum="+videonum+"&channelnum="+channelnum
	 });	
}
function closeRecordVideo(){
	var win		=	$.getTopWindowById(windowId);
	win.closeRecordvideo();
}
//初始化行政树
function initTreePanel(){
	$.ajax({
		 type:'post',
		 async:false,
		 dataType : 'json',
		 url:'menuConfigAction!query_Ptxzs.do',
//		 url:'ptxzsAction!query_Ptxzs.do',
//		 data:{
//			 order : " order by jdpx asc "
//		 }
	 }).done(function(data){
		if(data!=null && data.length>0) {
			$.each(data, function (i, o) {
				o.state = 'closed';
				o.treeId = o.xzbh;
				o.name = o.xzmc;
				o.treeType = "xz";
				o.id = o.xzbh;
				o.pid = o.sjxzbh;
				o.start = 0; //iconCls
			})
		}
			var xzfz = {};
		 	xzfz.treeType 	= "xz";
			xzfz.id		= "-1";
			xzfz.pid		= "xzTree";
			xzfz.start 	= 0; //iconCls
		 	xzfz.xzbh = "-1";
		 	xzfz.xzmc = "行政分组";
		 	xzfz.state = "closed";
		 	xzfz.treeId = "-1";
		 	xzfz.sjxzbh = "xzTree";
		 	xzfz.name = "行政分组";
			data.push(xzfz);
			$('#xzbh').combotree({
				checkbox:false,
				border:false,
				parentField:"pid",
				textFiled:"name",
				idFiled:"id",
				panelWidth:148,
				data:data,
				onLoadSuccess:function(node,data){
					if(data){
						
					}
				},
				onClick: function(node){
					
				},
				data   : data
			});
	});
}