<?php

class ControllerLoginAdmin
{
    private $gatewayAdministrator;

    function __construct()
    {
        global $dns, $user, $pass, $vues, $twig;
        session_start();
        try {

            $con = new Connection($dns, $user, $pass);

            $this->gatewayAdministrator = new GatewayAdministrator($con);
            
            echo $twig->render($vues["loginAdmin"], [
                'error' => $_SESSION["error"],
            ]);

            $_SESSION["error"]="";

        } catch (PDOException $e) {
            // $dataVueEreur[] = "Erreur inattendue!!! ";
            // require(__DIR__.'/../vues/erreur.php');
        } catch (Exception $e2) {
            // $dataVueEreur[] = "Erreur inattendue!!! ";
            // require ($rep.$vues['erreur']);
        }
    }

    function verify(){
        $username = $_POST['username'];
        $password = $_POST['password'];

        $Administrator = new Administrator($username, $password);
        
        $AdministratorIsOk = $this->gatewayAdministrator->verifyAdministrator($Administrator);
        if($AdministratorIsOk != null) {
            $_SESSION["idAdminConnected"]=$AdministratorIsOk;
            header("Location:/admin/administrators");
        } 
        else {
            $_SESSION["error"]="utilisateur introuvable.";
            header("Location:/login");
        }
    }
}
