var roleId= $.getUrlParam("roleId"),gridSelected,gridAll;

$(function() {
	 initButtonEvents();
	 initSelectedUser();
	 initAllUser();
});
function initButtonEvents(){
	$("#buttonAll").bind('click', function(){
		searchAllUser();
    });
	$("#buttonSelected").bind('click', function(){
		searchSelectedUser();
   });
}
 
function initSelectedUser(){
	gridSelected = $('#selectedUser').datagrid({
		singleSelect:false,
		queryParams:getSelectedGridParam(),
		url:'commonMenuGroupAction!getUserByGroupId.do',
		columns : [ [ 
		{ 
			field: 'A',
			checkbox:'true',
			width:30
		},
		{
			title: "用户编号",
			field: "adminId",
			width: 150
		},
		{
			title: "用户账号",
			field: "adminName",
			width: 150
		}] ]
	});
	gridSelected.datagrid("getPager").pagination({ 
		pageList: [20,50,80,100],
        layout:['list','first','prev','next','last']
	});
}

function initAllUser(){
	gridAll = $('#allUser').datagrid({
		singleSelect:false,
		queryParams:getAllGridParam(),
		url:'commonMenuGroupAction!getCommonUserNotInGroup.do',
		columns : [ [ 
		{ 
			field: 'A',
			checkbox:'true',
			width:30
		},{
			title: "用户编号",
			field: "adminId",
			width: 150
		},
		{
			title: "用户账号",
			field: "adminName",
			width: 150
		}] ]
	});
	gridAll.datagrid("getPager").pagination({ 
		pageList: [20,50,80,100],
        layout:['list','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{
		groupId:roleId,
		keyword : $('#loginName').textbox('getValue')
	}
}

function getAllGridParam(){
	return {
		groupId:roleId,
		keyword : $('#loginNameAll').textbox('getValue')
	}
}

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

function goLeft(){
	doInsert(getSelectedRow(gridAll));
}

function goLeftAll(){
	var rows  = getSelectedRow(gridAll,true);	
	doInsert(rows);
}

function goRight(){
	var rows  = getSelectedRow(gridSelected);
	doDelete(rows);
}

function goRightAll(){
	var rows  = getSelectedRow(gridSelected,true);
	var ids  = [];
	for(var i=0; i<rows.length; i++){
		ids.push(rows[i].adminId);
	}
	
    if (ids.length == 0) {
    	top.$.dlg.alertInfo('请勾选要移动的记录！');
        return;
    }
	top.$.dlg.confirm('确定移除所选用户?',function(){ 
		    var adminIds = ids.join(",");
		
			$.ajax({
		        type:'post',
		        url : 'commonMenuGroupAction!removeUserGroup.do',
		        data: {
		            userIds	 : adminIds,
		            groupId  : roleId
		        },
		        success : function(result){
		            if(result == 1){
		            	searchSelectedUserAndAllUser();
					 }else{ 
						 top.$.dlg.alertInfo('操作失败！');
					 }
		      }
			});
	});
}
function doInsert(){
	var ids  = [];
	var rows  = getSelectedRow(gridAll);
	
	for(var i=0; i<rows.length; i++){
		ids.push(rows[i].adminId);
	}
	
    if (ids.length == 0) {
    	top.$.dlg.alertInfo('请勾选要移动的记录！');
        return;
    }
    top.$.dlg.confirm('确定添加用户?',function(){
		
	    var adminIds = ids.join(",");
	    $.ajax({
			type:'post',
			url: 'commonMenuGroupAction!insertUserGroup.do',
			data: {
	            userIds	 : adminIds,
	            groupId  : roleId
	        },
			success: function (result){
				 if(result == 1){
					 searchSelectedUserAndAllUser();
				 }else{ 
					 top.$.dlg.alertInfo('操作失败！');
				 }
			} 
		});
	});
}
function doDelete(rows){
	var ids  = [];
	for(var i=0; i<rows.length; i++){
		ids.push(rows[i].adminId);
	}
    if (ids.length == 0) {
    	top.$.dlg.alertInfo('请勾选要移动的记录！');
        return;
    }
    var adminIds = ids.join(",");
    top.$.dlg.confirm('确定移除所选用户?',function(){ 
		$.ajax({
			        type:'post',
			        url : 'commonMenuGroupAction!removeUserGroup.do',
			        data: {
			            userIds	 : adminIds,
			            groupId  : roleId
			        },
			        success : function(result){
			            if(result == 1){
			            	searchSelectedUserAndAllUser();
						 }else{ 
							 top.$.dlg.alertInfo('操作失败！');
						 }
			      }
		});
    });
}