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

if (!isset($_SESSION['userID'])) {
    header("Location: /login");
    http_response_code(403);
}

if (isset($_POST['delete-account']) && $_POST['delete-account'] == "delete-account") {
    $sqlRequest = "DELETE FROM users WHERE ID = :userID";
    $request = $pdo->prepare($sqlRequest);
    $request->bindParam(":userID", $_SESSION['userID']);
    if($request->execute()) {
        $sqlRequest = "UPDATE articles SET author = 0 WHERE author = :userID";
        $request = $pdo->prepare($sqlRequest);
        $request->bindParam(":userID", $_SESSION['userID']);
        if($request->execute()) {
            $status = "Votre compte a été supprimé, votre session reste active jusqu'à déconnexion";
        } else {
            $status = "Erreur lors du passage de propriétés";
        }
    } else {
        $status = "Erreur lors de la suppression utilisateur";
    }
}

?>

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

    <head>
        <?php fillHead($rootPageURL, $pageTitle, $darkTheme, $lightTheme);?>
        <style>
            .button, button {
                margin: 0 1em;
                font-size: 1.5em;
            }
            .choice {
                display: flex;
                align-items: space-between;
            }

            #yes {
                color: #ff0000;
            }

            #no {
                color: #00ff00;
            }
        </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">Etes vous sur de vouloir supprimer votre compte</div>
                        <div class="status"><?=$status?></div>
                        <div class="choice">
                            <a href="/settings" class="button" id="no">Non</a>
                            <form action="#"><input type="hidden" name="delete-account" value="delete-account"><button type="submit" id="yes">Oui</button></form>
                        </div>
                    </div>
                </form>
            </div>
        </main>
        <footer>
            <div class="panel-content">
                <?php fillFooter($footerText);?>
            </div>
        </footer>
    </body>
</html>