var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
var flag = $.getUrlParam("flag");
var id = $.getUrlParam("id");
var cacheStatusObj =  {};
$(function(){
	initCacheStatus();
	initDefault();	
});
//默认
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 = {};
	/*
	sParam.LIKEid = $('#LIKEid').textbox("getValue");
	sParam.LIKErunsetid = $('#LIKErunsetid').textbox("getValue");
	sParam.LIKEtaskid = $('#LIKEtaskid').textbox("getValue");
	sParam.LIKEtaskname = $('#LIKEtaskname').textbox("getValue");
	sParam.LIKEtasktype = $('#LIKEtasktype').textbox("getValue");
	sParam.LIKEchannelsetid = $('#LIKEchannelsetid').textbox("getValue");
	sParam.LIKEvideonum = $('#LIKEvideonum').textbox("getValue");
	sParam.LIKEchannelnum = $('#LIKEchannelnum').textbox("getValue");
	sParam.LIKEchannelname = $('#LIKEchannelname').textbox("getValue");
	sParam.LIKEpicturepath = $('#LIKEpicturepath').textbox("getValue");
	sParam.LIKEpicturebyte = $('#LIKEpicturebyte').textbox("getValue");
	sParam.LIKEruntime = $('#LIKEruntime').textbox("getValue");
	sParam.LIKErunstatus = $('#LIKErunstatus').textbox("getValue");
	sParam.LIKEisonline = $('#LIKEisonline').textbox("getValue");
	sParam.LIKEplayfail = $('#LIKEplayfail').textbox("getValue");
	sParam.LIKEnetworkerror = $('#LIKEnetworkerror').textbox("getValue");
	sParam.LIKEnosignalvalue = $('#LIKEnosignalvalue').textbox("getValue");
	sParam.LIKEnosignalstatus = $('#LIKEnosignalstatus').textbox("getValue");
	sParam.LIKEimgclearvalue = $('#LIKEimgclearvalue').textbox("getValue");
	sParam.LIKEimgclearstatus = $('#LIKEimgclearstatus').textbox("getValue");
	sParam.LIKEimgcontrastvalue = $('#LIKEimgcontrastvalue').textbox("getValue");
	sParam.LIKEimgcontraststatus = $('#LIKEimgcontraststatus').textbox("getValue");
	sParam.LIKEimgcolorvalue = $('#LIKEimgcolorvalue').textbox("getValue");
	sParam.LIKEimgcolorstatus = $('#LIKEimgcolorstatus').textbox("getValue");
	sParam.LIKEimglightvalue = $('#LIKEimglightvalue').textbox("getValue");
	sParam.LIKEimglightstatus = $('#LIKEimglightstatus').textbox("getValue");
	sParam.LIKEvideofreezevallue = $('#LIKEvideofreezevallue').textbox("getValue");
	sParam.LIKEvideofreezestatus = $('#LIKEvideofreezestatus').textbox("getValue");
	sParam.LIKEvideojittervalue = $('#LIKEvideojittervalue').textbox("getValue");
	sParam.LIKEvideojitterstatus = $('#LIKEvideojitterstatus').textbox("getValue");
	sParam.LIKEvideoupheavalvalue = $('#LIKEvideoupheavalvalue').textbox("getValue");
	sParam.LIKEvideoupheavalstatus = $('#LIKEvideoupheavalstatus').textbox("getValue");
	sParam.LIKEvideocodeerrorvalue = $('#LIKEvideocodeerrorvalue').textbox("getValue");
	sParam.LIKEvideocodeerrorstatus = $('#LIKEvideocodeerrorstatus').textbox("getValue");
	sParam.LIKEvideonoisevalue = $('#LIKEvideonoisevalue').textbox("getValue");
	sParam.LIKEvideonoisestatus = $('#LIKEvideonoisestatus').textbox("getValue");
	sParam.LIKEvideorollstripevalue = $('#LIKEvideorollstripevalue').textbox("getValue");
	sParam.LIKEvideorollstripestatus = $('#LIKEvideorollstripestatus').textbox("getValue");
	sParam.LIKEvideostatus = $('#LIKEvideostatus').textbox("getValue");
	sParam.LIKEfailurecause = $('#LIKEfailurecause').textbox("getValue");
	sParam.LIKEcreateaccount = $('#LIKEcreateaccount').textbox("getValue");
	sParam.LIKEcreatetime = $('#LIKEcreatetime').textbox("getValue");
	sParam.LIKEmodifyaccount = $('#LIKEmodifyaccount').textbox("getValue");
	sParam.LIKEmodifytime = $('#LIKEmodifytime').textbox("getValue");
	sParam.LIKEremarks = $('#LIKEremarks').textbox("getValue");
	sParam.LIKEextend1 = $('#LIKEextend1').textbox("getValue");
	sParam.LIKEextend2 = $('#LIKEextend2').textbox("getValue");
	sParam.LIKEextend3 = $('#LIKEextend3').textbox("getValue");
	sParam.LIKEextend4 = $('#LIKEextend4').textbox("getValue");

	*/
	/*
	var xx1 = $("#xx1").textbox("getValue");//文本
	var xx2 = $("#xx2").combobox("getValue");//下拉
	var xx3 = $("#xx3").datetimebox("getValue");//日期
	var xx4 = $("#xx4").numberbox("getValue");//数字
	var LIKEtime1 = $('#LIKEtime1').datetimebox("getValue");	
	var LIKEtime2 = $('#LIKEtime1').datetimebox("getValue");	
	var tableparam = "";
	if(!(LIKEtime1==null||LIKEtime1==""||LIKEtime1==undefined)){
		tableparam += " AND time > '" +LIKEtime1 + "'";
	}
	if(!(LIKEtime2==null||LIKEtime2==""||LIKEtime2==undefined)){
		tableparam += " AND time < '" +LIKEtime2 + "'";
	}
	sParam.tableparam = tableparam;
	sParam.order = "order by time desc ";
	*/
	if(flag=="xzGroup"){
		if(id!="-1") {
			sParam.xzbh = id;
		}
	}else if(flag=="rwGroup"){
		if(id!="-3"){
			sParam.taskid = id;
		}
	}
	sParam.LIKEtaskname = $("#LIKEtaskname").val();
	sParam.videostatus = $("#videostatus").combobox("getValue");
	
	sParam.order = "order by runtime desc ";
	return sParam;
}
//加载列表
function initGrid(){
	videoStatistics();
	var toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    },{
        text	:	'播放',
        iconCls	:	'icon-start',
        handler	:	doPlay
    },{
        text	:	'人工工单',
        iconCls	:	'icon-export-excel',
        handler	:	doManualSheet
    },{
        text	:	'导出',
        iconCls	:	'icon-download',
        handler	:	doExport
    }/*{
        text	:	'增加',
        iconCls	:	'icon-add',
        handler	:	doAdd
    },{
        text	:	'修改',
        iconCls	:	'icon-cut',
        handler	:	doEdit
    },{
        text	:	'删除',
        iconCls	:	'icon-delete',
        handler	:	doDelete
    }*//*,{
        text	:	'模板',
        iconCls	:	'icon-export-excel',
        handler	:	doTemplate
    },{
        text	:	'导入',
        iconCls	:	'icon-update',
        handler	:	doImport
    },{
        text	:	'导出',
        iconCls	:	'icon-download',
        handler	:	doExport
    }*/];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywvideotaskrunresultAction!listYwvideotaskrunresultByPage.do',
		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:'runsetid',title:'运行批次id',hidden:true,width:120},
			{field:'taskid',title:'任务id',hidden:true,width:120},
			{field:'taskname',title:'任务名称',hidden:false,width:120},
			{field:'tasktypename',title:'任务类型',hidden:false,width:120},
			{field:'channelsetid',title:'通道集合id',hidden:true,width:120},
			{field:'channelname',title:'通道名称',hidden:false,width:120},
			{field:'videonum',title:'设备编号',hidden:false,width:120},
			{field:'channelnum',title:'通道编号',hidden:false,width:60},
			{field:'picturepath',title:'有无图片',hidden:false,width:60,
				formatter:function showClick(value,record,num){ 
					if(value){
						return "有";
					}else{
						return "无";
					}
				}
			},
			{field:'picturebyte',title:'图片-设计需要',hidden:true,width:120},
			{field:'isonline',title:'是否在线',hidden:true,width:60},
			{field:'isonlinename',title:'是否在线',hidden:false,width:60},
			{field:'playfail',title:'播放失败',hidden:true,width:120},
			{field:'networkerror',title:'网络异常',hidden:true,width:60,},
			{field:'networkerrorname',title:'网络异常',hidden:false,width:60,
				formatter:function showClick(value,record,num){
					return transStatus(value+"");
//					return "<div style='height:15px;width:15px;background-color:#fe0000;margin:0 auto;'></div>";
			}},
			{field:'nosignalvalue',title:'无信号检测值',hidden:false,width:110,
				/*formatter:function(value,record,num){ 
					var value="90";
					value = value.indexOf('%')>0 ? value.replace("%",""):value;
					return $.gridProgressBar(value+"%",100*value/100.00); 
				}	*/
			},
			{field:'nosignalstatus',title:'无信号检测状态',hidden:false,width:95,
				formatter:function showClick(value,record,num){ 
					return transStatus(value+"");
				}
			},
			{field:'imgclearvalue',title:'图像清晰检测值',hidden:false,width:95},
			{field:'imgclearstatus',title:'图像清晰检测状态',hidden:false,width:110,
				formatter:function showClick(value,record,num){ 
					return transStatus(value);
				}
			},
			{field:'imgcontrastvalue',title:'图像对比检测值',hidden:false,width:95},
			{field:'imgcontraststatus',title:'图像对比检测状态',hidden:false,width:110,
				formatter:function showClick(value,record,num){ 
					return transStatus(value+"");
				}
			},
			{field:'imgcolorvalue',title:'颜色检测值',hidden:false,width:70},
			{field:'imgcolorstatus',title:'颜色检测状态',hidden:false,width:90,
				formatter:function showClick(value,record,num){ 
					return transStatus(value+"");
				}
			},
			{field:'imglightvalue',title:'亮度检测值',hidden:false,width:120},
			{field:'imglightstatus',title:'亮度检测状态',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return transStatus(value+"");
				}
			},
			{field:'videofreezevallue',title:'视频冻结检测值',hidden:false,width:120},
			{field:'videofreezestatus',title:'视频冻结检测状态',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return transStatus(value+"");
				}
			},
			{field:'videojittervalue',title:'镜头抖动检测值',hidden:false,width:120},
			{field:'videojitterstatus',title:'镜头抖动检测状态',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return transStatus(value+"");
				}
			},
			{field:'videoupheavalvalue',title:'视频剧变检测值',hidden:false,width:120},
			{field:'videoupheavalstatus',title:'视频剧变检测状态',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return transStatus(value+"");
				}
			},
			{field:'videocodeerrorvalue',title:'编码异常检测值',hidden:false,width:120},
			{field:'videocodeerrorstatus',title:'编码异常检测状态',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return transStatus(value+"");
				}
			},
			{field:'videonoisevalue',title:'噪声雪花检测值',hidden:false,width:120},
			{field:'videonoisestatus',title:'噪声雪花检测状态',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return transStatus(value+"");
				}
			},
			{field:'videorollstripevalue',title:'滚动条纹检测值',hidden:false,width:120},
			{field:'videorollstripestatus',title:'滚动条纹检测状态',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return transStatus(value+"");
				}
			},
			{field:'videostatus',title:'监控状态',hidden:false,width:120,
				formatter:function showClick(value,record,num){ 
					return transStatus(value+"");
				}
			},
			{field:'failurecause',title:'故障原因',hidden:false,width:120},
			{field:'runstatusname',title:'执行状态',hidden:false,width:120},
			{field:'runtime',title:'执行时间',hidden:false,width:120},
			{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}
		]]
	});
}
//统计
function videoStatistics(){
	$.ajax({
		url : "ywvideotaskrunresultAction!videoStatistics.do",
		dataType : "JSON",
		type : "POST",
		data : getQueryParams(),
		success : function(res){
			if(res!=null){
				$("#frontDeviceTotal").html(res.total);
				$("#abnormalFrontDeviceTotal").html(res.abnormalNum);
				$("#normalFrontDeviceTotal").html(res.normalNum);
				var data={"title":"设备故障类型占比分析",
						data:[
						      {name:"网络异常",value:res.networkerrorNum,color:"#fe0000"},
						      {name:"信号异常",value:res.nosignalstatusNum,color:"#e16616"},
						      {name:"图像清晰异常",value:res.videonoisestatusNum,color:"#f6a518"},
						      {name:"图像对比异常",value:res.imgcontraststatusNum,color:"#e36d6d"},
						      {name:"颜色检测异常",value:res.imgcolorstatusNum,color:"#754599"},
						      {name:"亮度检测异常",value:res.imglightstatusNum,color:"#84ccc9"},
						      {name:"视频冻结异常",value:res.videofreezestatusNum,color:"#60b631"},
						      {name:"镜头抖动",value:res.videojitterstatusNum,color:"#add597"},
						      {name:"视频剧变异常",value:res.videoupheavalstatusNum,color:"#24b8e8"},
						      {name:"编码异常",value:res.videocodeerrorstatusNum,color:"#c0bebf"},
						      {name:"噪声雪花异常",value:res.videonoisestatusNum,color:"#c8da2e"},
						      {name:"滚动条纹异常",value:res.videorollstripestatusNum,color:"#c8da2e"}
						     //,{name:"执行异常",value:res.runstatusNum,color:"#c8da2e"}
						      ]
				};		
						 	EchartsBase.pieChart(
						 			{
						 			id:"deviceAnalysisGraphChart",
						 			data:data
						 	});
			}
			console.log("resresres",res);
		}
	});
}

function doManualSheet(){
	$.showMsg("待开发");
}
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideotaskrunresultAction!delete_Ywvideotaskrunresult.do',
			data:{
				id	:	selRows.id

			},
			success:function(flag){
				if(flag == "success"){
					$.showMsg("操作成功！");
					doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	});
}
//批量删除
function doDelete_batch(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	 if(selRows===null){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].id);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywvideotaskrunresultAction!batch_delete_Ywvideotaskrunresult.do',
				 data:{keyword	:	keyword.join(",")},
				 success:function(flag){
				 	if(!$.isNull(flag)){
						$.showMsg("操作成功！");
					 	doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			 });
		}); 
	}
}
//查询
function doSearch(){	
	$('#tableGrid').datagrid('load',getQueryParams());
}
//增加
function doAdd(){
	toEdit=false;
	windowId = top.$.createWin({
		title	: 	'增加信息',
		width	:  	650,
		height	: 	300,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ywvideotaskrunresult/ywvideotaskrunresult_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	300,
		data   	:   selRows,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ywvideotaskrunresult/ywvideotaskrunresult_show.jsp"
	});
}
//保存
function saveNodeInfo(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	console.log(model);
	if(!win.$('#childform').form('enableValidation').form('validate')){
      return;
	}
	if(toEdit){//修改
		model.XGTJid = jq('#id').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideotaskrunresultAction!update_Ywvideotaskrunresult.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}else{
		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideotaskrunresultAction!insert_Ywvideotaskrunresult.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}
}
//打开修改
function doEdit(){
	var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'))
	if(selIndex==-1){
		$.showMsg('请先选择一行! ');
		 return;
	}else{
		doUpdate(selIndex);
	}		
}
//模板下载
function doTemplate(){
	var baseUrl = $.proPath();
	var downFile = baseUrl+"/down/down.xls";
	window.location.href= downFile;
}
//导入
function doImport(){
	toEdit=false;
	windowId = top.$.createWin({
		 id		:	$.uniqueId(),
		 title	: 	'导入',
		 width	:  	400,
		 height	: 	130,
		 url	:	sysUrl+"/im/xx/ywvideotaskrunresult_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywvideotaskrunresultAction!count_Ywvideotaskrunresult.do',
		success : function(value) {
			if(!$.isNull(value)){
				countExport(value);
			}else{
				$.showMsg("操作失败！");
			}
		}
	});
}
// 导出传参条件
function countExport(totalSize) {
	var limit = 5000;
	if (totalSize > 5000) {
		top.$.dlg.confirm('记录过多支持前5000条,确定要将记录导出Excel为表格吗?',function(){ 
			execExport();
		});		
	} else {
		top.$.dlg.confirm('确认导出?',function(){ 
			execExport();
		})
	}
}
// 调用后台
function execExport() {
	var model = getQueryParams();
	var downParam = "";
	document.excelForm.action = 'ywvideotaskrunresultAction!exp_Ywvideotaskrunresult.do?'+downParam;
	document.excelForm.submit(); 
}
function initCacheStatus(){
	var ywDeviceStatusArr = [{"id":"","text":"全部","selected":true}];
	$.ajax({
		url	: "xtclAction!query_Xtcl_task.do",
		type : "POST",
		dataType : "JSON",
		data : {csdl:"ywDeviceStatus"},
		async : false,
		success : function(res){
			if(res){
				for(i in res){
					cacheStatusObj[res[i].csdm+""] = res[i].csmc;
					ywDeviceStatusArr.push({"id":res[i].csdm,"text":res[i].csmc})
				}
			}
		}
	});
	$("#videostatus").combobox( "loadData", ywDeviceStatusArr);
}
function transStatus(data){
	if(data=="1"){
		return  "<div style='height:15px;width:15px;background-color:#60b631;margin:0 auto;'></div>";
	}
	return "<div style='height:15px;width:15px;background-color:#fe0000;margin:0 auto;'></div>";
}
//视频播放
function doPlay(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	var url = top.sysUrl+"/yw/videoalarm/itemPage/detailVideoQuality.jsp";
	windowid = top.$.createWin({
		 title	: 	'详情',
		 width  :  '720',
		 height :  '400',
		 data:selRows.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);
					}
				}
		 }
	 });
}