var gridSelected,gridAll;
var alarmruleid = $.getUrlParam("alarmruleid");
var alarmtype = $.getUrlParam("alarmtype");
var alarmsource = $.getUrlParam("alarmsource");

$(function() {
	
	$('#leftDeviceId').combobox({
		 url:'ywmachineinfoAction!query_Ywmachineinfo.do',
		 textField:"machinename",  
		 valueField:"machineid",
		 panelWidth:148,
		 value:"" 
	});
	
	 initButtonEvents();
	 initSelectedUser();
	 initAllUser();
});
function initButtonEvents(){	
	
	$("#buttonAll").bind('click', function(){
		searchAllUser();
    });
	$("#buttonSelected").bind('click', function(){
		searchSelectedUser();
   });
}
 
function initSelectedUser(){
	
	gridSelected = $('#leftDevice').datagrid({
		singleSelect:false,
		queryParams:getSelectedGridParam(),
		url :"ywalarmrulefordeviceAction!get_leftDevice.do",
		columns : [ [ 
		{ 
			field: 'A',
			checkbox:'true',
			width:30
		},{
			title: "",
			field: "id",
			hidden:true,
			width: 150
		},{
			title: "机器名称",
			field: "machinename",
			width: 135
		},{
			title: "cpu品牌",
			field: "extend4",
			width: 135
		}, {
			title: "cpu型号",
			field: "linkdevicename",
			width: 100
		}, {
			title: "内核数",
			field: "extend3",
			width: 50
		}] ]
	});
	gridSelected.datagrid("getPager").pagination({ 
        showPageList: false,                   
        showRefresh: false,
        layout:['first','prev','next','last']
	});
}

function initAllUser(){
	gridAll = $('#rightDevice').datagrid({
		singleSelect:false,
		queryParams:getAllGridParam(),
		url :"ywalarmrulefordeviceAction!listYwalarmrulefordeviceByPage.do",
		columns : [[ 
		{ 
			field: 'A',
			checkbox:'true',
			width:30
		},{
			title: "",
			field: "relateid",
			hidden:true,
			width: 120
		},{
			title: "cpu品牌",
			field: "extend4",
			width: 140
		},{
			title: "cpu型号",
			field: "linkdevicename",
			width: 110
		},{
			title: "内核数",
			field: "extend3",
			width: 65
		}] ]
	});
	gridAll.datagrid("getPager").pagination({ 
        showPageList: false,                   
        showRefresh: false,
        layout:['first','prev','next','last']
	});
}

function searchSelectedUser(){
	gridSelected && gridSelected.datagrid('load',  getSelectedGridParam());
}

function searchAllUser(){
	gridAll && gridAll.datagrid('load',  getAllGridParam());
}
function searchSelectedUserAndAllUser(){
	searchSelectedUser();
	searchAllUser();
}

function getSelectedGridParam(){
	
	return{
		alarmsource:alarmsource,
		alarmtype : alarmtype,
		deviceName : $('#leftDeviceName').textbox('getValue'),
		deviceId : $('#leftDeviceId').combobox('getValue'),
	}
}

function getAllGridParam(){
	return {
		alarmsource:alarmsource,
		alarmtype : alarmtype,
		LIKElinkdevicename : $('#rightDeviceName').textbox('getValue'),
		tableparam : " and alarmsource = '"+alarmsource+"' and alarmtype='"+alarmtype+"'",
	}
}

function getSelectedRow(grid ,selectall){
	!! selectall && grid.datagrid("selectAll");
	var rows = grid.datagrid("getSelections");
	if(rows.length == 0){
    	top.$.dlg.alertInfo(!! selectall ? '暂无记录！' : '请先勾选记录！');
        return;
    }
    return rows;
}

function goLeft(){
	
	var rows  = getSelectedRow(gridAll);
	doDelete(rows);

}

function goLeftAll(){
	var rows  = getSelectedRow(gridAll,true);
	top.$.dlg.confirm('确定移除所有设备?',function(){
		doDelete(rows);
	 });
}

function goRight(){
	
	doInsert(getSelectedRow(gridSelected));
	searchSelectedUserAndAllUser();
}

function goRightAll(){
	
	var rows  = getSelectedRow(gridSelected,true);
	top.$.dlg.confirm('确定添加所有设备?',function(){
		doInsert(rows);
	});
}
function doInsert(rows){
//	console.log(rows);
	if(rows && rows.length){
		rows.forEach(function(item){
			
			var linkdevicenum = item.linkdevicenum;
			if(typeof linkdevicenum == "undefined" || linkdevicenum == null || linkdevicenum == ""){
				linkdevicenum = " ";
			}
			$.ajax({
		         type:'post',
		         url : 'ywalarmrulefordeviceAction!insert_Ywalarmrulefordevice.do', 
		         data: {'linkdeviceid':item.id, 'linkdevicenum':linkdevicenum, 
		        	 	'linkdevicename':item.linkdevicename,'extend4':item.extend4,'extend3':item.extend3,
		        	 	'alarmruleid':alarmruleid,'alarmsource':alarmsource,'alarmtype':alarmtype}
		    });
		})
		
		searchSelectedUserAndAllUser();
	}
}

function doDelete(rows){
	if(rows && rows.length){
		rows.forEach(function(item){
			$.ajax({
		         type:'post',
		         url : 'ywalarmrulefordeviceAction!delete_Ywalarmrulefordevice.do', 
		         data: {"relateid":item.relateid}
		    });
		})		
		searchSelectedUserAndAllUser();
		
	}
}