var proName = window.location.pathname.split("/")[1];
var sysUrl = "http://"+window.location.host+"/"+proName;
var runsetid	= $.getUrlParam("runsetid");
console.log("videoQ--------------",runsetid)
$(function() {
	if(!runsetid){
		$("#LIKEruntimeStart").datetimebox("setValue",$.getTodayStart(0,"00:00:00"));
		$("#LIKEruntimeEnd").datetimebox("setValue",$.getTodayEnd("23:59:59"));
	}
	initSelect();
//	initXzNameTree();
});
$.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
         }
         var f = 0,
         l = [],
         e,
         d,
         j = this;
         for (; (g.data[f]); f++) {
//             g.data[f].oprStatusStr = g.data[f].oprStatus == 0 ? "正常": "异常";
             g.data[f].oprStatusColor = 'style="color:' + (g.data[f].oprStatus == 0 ? "blue;": "red;") + '"';
//             g.data[f].picturepath = sysUrl+"/sourceProxy?fileType=picUrlProxy&picUrl="+g.data[f].picturepath;

//             g.data[f].picturepath = $.getPicProxyUrl(g.data[f].picturepath);
//             l.push('<div class="view-item" style="width:300px;position:relative;"><div class="showAlarmType" style="top: -15px;right: 4px;width: 67px; color: white;z-index: 10; height: 40px;background: #1612263b;font-size: 14px;font-weight: bolder;position:absolute;text-align: center; line-height: 40px;">'+g.data[f].oprStatusStr+'</div>' + '<img  src="' + (g.data[f].picturepath) + '" onerror="javascript:$.loadErrorPic(this);" style="width:218px;"/><div>' + "<p>相机名称:&nbsp;" + (g.data[f].channelname || "无") + "</p>" + "<p>抓拍时间:&nbsp;" + (g.data[f].runtime || "") + "</p>" + "<p>告警类型:&nbsp;<span " + g.data[f].oprStatusColor + ">" + (g.data[f].oprStatusStr || "") + "</span></p>" + '<p><a href="javascript:void(0);" tag=' + g.data[f].id + ' class="ahref" onclick="oprStatus(this,0);" >标记正常</a> &nbsp;&nbsp;&nbsp; <a href="javascript:void(0);" tag=' + g.data[f].id + ' class="ahref" onclick="oprStatus(this,1);" >标记异常</a>&nbsp;&nbsp;&nbsp; <a href="javascript:void(0);" tag=' + g.data[f].id + ' class="ahref" onclick="oprStatus(this,2);" >重点关注</a></p>' + "</div></div>")
            if(g.data[f].videolabelstatusname==null){
            	l.push('<div class="view-item" style="width:220px;position:relative;">'
            			+' <div class="showAlarmType" style="top: -15px;right: 4px;width: 67px; color: white;z-index: 10; height: 40px;background: #1612263b;font-size: 14px;font-weight: bolder;position:absolute;text-align: center; line-height: 40px;display:none;">'+g.data[f].videolabelstatusname+'</div>' 
            			+ '<img onclick="detailVideoQuality(\''+g.data[f].id+'\');"  src="' + (g.data[f].picturepath) + '" onerror="javascript:$.loadErrorPic(this);" style="width:218px;"/><div style="width:218px;">' + "<p>相机名称:&nbsp;" + (g.data[f].channelname || "无") + "</p>" + "<p style='line-height:20px;'>抓拍时间:&nbsp;" + (g.data[f].runtime || "") + "</p>" + "<p style='line-height:20px;'>告警类型:&nbsp;<span " + g.data[f].oprStatusColor + ">" + (g.data[f].videostatusname || "") + "</span></p>" + labelstatusRender(g.data[f].id,g.data[f].videolabelstatusname) + "</div></div>")
            }else{
            	l.push('<div class="view-item" style="width:220px;position:relative;">'
            			+' <div class="showAlarmType" style="top: -15px;right: 4px;width: 67px; color: white;z-index: 10; height: 40px;background: #1612263b;font-size: 14px;font-weight: bolder;position:absolute;text-align: center; line-height: 40px;">'+g.data[f].videolabelstatusname+'</div>' 
            			+ '<img onclick="detailVideoQuality(\''+g.data[f].id+'\');" src="' + (g.data[f].picturepath) + '" onerror="javascript:$.loadErrorPic(this);" style="width:218px;"/><div style="width:218px;">' + "<p>相机名称:&nbsp;" + (g.data[f].channelname || "无") + "</p>" + "<p style='line-height:20px;'>抓拍时间:&nbsp;" + (g.data[f].runtime || "") + "</p>" + "<p style='line-height:20px;'>告警类型:&nbsp;<span " + g.data[f].oprStatusColor + ">" + (g.data[f].videostatusname || "") + "</span></p>" + labelstatusRender(g.data[f].id,g.data[f].videolabelstatusname) + "</div></div>")
            }
         }
         c.html('<div class="img-box">' + l.join("") + "</div>")
     }
});
//加载列表
function initGrid(data){
	var param = {};
	if(data){
		param = getQueryParams(data)
	}else{
		param = getQueryParams()
	}
	var toolbar = [{
        text:'查询',
        iconCls:'icon-search',
        handler: doSearch
    },{
        text:'单个批次查询',
        iconCls:'icon-search',
        handler: doRunsetSearch 
    }];
	$('#searchGrid').datagrid({
		toolbar : toolbar,
		bodyCls:'pic-datagrid',
		queryParams:param,
		url : 'ywvideotaskrunresultAction!listYwvideotaskrunresultByPage.do',
		onLoadSuccess:function(data){
			if(data){
				data.data=data.rows;
				data.totalSize=data.total;
				$('#searchGrid').datagrid("renderMonitorData",data);
			}
		},
		rownumbers:true,
	});
}

function doSearch() {
	$('#searchGrid').datagrid('load', getQueryParams());
}
function doRunsetSearch() {
	$('#searchGrid').datagrid('load', getQueryParams(runsetid));
}

function getQueryParams(data){
	var sParam = {};
	if(data){
		sParam.runsetid = data;
	}
	var xzbh = $('#LIKExzName').textbox("getValue");
	if(xzbh!="-1" || xzbh!=""){
		sParam.xzbh = $('#LIKExzName').textbox("getValue");
	}
	var LIKEruntimeStart = $('#LIKEruntimeStart').datetimebox("getValue");	
	var LIKEruntimeEnd = $('#LIKEruntimeEnd').datetimebox("getValue");	
	var tableparam = "";
	if(!(LIKEruntimeStart==null||LIKEruntimeStart==""||LIKEruntimeStart==undefined)){
		tableparam += " AND runtime > '" +LIKEruntimeStart + "'";
	}
	if(!(LIKEruntimeEnd==null||LIKEruntimeEnd==""||LIKEruntimeEnd==undefined)){
		tableparam += " AND runtime < '" +LIKEruntimeEnd + "'";
	}
	sParam.tableparam = tableparam;
	sParam.LIKEtaskname = $('#taskname').textbox("getValue"); 
	sParam.tasktype = $('#tasktype').textbox("getValue"); 
	sParam.runstatus = $('#runstatus').textbox("getValue"); 
	sParam.videolabelstatus = $('#videolabelstatus').textbox("getValue"); 
	sParam.videostatus = $('#videostatus').textbox("getValue"); 
	sParam.order = "order by runtime desc ";
	return sParam;
}
function oprStatus(obj,videolabelstatus) {
	var id = $(obj).attr("id");
	var m = {};
	m.XGTJid = id;
	var html=$(obj).html().trim();
	if(html=='取消关注'){
		m.videolabelstatus = "-1";
	}else{
		m.videolabelstatus = videolabelstatus;
	}
	$.ajax({
		url:'ywvideotaskrunresultAction!updateVideolabelStatus.do',
		type:"post",
		dataType:"JSON",
		data : m,
		success:function(res){
			if(res){
				$(obj).parents(".view-item").find(".showAlarmType").html(html);
				$(obj).parents(".view-item").find(".showAlarmType").css("display","block");
				if(videolabelstatus==2){
					if(html=='重点关注'){
						$(obj).html('取消关注');
					}else{
						$(obj).html('重点关注');
					}
				}
			}
		}
	});
}

function initSelect(){
	//加载下拉框
	$.ajax({
		url:" xtclAction!query_Xtcl_task.do",
		type:"post",
		dataType:"JSON",
		data : {"csdl":"ywVideoTaskType,ywTaskRunStatus,ywvideolabelstatus,ywDeviceStatus"},
		success:function(res){
			console.log(res);
			if(res && res.length>0){
				var taskTypeArr = [{"id":"","text":"全部","selected":true}];
				var taskStatuslArr = [{"id":"","text":"全部","selected":true}];
				var taskLabelStatuslArr = [{"id":"","text":"全部","selected":true}];
				var taskvideostatusArr = [{"id":"","text":"全部","selected":true}];
				for(i in res){
					if(res[i].csdl=="ywVideoTaskType"){
						taskTypeArr.push({"id":res[i].csdm,"text":res[i].csmc})
					}else if(res[i].csdl=="ywTaskRunStatus"){
						taskStatuslArr.push({"id":res[i].csdm,"text":res[i].csmc})
					}else if(res[i].csdl=="ywvideolabelstatus"){
						taskLabelStatuslArr.push({"id":res[i].csdm,"text":res[i].csmc})
					}else if(res[i].csdl=="ywDeviceStatus"){
						taskvideostatusArr.push({"id":res[i].csdm,"text":res[i].csmc})
					}
				}
				$("#tasktype").combobox( "loadData", taskTypeArr);
				$("#runstatus").combobox( "loadData", taskStatuslArr);
				$("#videolabelstatus").combobox( "loadData", taskLabelStatuslArr);
				$("#videostatus").combobox( "loadData", taskvideostatusArr);
				    
			}
		}
	});
	//加载行政树
	$.ajax({
		 type:'post',
		 async:false,
		 dataType : 'json',
		 //url:'ptxzsAction!query_Ptxzs.do'
		 url:'menuConfigAction!query_Ptxzs.do'
	 }).done(function(data){
		 console.log("",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		= "-1";
				xzfz.start 	= 0; //iconCls
		 	xzfz.xzbh = "-1";
		 	xzfz.xzmc = "全部分组";
		 	xzfz.state = "open";
		 	xzfz.treeId = "-1";
		 	xzfz.sjxzbh = "-1";
		 	xzfz.name = "全部分组";
			data.push(xzfz)
				 $('#LIKExzName').combotree({
				        checkbox:false,
				    	border:false,
				    	parentField:"pid",  
				    	textFiled:"name",  
				    	idFiled:"id",
				    	panelWidth:148,
				    	data:data,
						onLoadSuccess:function(node,data){
							if(data){
								//$('#LIKExzName').combotree("setValue",data[0].xzId).combotree("setText", data[0].xzName);
//								$('#LIKExzId').val(data[0].xzId);
//								$('#LIKExzName').val(data[0].xzName);
//								initDefault();
								initGrid(runsetid)
							}
						},
						onClick: function(node){
//							$('#xzbh').val(node.xzbh);
//							$('#xzmc').val(node.xzmc);
						}
				});
	 });
}
function labelstatusRender(id,name){
	var $p = ""
	$.ajax({
		url:" xtclAction!query_Xtcl_task.do",
		type:"post",
		dataType:"JSON",
		async:false,
		data : {"csdl":"ywvideolabelstatus"},
		success:function(res){
			if(res){
				$p += '<p>';
				for(i in res){
					var $a = "";
					if(name=="重点关注" && res[i].csdm=="2"){
						$a = "<a href=\"javascript:void(0);\" id=\""+id+"\"  tag=\""+id+"\" class=\"ahref\" onclick=\"oprStatus(this,'-1');\" >取消关注</a>&nbsp;&nbsp;&nbsp;";
					}else{
						$a = "<a href=\"javascript:void(0);\" id=\""+id+"\"  tag=\""+id+"\" class=\"ahref\" onclick=\"oprStatus(this,'"+res[i].csdm+"');\" >"+res[i].csmc+"</a>&nbsp;&nbsp;&nbsp;";
					}
					$p +=$a;
				}
				$p += "</p>";
			}
		}
	});
	return $p;
}

function detailVideoQuality(id){

	var url = top.sysUrl+"/yw/videoalarm/itemPage/detailVideoQuality.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);
					}
				}
		 }
	 });
}