var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
var num=0;
$(function(){
	initGrid();
});

//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKEalarmrulename = $('#LIKEalarmrulename').textbox("getValue");
	return sParam;
}

//加载列表
function initGrid(){
	$('#ruleGrid').datagrid({
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywalarmruleinfoAction!listYwalarmruleinfoByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
		    {field:'alarmruleid',title:'alarmruleid',hidden:true,width:120, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
 			},
			{field:'alarmrulename',title:'告警规则名称',hidden:false,width:120},
			{field:'isenablemc',title:'是否启用',hidden:false,width:80},
			{field:'alarmsourcemc',title:'告警来源',hidden:false,width:120},
			{field:'alarmsource',title:'告警来源',hidden:true,width:120},
			{field:'alarmtype',title:'告警类型',hidden:false,width:220,
				formatter:function showClick(value,record,num){ 
					var alarmTypeMc = '';
//					console.log("alarmTypeMc: ",record,value);
					var alarmSource = record.alarmsource;
					var types = value.toString().split(",");			
					var len = types.length;
					var typeMc = "";
					for(var i=0; i<len; i++){
						var type = types[i];
						type = getAlarmtypeMc(alarmSource,type);
						typeMc += type+",";			
					}							
					return typeMc.substr(0,typeMc.length-1);
				}
			},
			{field:'alarmgrademc',title:'告警等级',hidden:false,width:80},
			{field:'alarmtriggermc',title:'告警时触发',hidden:false,width:80},
			{field:'alarmmaxcount',title:'最大告警次数',hidden:true,width:90},
			{field:'autounalarmtime',title:'自动消警时间间隔',hidden:true,width:120},						
			{field:'remarks',title:'备注',hidden:false,width:150},		
		]],
		
		onClickRow:function(index,row){
			num=index;
			setchecked();
		},
		onLoadSuccess:function(){
			$("#ruleGrid").datagrid("selectRow",num);
			setchecked();			
		}
	});
}

function getAlarmtypeMc(alarmSource, type){
	if(alarmSource == '1'){					// 监控告警
		if(type=='2'){
			type = "网络异常"
		}else if(type=='3'){
			type = "设备不在线"
		}else if(type=='4'){
			type = "视频抽帧异常"
		}else if(type=='5'){
			type = "视频质量诊断异常"
		}else if(type=='6'){
			type = "录像异常"
		}
	}else if(alarmSource == '2'){			// 服务器告警
		if(type=='2'){
			type = "主机不在线或程序异常"
		}else if(type=='3'){
			type = "CPU异常"
		}else if(type=='4'){
			type = "内存异常"
		}else if(type=='5'){
			type = "硬盘异常"
		}else if(type=='6'){
			type = "超时未上报"
		}
	}else if(alarmSource == '3'){			// 应用系统告警
		if(type=='1'){
			type='应用不在线';
		}else if(type=='2'){
			type='应用异常';
		}
	}else if(alarmSource == '4'){			// 存储告警
		if(type=='1'){
			type='存储无法连接';
		}else if(type=='2'){
			type='存储上水位报警';
		}
	}
	/*if(alarmSource == '1'){					// 监控告警
		if(type=='101'){
			type = "网络异常"
		}else if(type=='102'){
			type = "设备不在线"
		}else if(type=='104'){
			type = "视频抽帧异常"
		}else if(type=='105'){
			type = "视频质量诊断告警"
		}else if(type=='107'){
			type = "录像异常"
		}
	}else if(alarmSource == '2'){			// 服务器告警
		if(type=='201'){
			type = "主机不在线或程序异常"
		}else if(type=='202'){
			type = "CPU异常"
		}else if(type=='203'){
			type = "内存异常"
		}else if(type=='204'){
			type = "硬盘异常"
		}
	}else if(alarmSource == '3'){			// 应用系统告警
		if(type=='1'){
			type='应用不在线';
		}else if(type=='2'){
			type='应用异常';
		}
	}else if(alarmSource == '4'){			// 存储告警
		if(type=='1'){
			type='存储无法连接';
		}else if(type=='2'){
			type='存储上水位报警';
		}
	}*/
	return type;
}

function setchecked(){
	var selRows = $('#ruleGrid').datagrid('getSelected');
	if(selRows){
		var alarmruleid = selRows.alarmruleid;
		var alarmtype = selRows.alarmtype;		
		var alarmsource = selRows.alarmsource;
		$("#alarmdeviceInfo").prop("src",'../../alarmtaskmanage/alarmruleinfodevice/ywalarmrulefordevice.jsp?alarmruleid='+alarmruleid+"&alarmtype="+alarmtype+"&alarmsource="+alarmsource);
	}
	
}

//查询
function doSearch(){	
	$('#ruleGrid').datagrid('load',getQueryParams());
}
