<?php
session_start();

if (!isset($_SESSION['set_pw_user'])) {
    header("location: index.php");
    exit;
}

require_once "config/config.php";

$pw1 = $pw2 = "";
$pw_err = "";

if ($_SERVER["REQUEST_METHOD"] === "POST") {

    $pw1 = $_POST['password1'] ?? '';
    $pw2 = $_POST['password2'] ?? '';

    if (strlen($pw1) < 5) {
        $pw_err = "Passwort muss mindestens 5 Zeichen lang sein.";
    } elseif ($pw1 !== $pw2) {
        $pw_err = "Passwörter stimmen nicht überein.";
    } else {

        $hash = password_hash($pw1, PASSWORD_DEFAULT);

        $stmt = $pdo->prepare("
            UPDATE users
            SET password_hash = :hash,
                first_login = 0
            WHERE id = :id
        ");

        $stmt->execute([
            ':hash' => $hash,
            ':id'   => $_SESSION['set_pw_user']
        ]);

        unset($_SESSION['set_pw_user']);

        header("location: index.php");
        exit;
    }
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Passwort setzen – ANJUMA</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
body {
    background: #ffffff;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Segoe UI', sans-serif;
    margin: 0;
    overflow-x: hidden;
}

.login-card {
    width: 90%;
    max-width: 380px;
    background: #fff;
    border-radius: 16px;
    padding: 30px;
    box-shadow: 0 6px 25px rgba(0,0,0,0.08);
    text-align: center;
    animation: fadeIn 0.4s ease-out;
    border: 1px solid #f1f1f1;
}

.login-logo {
    width: 200px;
    max-width: 100%;
    margin-bottom: 12px;
}

.form-control {
    border-radius: 10px;
    padding: 12px;
    border: 1px solid #dcdcdc;
    font-size: 15px;
}

.form-control:focus {
    border-color: #0d6efd;
    box-shadow: 0 0 0 2px rgba(13,110,253,0.15);
}

.btn-login {
    width: 100%;
    padding: 12px;
    border-radius: 10px;
    font-size: 16px;
    background: #0d6efd;
    color: white;
    font-weight: 600;
    transition: 0.2s;
}

.btn-login:hover {
    background: #0b5ed7;
    transform: translateY(-2px);
}

.error {
    color: #d9534f;
    font-size: 0.85rem;
    margin-top: 6px;
    text-align: left;
}
</style>
</head>

<body>

<div class="login-card">

    <img src="ANJUMA_LOGO.png" class="login-logo" alt="ANJUMA Logo">

    <h4 class="mb-2">Passwort festlegen</h4>
    <p class="text-muted mb-4" style="font-size: 0.9rem;">
        Bitte setze dein persönliches Passwort für das ANJUMA Dashboard.
    </p>

    <form method="post">

        <div class="text-start mb-3">
            <label class="mb-1">Neues Passwort</label>
            <input type="password" name="password1" class="form-control" required>
        </div>

        <div class="text-start mb-4">
            <label class="mb-1">Passwort wiederholen</label>
            <input type="password" name="password2" class="form-control" required>
            <?php if (!empty($pw_err)) echo "<div class='error'>$pw_err</div>"; ?>
        </div>

        <button type="submit" class="btn btn-login">
            Passwort speichern
        </button>

    </form>

</div>

</body>
</html>
