<?php
class Main{
    private $login;
    
    private $secao;
    private $acao;
    
    private $requisicao;
    
    public function __construct($secao = '', $acao = '', $requisicao = 'get') {
        self::logado();
        
        $this->secao      = ($secao != '') ? $secao : filter_input(INPUT_GET, 'secao');
        $this->acao       = $acao;    
        $this->requisicao = $requisicao;
        
        self::loadCabecalho();
        
        if($this->login instanceof stdClass){
            self::loadMenu();
        }else{
            self::login();
        }
        self::loadRodape();
        return;
    }
    
    private function loadCabecalho(){
        if($this->secao == 'sair') self::sair();
        self::load('view/pages/_cabecalho.inc.html');
        return;
    }
    private function loadMenu(){
        self::load('view/pages/_menu.inc.php');
        return;
    }
     
    private function loadRodape(){
        self::load('view/pages/_rodape.inc.html');
        return;
    }
    /* Paginas  e manipuladores */
    protected function inicio(){
        self::load('view/pages/inicio.html');
    }
    protected function login(){ 
        self::load('view/pages/login.html');
        return;
    }

    protected function logado(){
        if(is_array($_SESSION['login'])){
            $this->login = new stdClass();
            $this->login->ModuloAtual = new stdClass();
            
            $this->login->idUsuario   = $_SESSION['login']['idUsuario'];
            $this->login->Nome        = $_SESSION['login']['Nome'];
            $this->login->Nivel       = $_SESSION['login']['Nivel'];
            $this->login->Modulos     = $_SESSION['login']['Modulos'];
            $this->login->Config      = $_SESSION['login']['configs'];
            $this->login->timestamp   = $_SESSION['login']['timestamp'];
            
            if(isset($_SESSION['login']['ModuloAtual'])){
                $this->login->ModuloAtual->id   = $_SESSION['login']['ModuloAtual']['id'];
                $this->login->ModuloAtual->Nome = $_SESSION['login']['ModuloAtual']['Nome'];
            }else{
                $this->login->ModuloAtual->Nome = 'Selecione um módulo';
            }
            return true;
        }else{ 
            return false;
        }
    }
    
    protected function load($url){
        require_once BASE.APP.$url;
        return;
    }
    protected function loadScript($jsfile){
        echo '<script type="text/javascript" src="'.BASE.APP.$jsfile.'">';
        echo '</script>';
        return;
    }


    public function sair(){
        unset($_SESSION['login']);
        session_unset();
        session_destroy();
        
        echo "<script>\n";
        echo "window.location.href='../sis/'";
        echo "</script>\n";
        exit();
    }
}