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

$(function() {

	 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: "linkdevicename",
			width: 150
		}, {
			title: "机器型号",
			field: "extend3",
			width: 120
		},{
			title: "机器品牌",
			field: "extend4",
			width: 150
		}] ]
	});
	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: 150
		},{
			title: "设备名称",
			field: "linkdevicename",
			width: 167
		}, {
			title: "机器型号",
			field: "extend3",
			width: 150
		}/*,{
			title: "机器品牌",
			field: "extend4",
			width: 150
		}*/] ]
	});
	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'),
	}
}

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);
	console.log("go-left: ",rows)
	top.$.dlg.confirm('确定移除所选设备?',function(){ 
		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();
		
	}
}