<?php

class Nav extends MY_Controller{
    
    public function __construct(){
        parent::__construct();
        $this->load->model('base/nav_model');
    }
    public function index(){
        $_data ['button']=$this->navButton('Base');
        $_data ['url'] = $this->url;
        $this->load->view('base/nav',$_data); 
    }
    
    public function get_nav(){
       $_row = $this->nav_model->get_nav();
       foreach ($_row as $_key=>$_value){
           if($_value->nav_fid!=0){
               foreach($_row as $_values){
                   if($_value->nav_fid==$_values->nav_id){
                       $_value->nav_father_name=$_values->nav_name;
                   }
               }
           }
       }
       foreach ($_row as $_key=>$_value){
           if($_value->nav_fid==0){
               unset($_row[$_key]);
           }
       }
       $_row=array_values($_row);
       $_str='['.substr(json_encode($_row),1,-1).']';
       echo $_str;
    }
    
    public function get_father_nav(){
        $_row = $this->nav_model->get_nav();
        $_nav=array();
        foreach ($_row as $_key=>$_value){
            if($_value->nav_fid!=0){
                unset($_row[$_key]);                
            }else{
                $_nav[$_key]->id=$_value->nav_id;
                $_nav[$_key]->text=$_value->nav_name;
            }
        }
        
        $_row=array_values($_nav);
        $_str='['.substr(json_encode($_row),1,-1).']';
        echo $_str;
    }    
    
    public function get_one_nav(){
        $_id=$this->input->post('id');
        if(isset($_id)){
            if($this->nav_model->get_one_nav($_id)){
                $_row = $this->nav_model->get_one_nav($_id);
                $_row = json_encode($_row);
                echo '['.$_row.']';;
            }else{
                echo'{"success":false,"message":"导航不存在"}';
            }
        }else{
            echo'{"success":false,"message":"非法访问"}';
        }       
     }
     
     public function add_nav(){
         $_fid=$this->input->post('nav_fid');
         if(isset($_fid)){
             $_data ["nav_fid"]=$this->input->post('nav_fid');
             $_data ["nav_name"]=$this->input->post('nav_name');
             $_data ["nav_url"]=$this->input->post('nav_url');
             $_data ["nav_level"]=$this->input->post('nav_level');
             $_data ["nav_status"]=$this->input->post('nav_status');
              
             if($this->nav_model->add_nav($_data)){
                 echo'{"success":true,"message":"添加成功"}';
             }else{
                 echo'{"success":false,"message":"添加失败"}';
             }
         }else{
             echo'{"success":false,"message":"非法访问"}';
         }
     }     
     
     public function update_nav(){
         $_id=$this->input->post('nav_id');
         if(isset($_id)){
             $_data ["nav_url"]=$this->input->post('nav_url');
             $_data ["nav_level"]=$this->input->post('nav_level');
             $_data ["nav_status"]=$this->input->post('nav_status');
             
             if($this->nav_model->update_nav($_id,$_data)){
                 echo'{"success":true,"message":"修改成功"}';
             }else{
                 echo'{"success":false,"message":"修改失败"}';
             }
         }else{
             echo'{"success":false,"message":"非法访问"}';
         }        
     }
}