<?php

class ControllerAdministrator
{

    function __construct()
    {
        global $dsn, $user, $pass, $vues;
        session_start();
        try {
            $action = "";
            if (isset($_REQUEST['action'])) {
                $action = $_REQUEST['action'];
            }

            switch ($action) {
                case "goToAdministratorConnexion":
                    require($vues['AdministratorConnexion']);
                    break;
                case "goToAdministration":
                    require($vues['Administration']);
                    break;
                case "verifValidation":
                    $this->validationConnexion();
                    break;
                case "quitterAdministrator":
                    $Administrator->deconnexion();
                    header("location: index.php");
                    break;
                case "AjouterQuestion":
                case "SupprimerQuestion":
                case "ModifierQuestion":
                case "AjouterReponse":
                case "SupprimerReponse":
                case "ModifierReponse":
                case "AjouterAdmin":
                case "SupprimerAdmin":
                default:
                    break;
            }
        } catch (PDOException $e) {
            // $dataVueEreur[] = "Erreur inattendue!!! ";
            // require(__DIR__.'/../vues/erreur.php');
        } catch (Exception $e2) {
            // $dataVueEreur[] = "Erreur inattendue!!! ";
            // require ($rep.$vues['erreur']);
        }
    }

    function validationConnexion()
    {
        global $vues;
        $validation = new Validation();
        $error = [];
        $validation->val_form($_POST['name'], $_POST['password'], $error);
        foreach ($error as $key) {
            print($key);
        }

        if (empty($error)) {
            $validation = $Administrator->connection($_POST['name'], $_POST['password']);
            if (!empty($validation)) {
                header("location: index.php?action=goToAdministratoristration");
            } else {
                header("location: index.php?action=goToAdministratorConnexion");
            }
        } else {
            header("location: index.php?action=goToAdministratorConnexion");
        }
    }
}
