<?php include_once "include/default-values.php" ?>
<?php
$user;
$message;
if ($_SERVER["REQUEST_METHOD"] === "POST") {
    if (isset($_POST["username"]) && isset($_POST["password"])) {
        $username = $_POST["username"];
        $res = $db_con->query("SELECT * FROM users WHERE Username = '$username'");
        $row = $res->fetch_assoc();
        $user = $row;


        if (isset($row)) {
            if ($row["Password"] === $_POST["password"]) {
                $_SESSION["username"] = $username;
                $_SESSION["id"] = $row["ID"];
                header("Location: " . $siteroot . "logged-in.php");
                exit();
            } else {
                $message .= "password is incorrect\n";
            }
        } else {
            $message .= "username is incorrect\n";
        }
    }
}

?>

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>EDEA - Login</title>
    <link rel="stylesheet" href="<?= $siteroot; ?>css/style.css">
    <link rel="stylesheet" href="<?= $siteroot; ?>css/forms.css">
</head>

<body>
    <?php include "include/header.php"; ?>
    <main>
        <h1>Login</h1>
        <?php
        if (isset($message)) {
            echo "<pre>$message</pre>";
        }
        ?>
        <form method="post">
            <div class="input-data required">
                <input type="text" id="username" name="username" required>
                <div class="underline"></div>
                <label for="username">Brugernavn</label>
            </div>
            <div class="input-data required">
                <input type="password" id="password" name="password" required>
                <div class="underline"></div>
                <label for="password">Adgangskode</label>
            </div>
            <div class="input-submit">
                <button>Login</button>
            </div>
        </form>
    </main>
    <?php include "include/footer.php"; ?>
    <script src="<?= $siteroot; ?>js/forms.js"></script>
</body>

</html>