<?php
include_once '../classes/pagina.php';

class Login extends Pagina {
    
    public function __construct() {
        parent::__construct();
    }
    
    public function corpo() {
        if($_REQUEST[sair] == 'sim'){
            unset($_SESSION['imediaco_geo']);
            unset($_SESSION);
            session_unset();
            session_destroy();
        }
        
        if(!empty($_POST[uid]) AND !empty($_POST[pwd])){
            $sql = "SELECT * FROM imediaco_geo.usuario WHERE email = '$_POST[uid]' AND senha = md5('$_POST[pwd]')";
            $this->bd->executarSQL($sql);
            $obj = $this->bd->getObjeto();
            if(!empty($obj->id)){
                $_SESSION[uid] = $_POST[uid];
                $_SESSION[email] = $_POST[uid];
                $_SESSION[nome] = $_POST[nome];
                
                $sql = "SELECT prova.*, provafase.id AS provafase_id "
                        . "FROM imediaco_geo.prova "
                        . "INNER JOIN imediaco_geo.provafase ON prova.id = provafase.prova_id "
                        . "WHERE prova.status = 'ATIVO' "
                        . "ORDER BY prova.datainscricaoinicio1 DESC, provafase.fase ASC "
                        . "LIMIT 1";//a ultima prova
                $this->bd->executarSQL($sql);
                $objprova = $this->bd->getObjeto();
                $_SESSION[prova_id] = $objprova->id;
                $_SESSION[provafase_id] = $objprova->provafase_id;
                
                $perfis = explode(',', $obj->perfil);
                foreach ($perfis as $perfil){
                    $_SESSION[role][] = trim($perfil);
                }
                
                echo "<meta http-equiv = 'Refresh' content = '0;URL=home.php' />";
                exit();
            }else{
                echo "<div class='erro'>Login e Senha não compativeis!</div>";
            }
        }
        
        $this->formulario();
    }
    
    public function formulario(){
        echo "<fieldset>";
        echo "<h1>Autenticação na área administrativa</h1>";
        echo "<form action='login.php' method='POST'>";
        echo "<table class='formulario'>";
        echo "<tr><th>Login (E-mail):</th><td><input type='text' name='uid' required size='50' /></td></tr>";
        echo "<tr><th>Senha:</th><td><input type='password' name='pwd' required /></td></tr>";
        echo "</table>";
        echo "<div align='center'><input type='submit' name='entrar' value='Entrar' class='confirmar' /></div>";
        echo "</fieldset>";
        echo "</form>";
    }
}

$pagina = new Login();
$pagina->montar();
