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

$.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 getQueryParams(){
	var sParam = {};
	var LIKEtaskname = $('#LIKEtaskname').textbox("getValue");
	var tasktype = $('#tasktype').combobox("getValue");
	var startruntime = $('#startruntime').datetimebox("getValue");	
	var endruntime = $('#endruntime').datetimebox("getValue");
	sParam.LIKEtaskname = LIKEtaskname;
	sParam.tasktype = tasktype;
	sParam.startruntime = startruntime;
	sParam.endruntime = endruntime;
	
	var tableparam = "";
	if(!(startruntime==null||startruntime==""||startruntime==undefined)){
		tableparam += " AND runtime >= '" +startruntime + "'";
	}
	if(!(endruntime==null||endruntime==""||endruntime==undefined)){
		tableparam += " AND runtime <= '" +endruntime + "'";
	}
	sParam.tableparam = tableparam;
	

	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!listywvideotaskcompleterate.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
			{field:'taskid',title:'任务id',hidden:true,width:120},
			{field:'taskname',title:'任务名称',hidden:false,width:130},
			{field:'tasktype',title:'任务类型',hidden:true,width:120},
			{field:'tasktypename',title:'任务类型',hidden:false,width:120},
			{field:'runtime',title:'巡检时间',hidden:false,width:130},
			{field:'sbzs',title:'任务设备量',hidden:false,width:90},
			{field:'sbdrzs',title:'完好率',hidden:false,width:250,
				formatter:function showClick(value,record,num){
					var tasktype = record.tasktype;
					var sbdrzs = 0;
					var onlinestotal = 0;
					var totalnormal = 0;
					var totalczzx = 0;
					var totallxczwh = 0;
					if (record.sbdrzs == null || record.sbdrzs == "") {
						return "";
					} else {
						sbdrzs = record.sbdrzs;
					}
					if (record.onlinestotal != null && record.onlinestotal != "") {
						onlinestotal = record.onlinestotal;
					}
					if (record.totalnormal != null && record.totalnormal != "") {
						totalnormal = record.totalnormal;
					}
					if (record.totalczzx != null && record.totalczzx != "") {
						totalczzx = record.totalczzx;
					}
					if (record.totallxczwh != null && record.totallxczwh != "") {
						totallxczwh = record.totallxczwh;
					}
					if (tasktype == 1) {
						var zxl = (totalczzx / sbdrzs) * 100;
						var whl = (totallxczwh / sbdrzs) * 100;
						return "视频在线率：" + parseFloat(zxl.toFixed(2)) + "%；抽帧完好率："+ parseFloat(whl.toFixed(2)) +"%";
					} else if (tasktype == 2) {
						var zxl = (onlinestotal / sbdrzs) * 100;
						var whl = (totalnormal / sbdrzs) * 100;
						return "视频在线率：" + parseFloat(zxl.toFixed(2)) + "%；视频完好率："+ parseFloat(whl.toFixed(2)) +"%";
					} else {
						var whl = (totallxczwh / sbdrzs) * 100;
						return "录像完好率：" + parseFloat(whl.toFixed(2)) +"%";
					}
				}
			},
			{field:'onlinestotal',title:'在线总数',hidden:true,width:120},
			{field:'totalnormal',title:'视频质量诊断正常数',hidden:true,width:120},
			{field:'totalczzx',title:'录像抽帧在线数',hidden:true,width:120},
			{field:'totallxczwh',title:'录像正常数',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 getQueryParamsForDetile(selRows) {
//	var selRows = $('#tableGrid').datagrid('getSelected');
	var sParam = {};
	sParam.runsetid = selRows.runsetid;
	sParam.tasktype = selRows.tasktype;
	return sParam;
}

//跳转详情
function lookdetail(index){
	displayType = "list";
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	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:'sbip',title:'设备IP',hidden:false,width:160},
			{field:'cjmc',title:'厂家名称',hidden:false,width:160},
			{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:'sbip',title:'设备IP',hidden:false,width:160},
			{field:'cjmc',title:'厂家名称',hidden:false,width:160},
			{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:'sbip',title:'设备IP',hidden:false,width:160},
			{field:'cjmc',title:'厂家名称',hidden:false,width:160},
//			{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);
			}
		}
	});
}
