<?php
require("../../include/variables.php");
require("../../include/functions.php");
$pdo = sqlConnect($sqlDatabaseHost, $sqlDatabaseName, $sqlDatabaseUser, $sqlDatabasePass);

if (isset($_SESSION['userID'])) {
    header("Location: index.php");
    die("Vous êtes déjà connecté");
}

if (isset($_POST['username']) && isset($_POST['password1']) && isset($_POST['password2'])) {
    if(empty($_POST['username']) == false && empty($_POST['password1']) === false && empty($_POST['password2']) === false) {
        if(preg_match('!\S!u', $_POST['username']) && preg_match('!\S!u', $_POST['password1']) && preg_match('!\S!u', $_POST['password2'])) {
            if($_POST['password1'] == $_POST['password2']) {

                if (empty($_POST['email']) === false && preg_match('!\S!u', $_POST['username']) == false) {
                    $status = "Caractères illégaux";
                } else {
                    $userEmail = isset($_POST['email']) ? strtolower($_POST['email']) : NULL;
                    $userDisplayName = htmlspecialchars($_POST['username']);
                    $userName = str_replace(" ", "_", strtolower(htmlspecialchars($_POST['username'])));

                    $sqlRequest = "SELECT ID FROM users WHERE username = :username";
                    $request = $pdo->prepare($sqlRequest);
                    $request->bindParam(":username", htmlspecialchars($_POST['username']));
                    $request->execute();
                    $result = $request->fetchAll(PDO::FETCH_ASSOC);

                    if($result) {
                        $status = "Le nom d'utilisateur existe déjà";
                    } else {

                        $request = $pdo->prepare("INSERT INTO users (username, password, email, display_name, level,  accreditation) VALUES (:username, :password, :email, :display_name, 1, 1)");
                        $request->bindParam(':username', $userName);
                        $request->bindParam(':password', md5($_POST['password1']));
                        $request->bindParam(':email', $userEmail);
                        $request->bindParam(':display_name', $userDisplayName);

                        if ($request->execute()) {
                            header("Location: index.php");
                            exit();
                        } else {
                            $status = "Erreur SQL";
                        }
                    }
                }
            } else {
                $status = "Les mots de passe ne correspondent pas";
            }
        } else {
            $status = "Caractères illégaux";
        }
    } else {
        $status = "Les champs ne doivent pas être vides";
    }
}
?>

<!DOCTYPE html>
<html lang="fr">

    <head>
        <?php fillHead($rootPageURL, $pageTitle, $darkTheme, $lightTheme);?>
        <style>
            /* CUSTOM STYLE */
        </style>
    </head>

    <body class="body">

        <header>
            <div class="panel-content">
                <?php fillHeader($rootPageURL, $headerTitle, $headerSubtitle);?>
            </div>
        </header>

        <nav>
            <div class="panel-content">
                <?php fillNav($rootPageURL);?>
            </div>
        </nav>

        <main>
            <div class="content">
                <form action="#" method="post">
                    <div class="form">
                        <div class="form-title">Créer un compte</div>
                        <div class="status"><?=$status?></div>
                        <?php 
                            textInput("text", "", "username", "Login", "");
                            textInput("email", "", "email", "Email (facultatif)", "");
                            textInput("password", "", "password1", "Mot de Passe", "");
                            textInput("password", "", "password2", "Confirmation du Mot de passe", "");
                        ?>
                        <button type="submit">Créer un compte</button>
                        <div>ou</div>
                        <a href="index.php" class="button">Se connecter</a>
                    </div>
                </form>
            </div>
        </main>
        <footer>
            <div class="panel-content">
                <?php fillFooter($footerText);?>
            </div>
        </footer>
    </body>
</html>