var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
var proName = window.location.pathname.split("/")[1];
var sysUrl = "http://"+window.location.host+"/"+proName;
//var id = $.getUrlParam("id");
var cacheStatusObj =  {};
var AllTree = null;
$(function(){
	$("#datetimeStart").datetimebox("setValue",$.getTodayStart(0,"00:00:00"));
	$("#datetimeEnd").datetimebox("setValue",$.getTodayEnd("23:59:59"));
	initDefault();	
	initCacheStatus();
	initTreePanel();
});
//默认
function initDefault(){
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKEchannelname = $("#LIKEchannelname").val();
	sParam.LIKEvideonum = $("#LIKEvideonum").val();
//	sParam.channelnum = $("#channelnum").val();
//	sParam.currentstate = $("#currentstate").combobox("getValue");
	var chosed = $('#currentstate').combobox('getValues');
	var currentstate = "";
	for(var i in chosed) {
		if(chosed[i]=="1"){
			currentstate = "1";
			break;
		}else if(chosed[i]=="2"){
			currentstate += " or a.networkerror=1 ";
		}else if(chosed[i]=="3"){
			currentstate += " or a.isonline=1 ";
		}else if(chosed[i]=="4"){
			currentstate += " or a.vqcstate=1 ";
		}else if(chosed[i]=="5"){
			currentstate += " or a.vqdstate=1 ";
		}else if(chosed[i]=="6"){
			currentstate += " or a.recordstate=1 ";
		}
	}
	if (currentstate == "1") {
		sParam.tableparam = " and a.isonline=1 and a.networkerror=1 and a.vqcstate=1 and a.vqdstate=1 and a.recordstate=1 ";
	}else{
		if(currentstate!=""){
			sParam.tableparam = " and ("+currentstate.substr(3)+") ";
		}
	}

	var datetimeStart = $('#datetimeStart').datetimebox("getValue");	
	var datetimeEnd = $('#datetimeEnd').datetimebox("getValue");
	var tableparam = "";
	if(!(datetimeStart==null||datetimeStart==""||datetimeStart==undefined)){
		tableparam += " AND a.currentstatetime >='" +datetimeStart + "'";
	}
	if(!(datetimeEnd==null||datetimeEnd==""||datetimeEnd==undefined)){
		tableparam += " AND a.currentstatetime <= '" +datetimeEnd + "'";
	}
	sParam.tableparam = tableparam;
//	sParam.xzbh = $("#videoalarm").combobox("getValue");
	var videoalarm = $("#videoalarm").combotree("getValue");
	if(videoalarm!="-1") {
		sParam.xzbh = videoalarm;
	}

	sParam.order = "order by a.currentstatetime,a.channelname desc ";
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywvideohistorystateAction!listYwvideohistorystateByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
 			{field:'channelname',title:'通道名称',hidden:false,width:120},
 			{field:'sbip',title:'设备IP',hidden:false,width:120},
 			{field:'cjmc',title:'厂家名称',hidden:false,width:120},
 			{field:'videostatus',title:'设备状态',hidden:true,width:90,
 				formatter:function showClick(value,record,num){
 					if (record.isonline == "0") {
 						return "设备不在线";
 					}
					if(value=="1"){
						return "正常";
					}else if(value=="101"){
						return "网络异常";
					}else if(value=="102"){
						return "设备不在线";
					}else if(value=="103"){
						return "抽帧异常";
					}else if(value=="104"){
						return "抽帧异常";
					}
				}
 			},
 			{field:'repairstate',title:'维修状态',hidden:true,width:90,
 				formatter:function showClick(value,record,num){
					if(value=="9"){
						return "设备维修";
					}else if(value=="900"){
						return "待处理";
					}else if(value=="901"){
						return "待受理";
					}else if(value=="902"){
						return "待反馈";
					}else if(value=="903"){
						return "已驳回";
					}else if(value=="904"){
						return "挂起";
					}else if(value=="905"){
						return "取消挂起";
					}else if(value=="906"){
						return "反馈待确认";
					}else if(value=="907"){
						return "反馈审核不通过";
					}else if(value=="908"){
						return "延期待审核";
					}else if(value=="909"){
						return "延期审核通过";
					}else if(value=="910"){
						return "延期审核不通过";
					}else if(value=="911"){
						return "维修确认通过";
					}else if(value=="912"){
						return "已忽略";
					}else if(value=="913"){
						return "维修确认不通过";
					}else if(value=="914"){
						return "解决告警";
					}
				}
 			},
			{field:'isonline',title:'是否在线',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return transStatus(value+"");
				}
			},
			{field:'vqcstate',title:'抽帧状态',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return transStatus(value+"");
				}
			},
//			{field:'vqdstate',title:'质量诊断状态',hidden:true,width:70,
//				formatter:function showClick(value,record,num){
//					return transStatus(value+"");
//				}
//			},
			{field:'nosignalstatus',title:'视频丢失',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return nosignalTransStatus(value+"");
				}
			},
			{field:'imgclearstatus',title:'图像模糊',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return transStatus(value+"");
				}
			},
			{field:'imgoverbrightstatus',title:'图像过亮',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return transStatus(value+"");
				}
			},
			{field:'imgtoodarkstatus',title:'图像过暗',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return transStatus(value+"");
				}
			},
			{field:'imgcolorstatus',title:'图像偏色',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return transStatus(value+"");
				}
			},
			{field:'videojitterstatus',title:'视频抖动',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return transStatus(value+"");
				}
			},{field:'videoupheavalstatus',title:'视频剧变',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return transStatus(value+"");
				}
			},
			{field:'scenechangestatus',title:'场景变更',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return transStatus(value+"");
				}
			},
			{field:'videoocclusionstatus',title:'视频遮挡',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return transStatus(value+"");
				}
			},
//			{field:'imgcontraststatus',title:'图像对比检测状态',hidden:true,width:120},
			{field:'recordstate',title:'录像状态',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return transStatus(value+"");
				}
			},
			{field:'currentstatetime',title:'检测时间',hidden:false,width:130},
			{field:'historystateid',title:'操作',hidden:false,width:50, 
				formatter:function showClick(value,record,num){ 
					return '<a href="javascript:;" style="background: #67c23a;color:white;padding: 2px,5px;" class="l-btn l-btn-small" onclick="doPlay(\''+value+'\')" group="" id=""><span class="l-btn-left"><span class="l-btn-text">详情</span></span></a>';
				}
 			}
		]],
		onLoadSuccess:function(data){
			$(".tip").tooltip({
                onShow: function(){
                	$(this).tooltip('tip').css({
                		width:'100',
                        boxShadow: '1px 1px 3px #292929'
                	});
                }
			})
		}
	});
}

//查询
function doSearch(){
	$('#tableGrid').datagrid('load',getQueryParams());
}

//视频播放
function doPlay(id){
//	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!id){
		$.showMsg('请先选择记录! ');
		return;
	}
	var url = sysUrl+"/yw/history/video/detailVideoHistory.jsp";
	windowid = top.$.createWin({
		 title	: 	'详情',
		 width  :  '720',
		 height :  '400',
		 data:id,
		 url	:  url,
		 onClose:function(){
			 var win		=	$.getTopWindowById(windowid);
			 for(var i = 0;i < win.BlockCacher.blockCache.length;i++){
					if(win.BlockCacher.blockCache[i].fdId && win.BlockCacher.blockCache[i].channelId != null){
						win.BlockUtil.stopMonitor(i);
					}
				}
		 }
	 });
}
//初始化下拉框
function handleA(e){
	var event=e;
}
function initCacheStatus(){
	var ywDeviceStatusArr = [];
	var videoStatus = Constants.videoStatus;
	for(var i in videoStatus){
		ywDeviceStatusArr.push({"id":i,"text":videoStatus[i]})
	}
	$("#currentstate").combobox( "loadData", ywDeviceStatusArr);
	$('#currentstate').combobox({
		formatter: function(row){
			var opts = $(this).combobox('options');
			return row[opts.textField];
		}
	});
	var firstShowPanel=true;
	var html=$("#_easyui_combobox_i1_0").html();
	var html1=$("#_easyui_combobox_i1_1").html();
	var html2=$("#_easyui_combobox_i1_2").html();
	var html3=$("#_easyui_combobox_i1_3").html();
	var html4=$("#_easyui_combobox_i1_4").html();
	var html5=$("#_easyui_combobox_i1_5").html();
	$("#_easyui_combobox_i1_1").after("<div onclick='handleA' class='handleCom' style='display:none;'>"+html1+"</div>");
	$("#_easyui_combobox_i1_2").after("<div onclick='handleA' class='handleCom' style='display:none;'>"+html2+"</div>");
	$("#_easyui_combobox_i1_3").after("<div onclick='handleA' class='handleCom' style='display:none;'>"+html3+"</div>");
	$("#_easyui_combobox_i1_4").after("<div onclick='handleA' class='handleCom' style='display:none;'>"+html4+"</div>");
	$("#_easyui_combobox_i1_5").after("<div onclick='handleA' class='handleCom' style='display:none;'>"+html5+"</div>");
	$("#_easyui_combobox_i1_0").after("<div onclick='handleA' class='handleCom1' style='display:none;'>"+html+"</div>");
	$('#currentstate').combobox({
		onUnselect:function(record){
			var opts = $(this).combobox('options');
			var values=$('#currentstate').combobox('getValues');
			if(record.id==1){
				$("#_easyui_combobox_i1_1").show();
				$("#_easyui_combobox_i1_2").show();
				$("#_easyui_combobox_i1_3").show();
				$("#_easyui_combobox_i1_4").show();
				$("#_easyui_combobox_i1_5").show();
				$(".handleCom").hide();
			}else{
				if(values.length<=1){
					var values=$('#currentstate').combobox('getValues');
					$(".handleCom1").hide();
					$("#_easyui_combobox_i1_0").show();
				}
			}
		},
		onChange:function(){
			var values=$('#currentstate').combobox('getValues');
		},
		onHidePanel:function(){
			selectedValues=$('#currentstate').combobox('getValues');
			firstShowPanel=false;
		},
		onShowPanel:function(){
			if(!firstShowPanel){
					if(selectedValues.indexOf("1")!=-1){
						$("#_easyui_combobox_i1_1").hide();
						$("#_easyui_combobox_i1_2").hide();
						$("#_easyui_combobox_i1_3").hide();
						$("#_easyui_combobox_i1_4").hide();
						$("#_easyui_combobox_i1_5").hide();
					}else{
						if(selectedValues.length>0){
							$("#_easyui_combobox_i1_0").hide();
						}
					}
			}
		},
		onSelect: function(record){
			var values=$('#currentstate').combobox('getValues');
			var opts = $(this).combobox('options');
			if(record.id==1){
				$("#_easyui_combobox_i1_1").hide();
				$("#_easyui_combobox_i1_2").hide();
				$("#_easyui_combobox_i1_3").hide();
				$("#_easyui_combobox_i1_4").hide();
				$("#_easyui_combobox_i1_5").hide();
				$(".handleCom").show();
			}else{
				$("#_easyui_combobox_i1_0").hide();
				$(".handleCom1").show();
			}
			return false;
		}
	});
}

function transStatus(data){
	if(data=="1"){
		return  "<div style='height:13px;width:13px;background-color:#60b631;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
	} else if (data=="0") {
		return "<div style='height:13px;width:13px;background-color:#fe0000;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
	}
	return "";
}

function nosignalTransStatus(data){
	if(data=="1"){
		return  "<div style='height:13px;width:13px;background-color:#60b631;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
	} else if(data=="2") {
		return "<div class='tip' title='码流中断' style='height:13px;width:13px;background-color:#ff7a05;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
	} else if(data=="3") {
		return "<div class='tip' title='帧率过低' style='height:13px;width:13px;background-color:#fe0000;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
	} else if(data=="4") {
		return "<div class='tip' title='丢包' style='height:13px;width:13px;background-color:#fe0000;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
	} else {
		return "";
	}
}

//初始化行政树
function initTreePanel(){
	$.ajax({
		 type:'post',
		 async:false,
		 dataType : 'json',
		 url:'menuConfigAction!query_Ptxzs.do',//dac 行政树
//		 url:'ptxzsAction!query_Ptxzs.do',
//		 data:{
//			 order : " order by jdpx asc "
//		 }
	 }).done(function(data){
		 $.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);
			$('#videoalarm').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
			});
	});
}