<?php
require_once "includes/db_connect.php";
$username = $password = $confirm_password = "";
$username_err = $password_err = $confirm_password_err = "";
if($_SERVER["REQUEST_METHOD"] == "POST"){
    if(empty(trim($_POST["username"]))){ $username_err = "Bitte geben Sie einen Benutzernamen ein.";
    } else{
        $sql = "SELECT id FROM users WHERE username = ?";
        if($stmt = $mysqli->prepare($sql)){
            $stmt->bind_param("s", $param_username); $param_username = trim($_POST["username"]);
            if($stmt->execute()){
                $stmt->store_result();
                if($stmt->num_rows == 1){ $username_err = "Dieser Benutzername ist bereits vergeben."; } else{ $username = trim($_POST["username"]); }
            } else{ echo "Oops! Etwas ist schief gelaufen."; }
            $stmt->close();
        }
    }
    if(empty(trim($_POST["password"]))){ $password_err = "Bitte geben Sie ein Passwort ein."; } elseif(strlen(trim($_POST["password"])) < 6){ $password_err = "Das Passwort muss mindestens 6 Zeichen lang sein."; } else{ $password = trim($_POST["password"]); }
    if(empty(trim($_POST["confirm_password"]))){ $confirm_password_err = "Bitte bestätigen Sie das Passwort."; } else{ $confirm_password = trim($_POST["confirm_password"]); if(empty($password_err) && ($password != $confirm_password)){ $confirm_password_err = "Die Passwörter stimmen nicht überein."; } }
    if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
        $sql = "INSERT INTO users (username, password_hash) VALUES (?, ?)";
        if($stmt = $mysqli->prepare($sql)){
            $stmt->bind_param("ss", $param_username, $param_password);
            $param_username = $username;
            $param_password = password_hash($password, PASSWORD_DEFAULT);
            if($stmt->execute()){ header("location: login.php"); } else{ echo "Oops! Etwas ist schief gelaufen."; }
            $stmt->close();
        }
    }
    $mysqli->close();
}
define('APP_VERSION', '8.3.0');
?>
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Registrierung - Cazubu</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/style.css?v=<?php echo APP_VERSION; ?>">
</head>
<body>
    <div class="auth-container">
        <div class="auth-form-wrapper">
            <div class="text-center mb-4"><img src="logo.png" alt="Cazubu Logo" style="max-width: 120px;"><h2 class="mt-2">Neues Konto erstellen</h2></div>
            <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" novalidate>
                <div class="mb-3"><label for="username" class="form-label">Benutzername</label><input type="text" name="username" id="username" class="form-control <?php echo (!empty($username_err)) ? 'is-invalid' : ''; ?>" value="<?php echo htmlspecialchars($username); ?>" required><div class="invalid-feedback"><?php echo $username_err; ?></div></div>
                <div class="mb-3"><label for="password" class="form-label">Passwort</label><input type="password" name="password" id="password" class="form-control <?php echo (!empty($password_err)) ? 'is-invalid' : ''; ?>" required><div class="invalid-feedback"><?php echo $password_err; ?></div></div>
                <div class="mb-3"><label for="confirm_password" class="form-label">Passwort bestätigen</label><input type="password" name="confirm_password" id="confirm_password" class="form-control <?php echo (!empty($confirm_password_err)) ? 'is-invalid' : ''; ?>" required><div class="invalid-feedback"><?php echo $confirm_password_err; ?></div></div>
                <div class="d-grid"><button type="submit" class="btn btn-primary">Konto erstellen</button></div>
                <p class="text-center mt-3">Haben Sie bereits ein Konto? <a href="login.php">Hier anmelden</a></p>
            </form>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
