var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
var displayType = "VideotaskrunresultList";
var toolbar = [];
$(function(){
	initSelect();
	$("#startTime").datetimebox("setValue",$.getTodayStart(0,"00:00:00"));
	$("#endTime").datetimebox("setValue",$.getTodayEnd("23:59:59"));
	initDefault();
});

$.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++) {
        	 var proName = window.location.pathname.split("/")[1];
        	 var sysUrl = "http://"+window.location.host+"/"+proName;
        	 var picProxyGetUrl = sysUrl+"/sourceProxy?fileType=picUrlProxy&picUrl="+g.data[f].picturepath;
             g.data[f].oprStatusColor = 'style="color:' + (g.data[f].videostatus == 1 ? "blue;": "red;") + '"';
            	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 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>")
            			+ '<img src="' + (picProxyGetUrl) + '" 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 + ">" + statustsf(g.data[f].videostatus) + "</span></p></div></div>")
         }
         c.html('<div class="img-box">' + l.join("") + "</div>")
     }
});

function statustsf(data) {
	if (data == "101") {
		return "网络异常";
	} else if(data == "102") {
		return "设备不在线";
	} else if(data == "103") {
		return "抽帧异常";
	} else if(data == "104") {
		return "抽帧异常";
	} else if(data == "107") {
		return "录像异常";
	} else if(data == "0") {
		return "异常";
	} else if(data == "1") {
		return "正常";
	}
}

//默认
function initDefault(){
	initGrid();
}

function getQueryParamsForDetile(selRows) {
//	var selRows = $('#tableGrid').datagrid('getSelected');
	var sParam = {};
	sParam.runsetid = selRows.runsetid;
	sParam.tasktype = selRows.tasktype;
	return sParam;
}

//查询条件
function getQueryParams(){
	var sParam = {};
	var tableparam = "";
	var startTime = $('#startTime').datetimebox("getValue");
	var endTime = $('#endTime').datetimebox("getValue");
	if(!(startTime==null||startTime==""||startTime==undefined)){
		tableparam += " AND runtime >= '" +startTime + "'";
	}
	if(!(endTime==null||endTime==""||endTime==undefined)){
		tableparam += " AND runtime <= '" +endTime + "'";
	}
	sParam.tableparam = tableparam;
//	sParam.runstatus = $('#runstatus').combobox("getValue");
	sParam.tasktype = $('#tasktype').combobox("getValue");
	sParam.LIKEtaskname = $('#LIKEtaskname').textbox("getValue");
	sParam.order = "order by runtime desc ";
	return sParam;
}
//加载列表
function initGrid(){
	toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    }];

	showVideotaskrunresult();
}

function showVideotaskrunresult() {
	var displayType = "VideotaskrunresultList";
	$("#searchGridDiv").show();
	$("#searchGridListDiv").hide();
	$("#searchGridPicDiv").hide();
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywvideotaskrunsetAction!listYwvideotaskrunsetByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
//		    {field:'runsetid',title:'runsetid',hidden:true,width:120},
			{field:'taskid',title:'任务id',hidden:true,width:120},
			{field:'taskname',title:'任务名称',hidden:false,width:120},
			{field:'tasktype',title:'任务类型',hidden:true,width:120},
			{field:'tasktypename',title:'任务类型',hidden:false,width:120},
			{field:'channelsetid',title:'通道集合id',hidden:true,width:120},
			{field:'runstatus',title:'执行状态',hidden:true,width:120},
			{field:'runstatusname',title:'执行状态',hidden:false,width:120},
			{field:'runprogress',title:'执行进度',hidden:false,width:120,
				formatter:function showClick(value,record,num){
					value = value==null?'0':value;
					return '<div class="progress"><div class="progress-value"  style="background-color:#56ABE4;width: ' + (value || 0) + 'px;"><div class="progress-text">' + (value+"%" || "") + "</div></div></div>"
				}
			},
			{field:'runtime',title:'巡检时间',hidden:false,width:130},
			{field:'createaccount',title:'创建人员',hidden:true,width:120},
			{field:'createtime',title:'创建日期',hidden:true,width:120},
			{field:'modifyaccount',title:'修改人员',hidden:true,width:120},
			{field:'modifytime',title:'修改日期',hidden:true,width:120},
			{field:'remarks',title:'备注',hidden:true,width:120},
			{field:'extend1',title:'扩展1',hidden:true,width:120},
			{field:'extend2',title:'扩展2',hidden:true,width:120},
			{field:'extend3',title:'扩展3',hidden:true,width:120},
			{field:'extend4',title:'扩展4',hidden:true,width:120},
			{field:'runsetid',title:'操作',hidden:false,width:70,
				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="lookdetail('+num+')"  group="" id=""><span class="l-btn-left"><span class="l-btn-text">详情</span></span></a>';
				}
 			}
		]]
	});
}

//跳转详情
function lookdetail(index){
	displayType = "list";
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
//	top.window.MainPage.menuClick('200010020' ,'http://localhost:8888/impyw/yw/videoalarm/ywvideotaskrunset/ywvideotaskrunDetail.jsp?runsetid='+selRows.runsetid ,'视频质量诊断');
//	top.window.MainPage.menuClick('200010020' ,'http://localhost:8080/impyw/yw/videoalarm/videoQuality.jsp?runsetid='+selRows.runsetid ,'任务运行详情概览');
//	top.window.MainPage.menuClick('200010020' ,'http://localhost:8080/impyw/yw/videoalarm/ywvideotaskrunset/ywvideotaskrunresulthistory.jsp?runsetid='+selRows.runsetid ,'任务运行详情概览');
	toolbar = [{
		text:'普通列表',
		iconCls:'icon-showList',
		handler: function() {showList(selRows)}
	},{
		text:'图片列表',
		iconCls:'icon-showPic',
		handler:function() {showPicture(selRows)}
	},{
		text : '返回',
		iconCls : 'icon-undo',
		handler : doSearch
	}];
	showList(selRows);
}

function showListColumns(selRows) {
//	tasktype
	var columns;
	if (selRows.tasktype == "1") {
		// 视频画面抽帧
		columns = [[
			{
				field: 'A',
				checkbox:'true',
				width:30
			},
			{field:'id',title:'id',hidden:true,width:120,
				formatter:function showClick(value,record,num){
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
			},
			{field:'taskname',title:'任务名称',hidden:false,width:120},
//			{field:'tasktypename',title:'任务类型',hidden:false,width:120},
			{field:'channelname',title:'设备名称',hidden:false,width:120},
			{field:'videonum',title:'设备编号',hidden:false,width:160,
				formatter: function(value,row,index) {
					return row.videonum + "-" + row.channelnum;
				}
			},
			{field:'isonlinename',title:'是否在线',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					if (record.videostatus == '102') {
						return transStatus("0");
					} else {
						return transStatus("1");
					}
				}
			},
			{field:'videostatus',title:'设备状态',hidden:false,width:60,
				formatter:function showClick(value,record,num){ 
					return videostatusStatus(value+"", num);
				}
			},
			{field:'failurecause',title:'故障原因',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					var status = record.videostatus + "";
					if (value == "" || value == null){
						if(status=="1"){
							return  "";
						}
						if (status == "101") {
							return "网络异常";
						} else if(status == "102") {
							return "设备不在线";
						} else if(status == "103") {
							return "抽帧异常";
						} else if(status == "104") {
							return "抽帧异常";
						} else if(status == "107") {
							return "录像异常";
						}
					} else {
						return value+"";
					}
				}
			},
			{field:'runtime',title:'巡检时间',hidden:false,width:130}
		]];
	} else if (selRows.tasktype == "2") {
		// 视频质量诊断
		columns = [[
			{
				field: 'A',
				checkbox:'true',
				width:30
			},
			{field:'id',title:'id',hidden:true,width:120,
				formatter:function showClick(value,record,num){
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
			},
			{field:'taskname',title:'任务名称',hidden:false,width:120},
//			{field:'tasktypename',title:'任务类型',hidden:false,width:120},
			{field:'channelname',title:'设备名称',hidden:false,width:120},
			{field:'videonum',title:'设备编号',hidden:false,width:160,
				formatter: function(value,row,index) {
					return row.videonum + "-" + row.channelnum;
				}
			},
			{field:'isonlinename',title:'是否在线',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					if (record.nosignalstatus == '2') {
						return transStatus("0");
					} else if (record.nosignalstatus == '99') {
						return transStatus("0");
					} {
						return transStatus("1");
					}
				}
			},
			{field:'nosignalstatus',title:'视频丢帧',hidden:false,width:60,
				formatter:function showClick(value,record,num){ 
					return nosignalStatustsf(value+"");
				}
			},
//			{field:'failurecause',title:'故障原因',hidden:false,width:120},
			{field:'videoocclusionstatus',title:'视频遮挡',hidden:false,width:60,
				formatter:function showClick(value,record,num){ 
					if (value == 99) {
						return "";
					}
					return transStatus(value+"");
				}
			},
			{field:'scenechangestatus',title:'场景变更',hidden:false,width:60,
				formatter:function showClick(value,record,num){ 
					if (value == 99) {
						return "";
					}
					return transStatus(value+"");
				}
			},
			{field:'imgtoodarkstatus',title:'图像过暗',hidden:false,width:60,
				formatter:function showClick(value,record,num){ 
					if (value == 99) {
						return "";
					}
					return transStatus(value+"");
				}
			},
			{field:'imgoverbrightstatus',title:'图像过亮',hidden:false,width:60,
				formatter:function showClick(value,record,num){ 
					if (value == 99) {
						return "";
					}
					return transStatus(value+"");
				}
			},
			{field:'imgclearstatus',title:'图像模糊',hidden:false,width:60,
				formatter:function showClick(value,record,num){ 
					if (value == 99) {
						return "";
					}
					return transStatus(value+"");
				}
			},
			{field:'imgcolorstatus',title:'图像偏色',hidden:false,width:60,
				formatter:function showClick(value,record,num){ 
					if (value == 99) {
						return "";
					}
					return transStatus(value+"");
				}
			},
			{field:'runtime',title:'巡检时间',hidden:false,width:130}
		]];
	} else if (selRows.tasktype == "10") {
		// 录像诊断
		columns = [[
			{
				field: 'A',
				checkbox:'true',
				width:30
			},
			{field:'id',title:'id',hidden:true,width:120,
				formatter:function showClick(value,record,num){
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
			},
			{field:'taskname',title:'任务名称',hidden:false,width:120},
//			{field:'tasktypename',title:'任务类型',hidden:false,width:120},
			{field:'channelname',title:'设备名称',hidden:false,width:120},
			{field:'videonum',title:'设备编号',hidden:false,width:160,
				formatter: function(value,row,index) {
					return row.videonum + "-" + row.channelnum;
				}
			},
//			{field:'isonlinename',title:'是否在线',hidden:false,width:60,
//				formatter:function showClick(value,record,num){
//					if (record.videostatus == '102') {
//						return transStatus("0");
//					} else {
//						return transStatus("1");
//					}
//				}
//			},
			{field:'videostatus',title:'录像状态',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return videostatusStatus(value+"", num);
				}
			},
			{field:'failurecause',title:'故障原因',hidden:false,width:230,
				formatter:function showClick(value,record,num){ 
					var status = record.videostatus + "";
					if (value == "" || value == null){
						if(status=="1"){
							return  "";
						}
						if (status == "101") {
							return "网络异常";
						} else if(status == "102") {
							return "设备不在线";
						} else if(status == "103") {
							return "抽帧异常";
						} else if(status == "104") {
							return "抽帧异常";
						} else if(status == "107") {
							return "录像异常";
						}
					} else {
						return value+"";
					}
				}
			},
			{field:'runtime',title:'巡检时间',hidden:false,width:130}
		]];
	}
	return columns;
}

function showList(selRows) {
	$("#searchGridDiv").hide();
	$("#searchGridListDiv").show();
	$("#searchGridPicDiv").hide();
	displayType = "list";
	$("#searchGridList").datagrid({
		toolbar : toolbar,
		singleSelect:true,
		queryParams:getQueryParamsForDetile(selRows),
		url : 'ywvideotaskrunresulthistoryAction!listYwvideotaskrunresulthistoryByPage.do',
		columns : showListColumns(selRows),
		onLoadSuccess:function(data){
			$(".tip").tooltip({
                onShow: function(){
                	$(this).tooltip('tip').css({
                		width:'100',
                        boxShadow: '1px 1px 3px #292929'
                	});
                }
			})
		}
	});
}

function showPicture(selRows) {
	$("#searchGridDiv").hide();
	$("#searchGridListDiv").hide();
	$("#searchGridPicDiv").show();
	displayType = "picture";
	$('#searchGridPic').datagrid({
		toolbar : toolbar,
		bodyCls:'pic-datagrid',
		queryParams:getQueryParamsForDetile(selRows),
		url : 'ywvideotaskrunresulthistoryAction!listYwvideotaskrunresulthistoryByPage.do',
		onLoadSuccess:function(data){
			if(data){
				data.data=data.rows;
				data.totalSize=data.total;
				$('#searchGridPic').datagrid("renderMonitorData",data);
			}
		},
		rownumbers:true,
	});
}

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>";
	}
	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>";
}

function videostatusStatus(data, index) {
	if(data=="1"){
		return  "<div style='height:15px;width:15px;background-color:#60b631;border-radius:50%;-moz-border-radius: 50%;-webkit-border-radius: 50%;margin:0 auto;'></div>";
	}
	if (data == "101") {
		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 == "102") {
		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 == "103") {
		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 == "104") {
		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 == "107") {
		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>";
	}
}

function nosignalStatustsf(data, index) {
	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>";
	}
	if (data == "2") {
		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 == "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 if(data == "99") {
		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 == "0") {
		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>";
	}
}

//查询
function doSearch(){	
	var displayType = "VideotaskrunresultList";
	$("#searchGridDiv").show();
	$("#searchGridListDiv").hide();
	$("#searchGridPicDiv").hide();
	$('#tableGrid').datagrid('load',getQueryParams());
}

function initSelect(){
	//加载下拉框
	$.ajax({
		url:" xtclAction!query_Xtcl_task.do",
		type:"post",
		dataType:"JSON",
		async:false,
		data : {"csdl":"ywVideoTaskType,ywTaskRunStatus"},
		success:function(res){
			if(res && res.length>0){
				var taskTypeArr = [{"id":"","text":"全部","selected":true}];
//				var taskRunStatusArr = [];
				for(i in res){
					if(res[i].csdl=="ywVideoTaskType"){
//						if(res[i].csdm=="1"){
//							taskTypeArr.push({"id":res[i].csdm,"text":res[i].csmc,"selected":true})
//						}else{
//							taskTypeArr.push({"id":res[i].csdm,"text":res[i].csmc})
//						}
						taskTypeArr.push({"id":res[i].csdm,"text":res[i].csmc})
					}else{
//						if(res[i].csdm=="1"){
//							taskRunStatusArr.push({"id":res[i].csdm,"text":res[i].csmc,"selected":true})
//						}else{
//							taskRunStatusArr.push({"id":res[i].csdm,"text":res[i].csmc})
//						}
					}
				}
				$("#tasktype").combobox( "loadData", taskTypeArr);
//				$("#runstatus").combobox("loadData", taskRunStatusArr);
			}
		}
	});
}