<?php
session_start();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "nice_finance";

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        $_SESSION['username'] = $username; // Setze die Benutzersitzung
        header("Location: dashboard.php");
    } else {
        header("Location: login.php?error=1");
    }

    $conn->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .login-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
            max-width: 90%;
        }

        .login-container h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #354798;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            font-weight: bold;
            margin-bottom: 5px;
            color: #354798;
        }

        .form-group input {
            width: calc(100% - 20px); /* Abzüglich des Padding links und rechts */
            padding: 10px;
            border-radius: 5px;
            border: 1px solid #ccc;
            margin: 0 auto; /* Zentrieren der Eingabefelder */
            display: block; /* Damit die Eingabefelder nicht nebeneinander sind */
        }

        .btn-login {
            width: 100%;
            padding: 10px;
            border-radius: 5px;
            border: none;
            background-color: #354798;
            color: #fff;
            font-weight: bold;
            cursor: pointer;
        }

        .btn-login:hover {
            background-color: #2a3e94;
        }
    </style>
</head>
<body>
    <div class="login-container">
        <h2>Login</h2>
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
            <div class="form-group">
                <label for="username">Benutzername:</label>
                <input type="text" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label for="password">Passwort:</label>
                <input type="password" id="password" name="password" required>
            </div>
            <button type="submit" class="btn-login">Anmelden</button>
        </form>
    </div>
</body>
</html>
