<?php

class User extends MY_Controller{
    
    public function __construct(){
        parent::__construct();
        $this->load->model('base/user_model');
    }
    public function index(){
        $_data ['button']=$this->navButton('Base');        
        $_data ['url'] = $this->url;
        $_data['role']= $this->get_role();
        $this->load->view('base/user',$_data); 
    }
    
    public function get_user(){
       $_row = $this->user_model->get_user();
       $_row=array_values($_row);
       $_str='['.substr(json_encode($_row),1,-1).']';
       echo $_str;
    }
    
    public function get_role(){
        return $this->user_model->get_role();
    }
    
    public function get_role_tree(){
        $_row=$this->get_role();
        foreach ($_row as $_key=>$_value){
                $_rows[$_key]->id=$_value->role_id;
                $_rows[$_key]->text=$_value->role_name;
        }
        $_row=array_values($_rows);
        $_str='['.substr(json_encode($_row),1,-1).']';
        echo $_str;
    }
    
    public function get_one_user(){
        $_id=$this->input->post('id');
        if(isset($_id)){
            if($this->user_model->get_one_user($_id)){
                $_row = $this->user_model->get_one_user($_id);
                $_row = json_encode($_row);
                echo '['.$_row.']';
            }else{
                echo'{"success":false,"message":"用户不存在"}';
            }
        }else{
            echo'{"success":false,"message":"非法访问"}';
        }
    }
  
     
     public function add_user(){
         $_role=$this->input->post('user_role');
         if(isset($_role)){
             $_data ["user_role"]=$this->input->post('user_role');
             $_data ["user_password"]=sha1('666');
             $_data ["user_name"]=$this->input->post('user_name');
              
             if($_data ["user_name"]){
                 $_row = $this->user_model->get_user_name($_data ["user_name"]);
                 if($_row){
                     echo'{"success":false,"message":"添加失败,用户名已经存在！"}';exit;
                 }
             }else{
                     echo'{"success":false,"message":"非法访问"}';exit;
             }
             if($this->user_model->add_user($_data)){
                 echo'{"success":true,"message":"添加成功"}';
             }else{
                 echo'{"success":false,"message":"添加失败"}';
             }
         }else{
             echo'{"success":false,"message":"非法访问"}';
         }
     }     
     
     public function update_user(){
         $_id=$this->input->post('user_id');
         if(isset($_id)){
             if($this->input->post('user_password')){
                 $_data ["user_password"]=sha1($this->input->post('user_password'));
             }
             $_data ["user_role"]=$this->input->post('user_role');
             if($this->user_model->update_user($_id,$_data)){
                 echo'{"success":true,"message":"修改成功"}';
             }else{
                 echo'{"success":false,"message":"修改失败"}';
             }
         }else{
             echo'{"success":false,"message":"非法访问"}';
         }        
     }
}