<?php
class MainMenu {
    //put your code here
    private $rol, $menu, $submenu, $pdo, $xtpl;
    private $menuEstatico = array();
    
    /**
     * @param int $usr_id Id del usuario actual
     * @param int $menu_selected Id del menú seleccionado
     * @param PDO $this->pdo  Conexión de base de datos
     */
    function __construct($rol_id, $menu_selected, $submenu_selected, $pdo){
        $this->menu = $menu_selected;
        $this->submenu = $submenu_selected;
        $this->rol = $rol_id;
        $this->pdo = $pdo;
        //$this->agregaMenu('Galeria de carteles', 'galeria.php', 'ing-galeria');
    }
    
    //Agrega menú estático
    public function agregaMenu($texto, $link, $icono =''){
        $this->menuEstatico[] = array('nombre'=>$texto, 'link'=>$link, 'icono'=>$icono);
    }
    
    private function creaMenu($id, $nombre, $icono){
        $this->xtpl->assign('MENU_ID', $id);
        $this->xtpl->assign('MENU_NOMBRE', $nombre);
        if($icono != ''){
            $this->xtpl->assign('MENU_ICONO', $icono);
            $this->xtpl->parse('main.sidebar.menus.menu.icono');
        }
        if($this->menu == $id){
            $this->xtpl->parse('main.sidebar.menus.menu.activo');
            $this->xtpl->parse('main.sidebar.menus.menu.show');
        }else{
            $this->xtpl->parse('main.sidebar.menus.menu.inactivo');
            $this->xtpl->parse('main.sidebar.menus.menu.collapsed');
        }
        
    }
    
    public function printMenu($titulo = '', $index = false){
        require_once('include/xtemplate.class.php');
        $this->xtpl = new XTemplate('templates/main_menu.tpl.html');        
        $rutaLogo = 'img/lasalle-logo.svg';
        $this->xtpl->assign('LOGO_IMG', $rutaLogo);
        $links = array();
        array_push($links,array('nombre' => 'Facultad de Ingeniería', 'pag' => 'https://ingenieria.lasalle.mx/'));
        array_push($links,array('nombre' => 'La Salle', 'pag' => 'https://lasalle.mx'));
        array_push($links,array('nombre' => 'Moodle', 'pag' => 'https://moodle.ingenierialasalle.mx/'));
        foreach($links as $link){
            $this->xtpl->assign('SISTEMA_LINK', $link['pag']);
            $this->xtpl->assign('SISTEMA_NOMBRE', $link['nombre']);
            $this->xtpl->parse('main.sidebar.sistemas.sistema');
            $this->xtpl->parse('main.sistemas.list.sistema');
        }
        if ($this->rol == 0){
            $this->xtpl->assign('MODAL', 'modalInicia');
            $this->xtpl->parse('main.sidebar.modal');
        } else{
            $this->xtpl->assign('SALIR', 'salir.php');
            $this->xtpl->parse('main.sidebar.salir');
        }       
        $this->xtpl->parse('main.sistemas.list');

        $this->xtpl->parse('main.sistemas');
        $this->xtpl->parse('main.sidebar.sistemas');
        
        $menuArr = array();
        if ($this->rol != 0 && !is_null($this->pdo)) {
            try {
                $stmt = $this->pdo->prepare('SELECT * FROM alu_fs_menuspagina(:rol)');
                $stmt->bindParam(':rol', $this->rol);
                if($stmt->execute())
                    $menuArr = $stmt->fetchAll();
            } catch (\PDOException $ex) {
                echo 'Error al imprimir el submenú';
                print_r($stmt->errorInfo());
                exit();
            }            
            $stmt->closeCursor();
            $stmt = null;
        }
        if(!$index){
            //Inicio por default           
            $this->xtpl->assign('MENU_ID', 0);
            if ($_SESSION['RolUsuario_desc'] == 'ALUMNO')
                $this->xtpl->assign('MENU_LINK', 'home.php');            
            else
                $this->xtpl->assign('MENU_LINK', 'home_admin.php');
            $this->xtpl->assign('MENU_NOMBRE', 'Home');
            $this->xtpl->assign('MENU_ICONO', 'ing-home');
            $this->xtpl->parse('main.sidebar.menus.menu_link.icono');
            if($this->menu == 0){
                $this->xtpl->parse('main.sidebar.menus.menu_link.activo');
            }
            $this->xtpl->parse('main.sidebar.menus.menu_link');
            if(count($this->menuEstatico)>0){
                foreach($this->menuEstatico as $menu){
                    $this->xtpl->assign('MENU_LINK', $menu['link']);
                    $this->xtpl->assign('MENU_NOMBRE', $menu['nombre']);
                    if($menu['icono'] != ''){
                        if (str_starts_with($menu['icono'], 'fa-'))
                            $prefijo = 'fa-fw ';
                        else
                            $prefijo = 'ing-fw ';
                        $this->xtpl->assign('MENU_ICONO', $prefijo . $menu['icono']);
                        $this->xtpl->parse('main.sidebar.menus.menu_link.icono');
                    }
                    $this->xtpl->parse('main.sidebar.menus.menu_link');
                }            
            }

            //Recorre menus
            $lastMenu = -1;
            $numMenus = 0;
            foreach($menuArr as $menu){
                if($lastMenu == -1){//Sin menú anterior
                    $lastMenu = $menu['Menu_id'];
                    $numMenus++;
                    $this->creaMenu($menu['Menu_id'], $menu['Menu_desc'], $menu['Menu_icono']);
                }else if($lastMenu != $menu['Menu_id']){//Cambio de menú
                    $this->xtpl->parse('main.sidebar.menus.menu');//guarda menu anterior

                    $lastMenu = $menu['Menu_id'];
                    $numMenus++;
                    $this->creaMenu($menu['Menu_id'], $menu['Menu_desc'], $menu['Menu_icono']);
                }
                $this->xtpl->assign('MENU_ID', $menu['Menu_id']);
                $this->xtpl->assign('SUBMENU_LINK', $menu['Submenu_pag_base']);
                $this->xtpl->assign('SUBMENU_NOMBRE', $menu['Submenu_desc']);
                if (str_starts_with($menu['Submenu_icono'], 'fa-'))
                            $prefijo = 'fa-fw ';
                        else
                            $prefijo = 'ing-fw ';
                $this->xtpl->assign('SUBMENU_ICONO', $prefijo . $menu['Submenu_icono']);
                if ($this->submenu == $menu['Submenu_id'])
                    $this->xtpl->parse('main.sidebar.menus.menu.submenu.activo');
                $this->xtpl->parse('main.sidebar.menus.menu.submenu');
            }
            if(count($menuArr)>0){
                $this->xtpl->parse('main.sidebar.menus.menu');
            }
            //'Compilar' resultados
            $this->xtpl->parse('main.sidebar.menus');
            $this->xtpl->parse('main.sidebar');
            if ($this->rol == 0){
                $this->xtpl->assign('MODAL', 'modalInicia');
                $this->xtpl->parse('main.menu_index.modal');
                $this->xtpl->parse('main.menu_index');
            } else{
                $this->xtpl->assign('SALIR', 'salir.php');
                $this->xtpl->parse('main.menu_sidebar.salir');
                $this->xtpl->parse('main.menu_sidebar');
            }
        } 
        if ($this->menu != 0) {
            $this->xtpl->assign('TITULO', $titulo);
            $this->xtpl->parse('main.barra');
        }
        $this->xtpl->parse('main');
        $this->xtpl->out('main');
    }    
    /* 
     * @return string Texto con el código HTML del menú
     */
    public function textMenu(){
        return $this->xtpl->text('main');
    }
    
    private function getFirstDirectory($pag){
        if(dirname($pag) == '/'){
            return $pag;
        }
        return $this->getFirstDirectory(dirname($pag));
    }
}
