<script>
$(function () {
	
	$('#erole').treegrid({
		url : '<?php echo $url?>base/role/view_nav',
		fit : true,
		fitColumns : true,
		striped : true,
		rownumbers : true,
		border : false,
		idField:'nav_id',    
	    treeField:'nav_name',
	    method:'get',
	    toolbar:'#tb',	    
		columns : [[
			{
				field : 'nav_name',
				title : '名称',
				width : 150,
			},
			{
				field : 'role_all',
				title : '全选',
				align:'center',
				width : 100,
				formatter: function(value,row,index){
					if (row.nav_fid!=0){
						return '<input  type="checkbox" onclick="roleall(this,'+row.nav_id+')">';
					} 
				}						
			},
        <?php foreach ($button as $key=>$value): ?>               
			{
				field : '<?=$key?>',
				title : '<?=$value->button_name?>',
				align:'center',
				width : 100,
				formatter: function(value,row,index){
					if (row.nav_fid!=0){
						return '<input name="'+'<?=$value->button_id?>'+'" class="erolenav'+row.nav_id+'" type="checkbox">';
					} 
				}						
			},
        <?php endforeach; ?>         								
		]]
	});
	
	
	
	$('input[name="role_name"]').validatebox({
		required : true,
		validType : 'length[2,20]',
		missingMessage : '请输入角色名称',
		invalidMessage : '角色名称在 2-20 位',
	});


	var erole_rang=<?php echo $rang?>	;	
    function erole_rang_alert(){
		    $.each( erole_rang, function(i, n){
	    	  for(var r=0;r<n.button.length;r++){
	    		  $('.erolenav'+n.nav_id).each(function(){
	    			  if(n.button[r] == $(this).attr('name')){
	    			  	$(this).prop("checked",true);
	    			  }
	  	        })    		  
	    	  }   
	    });
    }
    setTimeout(erole_rang_alert, 2000);

});
function roleall(obj,id){
	if ($(obj).is(":checked")) {
		$('.erolenav'+id).prop("checked",true);
	}
	else {
		$('.erolenav'+id).removeAttr("checked");
	}
}

function erole(){

	var role_id=<?php echo $role['role_id']?>;
	var obj=$("input[class^='erolenav']");

    var date = {};    
	obj.each(function(){
		 if($(this).prop('checked')){
			 var key=$(this).attr('class');
			 date[key] = $(this).attr('name');
		}
    });
	$.each(date, function (i, item) {
		date[i]=[];
	})
	$.each(date, function (i, item) {
		obj.each(function(){
			 if($(this).prop('checked')){
				 var key=$(this).attr('class');
				 //date[key] = $(this).attr('name');
				 if(i==$(this).attr('class')){
					 date[i].push($(this).attr('name'));
				}
			}
	    })
	}) 

	var role_name=$('#erole_name').val();
	$.ajax({
		   type: "POST",
		   url: "<?php echo $url?>base/role/erole/"+<?php echo $role['role_id']?>+"/"+role_name,
		   data: date,
			beforeSend : function () {
				$.messager.progress({
					text : '正在修改中...',
				});
			},		   
           success:function(data){
				$.messager.progress('close');   
                  var data = eval('(' + data + ')');     
                  if (data.success){    
               	 	 $.messager.alert('提示',data.message,'info');	  
                	 	//$('#tabs').tabs('close','修改角色');
                	 	var currentTab = $('#tabs').tabs('getSelected');
                      	 RefreshTab(currentTab);
                  }else{
                 	 	 $.messager.alert('提示',data.message,'info');   
                  }                   	
              }  		   
		});		  
}

function RefreshTab(currentTab) {
    var url = $(currentTab.panel('options')).attr('href');
     $('#tabs').tabs('update', {
        tab: currentTab,
         options: {
            href: url
        }
   });
   currentTab.panel('refresh');
}
</script>
<table id="erole"></table>

<div id="tb" style="padding:2px 5px;">
    角色名: <input id="erole_name" name="role_name"  style="width:110px" value="<?php echo $role['role_name']?>">
    <a id="erolebutton" href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="return erole()">提交</a>
</div>



