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: "硬盘类型",
			field: "linkdevicename",
			width: 135
		}, {
			title: "盘符",
			field: "extend3",
			width: 60
		}, {
			title: "容量",
			field: "extend4",
			width: 90
		}] ]
	});
	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: "硬盘类型",
			field: "linkdevicename",
			width: 150
		},{
			title: "盘符",
			field: "extend3",
			width: 80
		},{
			title: "容量",
			field: "extend4",
			width: 83
		}] ]
	});
	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();
		
	}
}