<?php

class Html extends MY_Controller{
    
    public  $_role;
    public function __construct(){
        parent::__construct();
        $this->load->model('html_model');
        $this->_role=$this->html_model->get_role($_SESSION['roleid']);
    }
    public function index(){
        if(!!$_row = $this->html_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]);
                }
            }
//             print_r($_row);
            if($this->_role['role_nav_button_rang']!='all'){
                $_role=unserialize($this->_role['role_nav_button_rang']);
//                 print_r($_role);
                foreach ($_role as $_keys=>$_values){
                    foreach ($_row as $_key=>$_value) {
                        if($_values['nav_id']==$_value->nav_id){                                                  
                            if($_values['child']){                               
                                foreach($_values['child'] as $_ke=>$_valu){
                                    foreach ($_value->children as $_k=>$_v){
                                        if ($_valu['nav_id']==$_v->nav_id){
                                            $_v->button=$_valu['button'];
                                        }
                                    }          
                                }
                            }
                            
                            foreach($_row[$_key] as $_kk=>$_vv){
                                if($_kk=='children'){
                                    foreach($_vv as $_kkk=>$_vvv){
                                        if(empty($_vvv->button)){
                                            unset($_row[$_key]->children[$_kkk]); 
                                        }
                                    }
                                }
                            }
//                             print_r($_row[$_key]);
                            $_arr[]=$_row[$_key];
                        }
                    }
                }       
                $_row=$_arr;
            }else{
                $_SESSION['admin']=true;
            }            
            $_SESSION['power']=$_row;
        }
        $_SESSION['button']=$this->button();
        $_data ['url'] = $this->url;
        $this->load->view('html',$_data); 
    }
    
    public function nav($_code){
//         print_r($_SESSION['power']);
        $_row = array();
        foreach ($_SESSION['power'] as $_key=>$_value){
            if ($_value->nav_code==$_code){
               $_row[0]->id=$_value->nav_id;
               $_row[0]->text=$_value->nav_name;
               $_row[0]->state=$_value->nav_status;
               $_row[0]->url=$_value->nav_url;
               if(isset($_value->children)){
                   $_value->children=array_values($_value->children);
//                    print_r($_value->children);
                   foreach ($_value->children as $_keys=>$_values){
                       $_row[0]->children[$_keys]->id=$_values->nav_id;
                       $_row[0]->children[$_keys]->text=$_values->nav_name;
                       $_row[0]->children[$_keys]->state=$_values->nav_status;
                       $_row[0]->children[$_keys]->url=$_values->nav_url;                     
                   }

               }
            }
        }
//             print_r($_row);
        echo json_encode($_row);
    }

    public function button(){
        if(!!$_row = $this->html_model->get_button()){
           return $_row;
        }
    }
}