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(){
	initDefault();	
	initCacheStatus();
	initTreePanel();
});
//默认
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 getQueryParams(){
	var sParam = {};
//	if(id!="-1") {
//		sParam.xzbh = id;
//	}
	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 networkerror=0 ";
		}else if(chosed[i]=="3"){
			currentstate += " or isonline=0 ";
		}else if(chosed[i]=="4"){
			currentstate += " or vqcstate=0 ";
		}else if(chosed[i]=="5"){
			currentstate += " or vqdstate=0 ";
		}else if(chosed[i]=="6"){
			currentstate += " or recordstate=0 ";
		}
	}
	if (currentstate == "1") {
		sParam.tableparam = " and isonline=1 and networkerror=1 and vqcstate=1 and vqdstate=1 and recordstate=1 ";
	}else{
		if(currentstate!=""){
			sParam.tableparam = " and ("+currentstate.substr(3)+") ";
		}
	}

//	sParam.xzbh = $("#videoalarm").combobox("getValue");
	var videoalarm = $("#videoalarm").combotree("getValue");
	if(videoalarm!="-1") {
		sParam.xzbh = videoalarm;
	}

	sParam.order = "order by currentstatetime desc ";
	return sParam;
}
//加载列表
function initGrid(){	
	videoStatistics();
	var toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywvideocurrentstateAction!listYwvideocurrentstateByPage.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:false,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:'videoocclusionstatus',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:'imgtoodarkstatus',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:'imgclearstatus',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:'recordstate',title:'录像状态',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return transStatus(value+"");
				}
			},
			{field:'currentstateid',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(){
	videoStatistics();
	$('#tableGrid').datagrid('load',getQueryParams());
}
//统计
function videoStatistics(){
	$.ajax({
		url : "ywvideocurrentstateAction!videoCurrentStateStatistics.do",
		dataType : "JSON",
		type : "POST",
		data : {},//getQueryParams(),
		success : function(res){
			if(res!=null){
				$("#frontDeviceTotal").html(res.total);
				$("#normalFrontDeviceTotal").html(res.normalNum);
				$("#abnormalFrontDeviceTotal").html(res.total-res.normalNum);
				var data={
						"title":"设备故障类型占比分析",
						data:[
							{name:"正常:" + res.normalNum,value:res.normalNum,color:"#fe0000"},
							//{name:"网络异常:" + res.abnormalNetNum,value:res.abnormalNetNum,color:"#e16616"},
							{name:"设备不在线:" + res.abnormalOnlineNum,value:res.abnormalOnlineNum,color:"#32c99d"},
							{name:"抽帧异常:" + res.abnormalVQCNum,value:res.abnormalVQCNum,color:"#e36d6d"},
							{name:"质量诊断异常:" + res.abnormalVQDNum,value:res.abnormalVQDNum,color:"#754599"},
							{name:"录像异常:" + res.abnormalRecordNum,value:res.abnormalRecordNum,color:"#84ccc9"}
							]
				};		
				EchartsBase.pieChart({
					id:"deviceAnalysisGraphChart",
					data:data
				});
			}
			//console.log("resresres",res);
		}
	});
}
//视频播放
function doPlay(id){
//	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!id){
		$.showMsg('请先选择记录! ');
		return;
	}
	var url = sysUrl+"/yw/videoalarm/itemPage/detailVideoQuality.jsp";
	windowid = top.$.createWin({
		 title	: 	'详情',
		 width  :  '720',
		 height :  '400',
		 data:id,
		 url	:  url,
		 onClose:function(){
			 var win		=	$.getTopWindowById(windowid);
			 win.unloadWindow();
		 }
	 });
}
//初始化下拉框
function handleA(e){
	var event=e;
	//console.log("e")
	//e.stopPropagation();
	//e.preventDefault();
}
//初始化下拉框
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
			});
	});
}