<?php

class Role extends MY_Controller{
    
    public function __construct(){
        parent::__construct();
        $this->load->model('base/role_model');
    }
    public function index(){
        $_data ['button']=$this->navButton('Base');
        $_data ['url'] = $this->url;
//         $_data ['button'] = $this->get_button();
        $this->load->view('base/role',$_data); 
    }
    
    public function add_view(){
        $_data ['url'] = $this->url;
        $_data ['button'] = $this->get_button();
        $this->load->view('base/rolea',$_data);
    }    
    
    public function edit_view($id){
        $_data ['url'] = $this->url;
        $_data ['button'] = $this->get_button();
        $_data ['role'] =$this->get_one_role($id);
        $_row=unserialize($_data ['role']['role_nav_button_rang']);    
//         $_Str=array(array(nav_id=>1,child=>array(array(nav_id=>6,button=>array(1,2,3,4)),array(nav_id=>7,button=>array(5,6)))),array(nav_id=>5,child=>array(array(nav_id=>10,button=>array(7,8)))));
//         echo serialize($_Str);
//         print_r($_Str);
        
        foreach($_row as $_value){
            foreach($_value['child'] as $_keys=>$_values){
                   $_arr[]=$_values ;
            }
        } 
        $_arr=json_encode($_arr);
        $_data ['rang']=$_arr;
        $this->load->view('base/rolee',$_data);
    }    
    
    public function arole($name){
        $_webpost=$this->input->post();
        if(empty($_webpost)){
            echo'{"success":false,"message":"没有选择按钮，不允许保存！"}';exit;
        }
        if(empty($name)){
            echo'{"success":false,"message":"没有填写角色名，不允许保存！"}';exit;
        }
    
        foreach($_webpost as $_key=>$_value){
            $_webpost[substr($_key,8)]=$_value;
            unset($_webpost[$_key]);
        }
    
        foreach ($_webpost as $_key=>$_value){
            $_arr[$_key]['nav_id']=$_key;
            $_arr[$_key]['button']=$_value;
        }
    
        $_row = $this->role_model->get_nav();
    
        foreach($_arr as $_key=>$_value){
            foreach($_row as $_keys=>$_values){
                if($_value['nav_id']==$_values->nav_id){
                    $_arrs[$_keys]['nav_id']=$_values->nav_fid;
                    $_arrs[$_keys]['child'][]=$_value;
                }
            }
        }
    
        foreach ($_arrs as $_key=>$_value){
            $_reuslt[$_value['nav_id']][]=$_value;
        }
    
        foreach($_reuslt as $_key=>$_value){
            if(count($_value)==1){
                $_reuslt[$_key]=$_value[0];
            }
            if(count($_value)>1){
                for($_i=1;$_i<count($_value);$_i++){
                    $_value[0]['child'][]=$_value[$_i]['child'][0];
                }
                $_reuslt[$_key]=$_value[0];
            }
        }
    
        $_data ["role_name"]=urldecode($name);
    
        $_data ["role_nav_button_rang"]=serialize($_reuslt);
    
        if($this->role_model->add_role($_data)){
            echo'{"success":true,"message":"角色添加成功"}';
        }else{
            echo'{"success":false,"message":"添加失败，疑似没有数据变化！"}';
        }
    }    
    
    public function erole($id,$name){
        $_webpost=$this->input->post();
        if(empty($_webpost)){
            echo'{"success":false,"message":"没有选择按钮，不允许保存！"}';exit;
        }
        if(empty($name)){
            echo'{"success":false,"message":"没有填写角色名，不允许保存！"}';exit;
        }        
        $_rolepost=$this->get_one_role($id);
        if($_rolepost['role_nav_button_rang']=='all'){
            echo'{"success":false,"message":"你不能修改这个角色"}';exit;
        }    
        
        foreach($_webpost as $_key=>$_value){
                $_webpost[substr($_key,8)]=$_value;
               unset($_webpost[$_key]);
            }

        foreach ($_webpost as $_key=>$_value){
            $_arr[$_key]['nav_id']=$_key;
            $_arr[$_key]['button']=$_value;
        }

        $_row = $this->role_model->get_nav();

        foreach($_arr as $_key=>$_value){
            foreach($_row as $_keys=>$_values){
                if($_value['nav_id']==$_values->nav_id){
                   $_arrs[$_keys]['nav_id']=$_values->nav_fid;
                   $_arrs[$_keys]['child'][]=$_value;
                }
            }
        }

        foreach ($_arrs as $_key=>$_value){
            $_reuslt[$_value['nav_id']][]=$_value;
        }

        foreach($_reuslt as $_key=>$_value){
            if(count($_value)==1){
                $_reuslt[$_key]=$_value[0];
            }
            if(count($_value)>1){
                for($_i=1;$_i<count($_value);$_i++){
                    $_value[0]['child'][]=$_value[$_i]['child'][0];
                }
                $_reuslt[$_key]=$_value[0];
            }
        }
        
        $_data ["role_name"]=urldecode($name);;

        $_data ["role_nav_button_rang"]=serialize($_reuslt);
        
        if($this->role_model->update_role($id,$_data)){
            echo'{"success":true,"message":"角色修改成功"}';
        }else{
            echo'{"success":false,"message":"修改失败，疑似没有数据变化！"}';
        }
    }
    
    public function get_role(){
       $_row = $this->role_model->get_role();
       $_row=array_values($_row);
       $_str='['.substr(json_encode($_row),1,-1).']';
       echo $_str;
    }
    public function get_button(){
        $_row = $this->role_model->get_button();
        return $_row;
    }  
    public function get_nav(){
        
        if(!!$_row = $this->role_model->get_nav()){
            foreach ($_row as $_key=>$_value){
                if($_value->nav_fid!=0){
                    foreach($_row as $_values){
                        if($_value->nav_fid==$_values->nav_id){
                            $_values->children[]=$_value;
                        }
                    }
                   unset($_row[$_key]);
                }
            }          
        }        
        return $_row;
    }
    
    
    public function view_nav(){
        $_row=$this->get_nav();
        echo  json_encode(array_values($_row));
    }
    
    public function get_one_role($_id){

        if($this->role_model->get_one_role($_id)){
            $_row = $this->role_model->get_one_role($_id);
            return $_row;
        }else{
            echo'{"success":false,"message":"角色不存在"}';
        }
     
     }
     
}