<?php

require_once __DIR__ . '/functions.php';

$pdo = app_db_pdo();
$token = trim((string)($_POST['token'] ?? $_GET['token'] ?? ''));
$errors = [];
$success = false;
$tokenValid = $token !== '' && saas_fetch_valid_auth_token($pdo, $token, 'password_reset') !== null;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    app_require_csrf();
    $result = saas_reset_password_with_token(
        $pdo,
        $token,
        (string)($_POST['password'] ?? ''),
        (string)($_POST['password_confirm'] ?? '')
    );

    if ($result['ok']) {
        $success = true;
        $tokenValid = false;
    } else {
        $errors = $result['errors'];
        $tokenValid = $token !== '' && saas_fetch_valid_auth_token($pdo, $token, 'password_reset') !== null;
    }
}

?>

<!DOCTYPE HTML>
<html lang="de">
    <head>
        <title>Kaffeeliste Passwort zurücksetzen</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
        <link rel="stylesheet" href="assets/css/main.css" />
        <link rel="stylesheet" href="assets/css/public.css" />
    </head>
    <body class="is-preload public-page">
        <div class="public-shell">
            <section class="public-auth">
                <nav class="public-nav" aria-label="Hauptnavigation">
                    <strong><a href="landing.php">Kaffeeliste</a></strong>
                    <ul class="actions">
                        <li><a href="login.php" class="button">Login</a></li>
                    </ul>
                </nav>

                <div class="public-auth-grid">
                    <div class="public-auth-copy">
                        <h1>Passwort zurücksetzen</h1>
                        <p>Setze ein neues Passwort für dein Kaffeeliste-Konto.</p>
                    </div>

                    <div class="public-panel">
                        <h2>Neues Passwort</h2>

                        <?php if ($success): ?>
                            <div class="hint-box success"><p>Das Passwort wurde geändert.</p></div>
                            <ul class="actions">
                                <li><a href="login.php" class="button primary">Zum Login</a></li>
                            </ul>
                        <?php else: ?>
                            <?php if ($errors !== []): ?>
                                <div class="hint-box error">
                                    <?php foreach ($errors as $error): ?>
                                        <p><?php echo saas_html($error); ?></p>
                                    <?php endforeach; ?>
                                </div>
                            <?php elseif (!$tokenValid): ?>
                                <div class="hint-box error"><p>Der Link ist ungültig oder abgelaufen.</p></div>
                            <?php endif; ?>

                            <?php if ($tokenValid): ?>
                                <form method="post" action="passwort-zuruecksetzen.php">
                                    <?php echo app_csrf_field(); ?>
                                    <input type="hidden" name="token" value="<?php echo saas_html($token); ?>">
                                    <div class="row">
                                        <div class="col-6 col-12-small">
                                            <label for="password">Neues Passwort</label>
                                            <input type="password" name="password" id="password" minlength="8" required>
                                        </div>
                                        <div class="col-6 col-12-small">
                                            <label for="password_confirm">Passwort wiederholen</label>
                                            <input type="password" name="password_confirm" id="password_confirm" minlength="8" required>
                                        </div>
                                    </div>
                                    <ul class="actions">
                                        <li><button type="submit" class="primary">Passwort speichern</button></li>
                                    </ul>
                                </form>
                            <?php else: ?>
                                <ul class="actions">
                                    <li><a href="passwort-vergessen.php" class="button primary">Neuen Link anfordern</a></li>
                                </ul>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                </div>
            </section>
        </div>

        <script src="assets/js/jquery.min.js"></script>
        <script src="assets/js/browser.min.js"></script>
        <script src="assets/js/breakpoints.min.js"></script>
        <script src="assets/js/util.js"></script>
        <script src="assets/js/main.js"></script>
    </body>
</html>
