function subAll(obj){
	var input = $(obj).closest("li").find(".sub_auth input[type='checkbox']");
	if($(obj).is(":checked"))
		input.attr("checked", true);
	else
		input.attr("checked",false);
	$.checkbox();
}
function subchildren(obj){
	var input = $(obj).closest(".sub_auth");
	input.prev().prev().attr("checked",input.find("input[type=checkbox]:checked").length > 0);
	$.checkbox();
}

function getPermissionId(){
	var permissionId = [];
	$(("input[type=checkbox]:checked")).each(function(){
		if(!isEmpty($(this).val()) && $(this).val() != 'on'){
		    permissionId.push($(this).val());
		}
	});
	return permissionId;
}

function checkRoleAdd(){
	var data = $("#addRoleForm").serializeJSON();
	if(data.name.isEmpty()){
		alert("请输入角色名称。");
		$("#name").focus();
		return false;
	}
	if(data.code.isEmpty()){
		alert("请输入角色编码。");
		$("#code").focus();
		return false;
	}
	if(data.info.isEmpty()){
		alert("请输入角色信息。");
		return false;
	}
	data['permissionId'] = getPermissionId().join(",");
	ajax("saverole.do",data,function(res){
		if(res.code == 1)
			alert("添加角色成功",1,function(){
				window.location.href = "rolelist.do";
			});
		else
			alert(res.msg);
	});
	return false;
}

function checkRoleModfiy(){
	var data = $("#modfiyRoleForm").serializeJSON();
	if(data.name.isEmpty()){
		alert("请输入角色名称。");
		$("#name").focus();
		return false;
	}
	if(data.code.isEmpty()){
		alert("请输入角色编码。");
		$("#code").focus();
		return false;
	}
	if(data.info.isEmpty()){
		alert("请输入角色信息。");
		return false;
	}
	data['permissionId'] = getPermissionId().join(",");
	
	ajax("updaterole.do",data,function(res){
		if(res.code == 1)
			alert("修改角色成功。",1,function(){
				window.location.href = "rolelist.do";
			});
		else
			alert(res.msg);
	});
	return false;
}

function delRole(roleId){
	confirm("确认删除吗",function(){
		ajax("deleterole.do",{"roleId": roleId},function(res){
			if(res.code == 1)
				alert("角色删除成功。", 1,function(){
					window.location.href = "rolelist.do";
				});
			else
				alert(res.msg);
		});
	});
}


function getList(page){
	page = page || 1;
	var pageSize = 10, data = {
		"pageNo": page,
		"pageSize": pageSize,
		"name": $("#name").val()
	}, html = "";
	$("#page, #list").empty();
	ajax("ajaxSearchRoleList.do",data,function(res){
		if(res.code == 1 && res.list.length > 0){
			$.each(res.list, function(index, item){
				html += "<tr>\n\
					<td>"+$.orderNum(page,pageSize,index)+"</td>\n\
					<td>"+item.name.toNull()+"</td>\n\
					<td>"+item.code.toNull()+"</td>\n\
					<td>"+item.info.toNull()+"</td>\n\
					<td class='edit'><a href=\"editrole.do?roleId="+item.roleId+"\" class=\"btnManage\">编辑</a>&nbsp;&nbsp;<a href=\"javascript:delRole("+item.roleId+");\" class=\"btnManage\">删除</a></td>\n\
				</tr>";
			});
			$("#list").html(html);
			var newpage = new createPage(res.count,page,pageSize,3);
			$("#page").html(newpage.pageHtml);
			$('#count').html(res.count);
		}else{
			$("#list").info("暂无数据。");
			$('#count').html(0);
		}
	});
}

pageGo = getList;