var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
var alarmruleid = $.getUrlParam("alarmruleid");
var alarmsource = $.getUrlParam("alarmsource");
var alarmtype = $.getUrlParam("alarmtype");
$(function(){
	initDefault();	
});
//默认
function initDefault(){
	
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.LIKElinkdevicename = $('#LIKElinkdevicename').textbox("getValue");
	sParam.alarmruleid = alarmruleid;
	/*
	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 ";
	*/
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [
	{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    },{
        text	:	'增加',
        iconCls	:	'icon-add',
        handler	:	addAlarmDevice
    }/*,{
        text	:	'修改',
        iconCls	:	'icon-cut',
        handler	:	doEdit
    }*/,{
        text	:	'删除',
        iconCls	:	'icon-delete',
        handler	:	doDelete_batch
    },{
        text	:	'启用',
        iconCls	:	'icon-start',
        handler	:	switchOpen
    },{
        text	:	'停止',
        iconCls	:	'icon-end',
        handler	:	switchClose
    }];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:false,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywalarmrulefordeviceAction!listYwalarmrulefordeviceByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
		    {field:'relateid',title:'relateid',hidden:true,width:120, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
 			},
 			{field:'linkdevicename',title:'联动设备名称',hidden:false,width:120},
			{field:'linkdevicetype',title:'联动设备类型',hidden:true,width:120},
			{field:'linkdevicenum',title:'联动设备编号',hidden:false,width:120},
			{field:'linkchannelnum',title:'联动通道编号',hidden:false,width:120},			
			{field:'detectionswitchMc',title:'是否启用',hidden:false,width:120}
		]]
	});
}
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywalarmrulefordeviceAction!delete_Ywalarmrulefordevice.do',
			data:{
				relateid	:	selRows.relateid

			},
			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].relateid);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywalarmrulefordeviceAction!batch_delete_Ywalarmrulefordevice.do',
				 data:{keyword	:	keyword.join(",")},
				 success:function(flag){
				 	if(flag == "success"){
						$.showMsg("操作成功！");
					 	doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			 });
		}); 
	}
}
//查询
function doSearch(){	
	$('#tableGrid').datagrid('load',getQueryParams());
}
//增加
function doAdd(){
	toEdit=false;
	windowId = top.$.createWin({
		title	: 	'增加信息',
		width	:  	650,
		height	: 	300,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ywgl/ywalarmrulefordevice_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	300,
		data   	:   selRows,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/im/ywgl/ywalarmrulefordevice_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.XGTJrelateid = jq('#relateid').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywalarmrulefordeviceAction!update_Ywalarmrulefordevice.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:'ywalarmrulefordeviceAction!insert_Ywalarmrulefordevice.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,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		 url	:	sysUrl+"/im/xx/ywalarmrulefordevice_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywalarmrulefordeviceAction!count_Ywalarmrulefordevice.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 = 'ywalarmrulefordeviceAction!exp_Ywalarmrulefordevice.do?'+downParam;
	document.excelForm.submit(); 
}

function switchOpen(){
	var selRows = $('#tableGrid').datagrid('getChecked');
	console.log("sel==",selRows);
	var ids = '';
	if(selRows===null || selRows.length<1){
		$.showMsg('请先选择一行! ');
		return;
	}else{
		selRows.forEach(function(item){
			ids += item.relateid+',';
		})
	    $.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'ywalarmrulefordeviceAction!edit_Ywalarmrulefordevice.do',
			 data:{'ids':ids,'detectionswitch':1},
			 success:function(res){
			 	if(res=="success"){
					$.showMsg("操作成功！");
				 	doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		 });
	}
}
function switchClose(){
	var selRows = $('#tableGrid').datagrid('getChecked');
	var ids = '';
	if(selRows===null || selRows.length<1){
		$.showMsg('请先选择一行! ');
		return;
	}else{
		selRows.forEach(function(item){
			ids += item.relateid+',';
		})
	    $.ajax({
			 type:'post',
			 dataType : 'json',
			 url:'ywalarmrulefordeviceAction!edit_Ywalarmrulefordevice.do',
			 data:{'ids':ids,'detectionswitch':0},
			 success:function(res){
			 	if(res=="success"){
					$.showMsg("操作成功！");
				 	doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		 });
	}
}

function addAlarmDevice(){
	
	var tarUrl = "";
//	console.log("sou=== :  ",alarmsource);
//	console.log("ty=== :  ",alarmtype);
	if(alarmsource==1){
		tarUrl = sysUrl+"/yw/alarmtaskmanage/alarmruleinfodevice/alarmVideodevice.jsp?alarmruleid="+alarmruleid+"&alarmtype="+alarmtype+"&alarmsource="+alarmsource;
		
	}else if(alarmsource==3){						//应用系统告警
		tarUrl = sysUrl+"/yw/alarmtaskmanage/alarmruleinfodevice/alarmSoftdevice.jsp?alarmruleid="+alarmruleid+"&alarmtype="+alarmtype+"&alarmsource="+alarmsource
		
	}else if(alarmsource==2){				//服务器告警
		if(alarmtype==4){					// 1-主机异常      2-CPU告警      3-内存告警      4-硬盘告警
			tarUrl = sysUrl+"/yw/alarmtaskmanage/alarmruleinfodevice/alarmDiskdevice.jsp?alarmruleid="+alarmruleid+"&alarmtype="+alarmtype+"&alarmsource="+alarmsource;
		}else if(alarmtype==1){
			tarUrl = sysUrl+"/yw/alarmtaskmanage/alarmruleinfodevice/alarmdevice.jsp?alarmruleid="+alarmruleid+"&alarmtype="+alarmtype+"&alarmsource="+alarmsource;
		}else if(alarmtype==2){
			tarUrl = sysUrl+"/yw/alarmtaskmanage/alarmruleinfodevice/alarmCPUdevice.jsp?alarmruleid="+alarmruleid+"&alarmtype="+alarmtype+"&alarmsource="+alarmsource;			
		}else if(alarmtype==3){
			tarUrl = sysUrl+"/yw/alarmtaskmanage/alarmruleinfodevice/alarmMemorydevice.jsp?alarmruleid="+alarmruleid+"&alarmtype="+alarmtype+"&alarmsource="+alarmsource;			
		}
	}
	
	windowId = top.$.createWin({
		title	: 	'增加信息',
		width	:  	900,
		height	: 	500,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:doclose
		}],
		url	:	tarUrl
	 });
}

function doclose(){
	doSearch();
	top.$("#"+windowId).dialog('close');
}
