jQuery(document).ready(function() {
	
	var grid_selector = "#grid-table";
	var pager_selector = "#grid-pager";
	
	function loadData(){
	      $('#grid-table').jqGrid('setGridParam',{'postData':{ 'org.id':sxUtil.tree.getSelectOrgId() }});
			$('#grid-table').setGridParam({
				url : '/role/do_list',
				datatype : 'json',
				postData: { 'org.id':sxUtil.tree.getSelectOrgId() },
				page : 1
			});
			$('#grid-table').trigger('reloadGrid');
		}
	
	//初始化组织架构
	sxUtil.tree.navTreeMap({
		selectedAfter:loadData
	});
	sxUtil.jqGrid(grid_selector,{
		mtype:'get',
		url: '/role/do_list',
		postData:{ 'org.id':sxUtil.tree.getSelectOrgId() },
		datatype: "json",
		colNames:["编号",'角色名称','创建时间','修改时间', '描述'],
		colModel:[
		    {name:'id',index:'id', width:60, sorttype:"int",hidden:true,hidedlg:true},
			{name:'name',index:'name', width:70},
			{name:'gmtCreate',index:'gmtCreate', width:70},
			{name:'gmtModified',index:'gmtModified', width:70},
			{name:'description',index:'description', width:160, sortable:false},
		], 
		viewrecords : true,
		rowNum:20,
		rowList:[10,20,30],
		pagerpos: 'left',
		pager : pager_selector,
		multiselect: true
	},true);
	
	$(window).triggerHandler('resize.jqGrid');

	var selr = jQuery(grid_selector).jqGrid('getGridParam','selrow');
	
	$('#add').click(function(){
		sxUtil.dialog.window({
			url:'/role/add',
			data:{'roleOrgId':sxUtil.tree.getSelectOrgId()},
			layer:{
				  title:'新增',
				  area: ['950px', '550px'],
				  success:function(dom,index){
					  
				  },
				  btn: ['确定', '关闭'],
				  yes: function(index, dom){
//					  $("#roleOrgId").val(sxUtil.tree.getSelectOrgId());
					  $("#roleForm").data('index',index).submit(); 
				  }
			}
		});
	});
	
	
	$('#delete').click(function(){
		var ids=jQuery(grid_selector).jqGrid('getGridParam','selarrrow');
		var id=jQuery(grid_selector).jqGrid('getGridParam','selrow');
		if(ids.length >= 1){
			layer.open({
				content: "您是否确认删除",
				icon: 0,
				title: "提示",
		        btn: ['确认', '取消'],
		        shadeClose: false,
		        yes: function(index, layero){
		        	$.ajax({
						type: "POST",
						url: "/role/do_delete",
						data: {"ids": ids.join(",")},
						error:function(){
							layer.msg("请求失败...");
						},
						success:function(msg){
							if(msg.success==true){
								ref();
								layer.close(index); 
							}else{
								layer.msg(msg.msg,{icon:2});
							}
						}
					});
		        }
			});
		}else{
			layer.msg("请选择删除一项",{icon:0});
		}
	});
	
	$('#update').click(function(){
		var ids=jQuery(grid_selector).jqGrid('getGridParam','selarrrow');
		var id=jQuery(grid_selector).jqGrid('getGridParam','selrow');
		if(ids.length==1){
			sxUtil.dialog.window({
				url:'/role/update',
				data:{"id":id},
				layer:{
					  title:'修改',
					  area: ['950px', '550px'],
					  success:function(dom,index){
						  
					  },
					  btn: ['确定', '关闭'],
					  yes: function(index, dom){
						  $("#roleForm").data('index',index).submit(); 
					  }
				}
			});
		}else{
			layer.msg("请选择修改一项",{icon:0});
		}
	});
	
	$('#select').click(function(){
		sxUtil.dialog.window({
			url:'/role/select',
			layer:{
				  title:'查询',
				  area: ['950px', '200px'],
				  success:function(dom,index){
				  },
				  btn: ['确定', '关闭'],
				  yes: function(index, dom){
					  var name = $("#name").val();
					  jQuery(grid_selector).jqGrid('setGridParam',{
							  url:'/role/do_list',
							  postData:{"name":name},
							  page:1 
			          }).trigger("reloadGrid"); //重新载入
					  layer.close(index);
				  }
			 }
		});
	});
	
	$('#refresh').click(function(){
		ref();
	});
	
	function ref(){
		jQuery(grid_selector).jqGrid('setGridParam',{
			  url:'/role/do_list',
			  postData:{"name":null,'org.id':sxUtil.tree.getSelectOrgId(),'level':$("#chosen-level").val()},
			  page:1 
		}).trigger("reloadGrid"); //重新载入
	}
	
	$("#chosen-level").change(function(){
		ref();
	});
			
});