<!DOCTYPE html>
<html lang="en">
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start();
if (!empty($_SESSION['username'])) {
    header('Location: /admin/securenotes.php');
    exit();
}

?>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NDF | LOGIN</title>
    <link rel="stylesheet" href="/static/css/stylesheet.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
</head>
<body>
    <div class="wrapper">
        <div class="header-bar"></div>
        <form id="loginForm" method="POST" action="index.php">
            <h1>NDF ACCESS</h1>
            <label for="username">Username</label>
            <input type="text" id="username" name="username" required>

            <label for="password">Password</label>
            <input type="password" id="password" name="password" required>

            <input type="submit" value="Login">
           <?php
                if (!empty($_POST)) {
                    $name = $_POST['username'];
                    $password = $_POST['password'];
                    if (empty($name)) {
                        echo '<div class="error-message">Username is empty.</div>';
                    } else {
                        $servername = "db";
                        $username = "ctf";
                        $password_db = "39gknzLD";
                        $dbname = "app";
                        $conn = new mysqli($servername, $username, $password_db, $dbname);
                        $sql = "SELECT username, pass FROM users WHERE username='$name' AND pass='$password'";
                        $result = $conn->query($sql);
                        if ($result->num_rows > 0) {
                            session_regenerate_id(true);
                            $_SESSION['username'] = $name;
                            header('Location: /admin/securenotes.php');
                            exit();
                        } else {
                            echo '<div class="error-message">Wrong username or password!</div>';
                        }
                    }
                }
            ?>
        </form>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js" integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous"></script>
</body>
</html>
