<?php
require "../include/variables.php";
require "../include/functions.php";

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

if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['password2'])) {
    if(empty($_POST['username']) == false && empty($_POST['password']) === false && empty($_POST['password2']) === false) {
        if($_POST['password'] == $_POST['password2']) {

            $display_name = isset($_POST['display_name']) ? $_POST['display_name'] : $_POST['username'];
            $email = isset($_POST['email']) ? $_POST['email'] : "";

            $bdd = connect($dbhost, $dbname, $dbuser, $dbpass);
            $req = $bdd->prepare("SELECT ID FROM users WHERE username = :username");
            $req->bindParam(":username", htmlspecialchars($_POST['username']));
            $req->execute();
            $resultat = $req->fetchAll(PDO::FETCH_ASSOC);
            if($resultat) {
                $status = "Erreur, le nom d'utilisateur existe déjà.";
            } else {
                $req = $bdd->prepare("INSERT INTO users (username, password, email, display_name, role,  accreditation) VALUES (:username, :password, :email, :display_name, 0, 0)");
                $req->bindParam(':username', htmlspecialchars($_POST['username']));
                $req->bindParam(':password', md5($_POST['password']));
                $req->bindParam(':email', htmlspecialchars($_POST['email']));
                $req->bindParam(':display_name', htmlspecialchars($_POST['display_name']));
                if ($req->execute()) {
                    header("Location: login.php");
                    exit();
                } else {
                    $status = "Erreur SQL";
                }
            }
        } else {
            $status = "Les mots de passe ne correspondent pas.";
        }
    }
}

?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./src/css/style.css">
    <style>
    @media (prefers-color-scheme: dark) {
        .body {
            <?=$theme_dark?>
        }
    } 
    @media (prefers-color-scheme: light) {
        .body {
            <?=$theme_light?>
            }
        }
    </style>
    <link rel="icon" href="./src/img/favicon.ico">
    <title><?=$title?></title>
</head>
<body class="body">
    <header>
        <div class="pancontent">
            <div class="athena-container">
                <a href="<?=$root?>" class="athena-link">
                    <img src="./src/img/athena-mono.png" class="athena">
                </a>
            </div>
            <div class="content">
                <div>
                    <div class="main-title">
                        <div class="title"><?=$header_title?></div>
                        <div class="subtitle"><?=$header_subtitle?></div>
                    </div>
                </div>
            </div>
        </div>
    </header>
    <nav>
        <div class="pancontent">
            <?php nav($nav);?>
        </div>
    </nav>
    <main>
        <div class="content">
            <div><form action="#" method="post">
                <div>
                    <div><input type="text" name="display_name" placeholder="Nom d'affichage"></div>
                    <div><input type="email" name="email" placeholder="Adresse email"></div>
                    <div><input type="text" name="username" placeholder="Nom d'utilisateur" required></div>
                    <div><input type="password" name="password" placeholder="Mot de Passe" required></div>
                    <div><input type="password" name="password2" placeholder="Confirmez votre mot de passe" required></div>
                    <div><input type="submit" value="Connexion"></div>
                    <div class="status"><?=$status?></div>
                </div>
            </div></form>
        </div>
    </main>
    <footer>
        <div class="content">
            <div>
                <div><?=$copyright?></div>
            </div>
        </div>
    </footer>
</body>
</html>