<?php
// register.php - Seite zur Benutzerregistrierung
// KORREKTUR: Vollständiges Design hinzugefügt, um es an das Login-Seiten-Layout anzugleichen.

$page_title = "Registrierung";

if (session_status() == PHP_SESSION_NONE) {
    session_start();
}
require_once 'db_connect.php';
$message = '';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);
    $confirm_password = trim($_POST['confirm_password']);

    $errors = [];
    if (empty($username)) {
        $errors[] = "Der Benutzername darf nicht leer sein.";
    } elseif (strlen($username) < 3) {
        $errors[] = "Der Benutzername muss mindestens 3 Zeichen lang sein.";
    }

    if (empty($password)) {
        $errors[] = "Das Passwort darf nicht leer sein.";
    } elseif (strlen($password) < 6) {
        $errors[] = "Das Passwort muss mindestens 6 Zeichen lang sein.";
    }

    if ($password !== $confirm_password) {
        $errors[] = "Die Passwörter stimmen nicht überein!";
    }

    if (!empty($errors)) {
        $message = '<div class="alert alert-danger" role="alert">' . implode('<br>', $errors) . '</div>';
    } else {
        $hashed_password = password_hash($password, PASSWORD_DEFAULT);

        $stmt_check = $conn->prepare("SELECT id FROM users WHERE username = ?");
        $stmt_check->bind_param("s", $username);
        $stmt_check->execute();
        $stmt_check->store_result();

        if ($stmt_check->num_rows > 0) {
            $message = '<div class="alert alert-danger" role="alert">Dieser Benutzername ist bereits vergeben.</div>';
        } else {
            $stmt_insert = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
            $stmt_insert->bind_param("ss", $username, $hashed_password);

            if ($stmt_insert->execute()) {
                $message = '<div class="alert alert-success" role="alert">Registrierung erfolgreich! Sie können sich jetzt <a href="login.php" class="alert-link">anmelden</a>.</div>';
            } else {
                error_log("Registrierungsfehler: " . $stmt_insert->error);
                $message = '<div class="alert alert-danger" role="alert">Ein interner Fehler ist aufgetreten. Bitte versuchen Sie es später erneut.</div>';
            }
            $stmt_insert->close();
        }
        $stmt_check->close();
    }
}
$conn->close();
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Registrierung - Trekking Packliste</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Nunito+Sans:wght@400;600;700&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <div class="auth-wrapper">
        <div class="auth-container">
            <div class="auth-header">
                <img src="./logo.png" alt="Trekking Packliste Logo" class="auth-logo">
                <h2>Registrieren</h2>
            </div>
            <div class="auth-body">
                <?php if(!empty($message)) echo $message; ?>
                <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                    <div class="mb-3">
                        <label for="username" class="form-label">Benutzername</label>
                        <input type="text" class="form-control" id="username" name="username" required>
                    </div>
                    <div class="mb-3">
                        <label for="password" class="form-label">Passwort</label>
                        <input type="password" class="form-control" id="password" name="password" required>
                    </div>
                    <div class="mb-3">
                        <label for="confirm_password" class="form-label">Passwort bestätigen</label>
                        <input type="password" class="form-control" id="confirm_password" name="confirm_password" required>
                    </div>
                    <button type="submit" class="btn btn-primary w-100 mt-3">Konto erstellen</button>
                </form>
                <div class="text-center mt-4">
                    <span class="text-muted">Bereits registriert?</span> <a href="login.php" class="fw-bold text-decoration-none" style="color: var(--color-primary);">Hier anmelden</a>
                </div>
            </div>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
</body>
</html>
