<?php
// reset.php
require_once __DIR__ . '/../Login/config.php';
if (session_status() === PHP_SESSION_NONE) session_start();

// Ambil param
$email = isset($_GET['email']) ? trim($_GET['email']) : '';
$token = isset($_GET['token']) ? trim($_GET['token']) : '';

$valid = false;
$msg = '';

if ($email === '' || $token === '') {
    $msg = "Link tidak lengkap. Pastikan Anda klik link yang benar dari email.";
} else {
    $stmt = $pdo->prepare("SELECT id, token_hash, expires_at, created_at 
                           FROM password_resets 
                           WHERE email = ? 
                           ORDER BY created_at DESC LIMIT 1");
    $stmt->execute([$email]);
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$row) {
        $msg = "Token tidak ditemukan atau sudah digunakan.";
    } else {
        $expires = strtotime($row['expires_at']);
        if ($expires < time()) {
            $msg = "Link sudah kadaluarsa. Silakan minta reset ulang.";
        } else {
            if (password_verify($token, $row['token_hash'])) {
                $valid = true;
                $_SESSION['password_reset_id']    = $row['id'];
                $_SESSION['password_reset_email'] = $email;
                $_SESSION['password_reset_token'] = $token;
            } else {
                $msg = "Token tidak valid.";
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="utf-8">
    <title>Reset Password - Pustakaia</title>

    <style>
        :root{--accent:#069797;}
        *{box-sizing:border-box;margin:0;padding:0}
        body{
            font-family:"Poppins",Arial,sans-serif;
            background:linear-gradient(135deg,#f9f9f9,#e9f5ec);
            color:#222;
            min-height:100vh;
            display:flex;
            flex-direction:column
        }

        /* HEADER */
        header{
            display:flex;
            justify-content:space-between;
            align-items:center;
            padding:15px 30px;
            background:#fff;
            border-bottom:1px solid #ddd;
            position:sticky;
            top:0;
            z-index:100;
            width:100%
        }
        .logo{
            font-size:20px;
            font-weight:bold;
            color:var(--accent);
            display:flex;
            align-items:center;
            gap:10px
        }

        /* MAIN */
        main{
            flex:1;
            display:flex;
            align-items:center;
            justify-content:center;
            padding:36px 20px
        }
        .box{
            width:100%;
            max-width:520px;
            background:#fff;
            padding:24px;
            border-radius:10px;
            box-shadow:0 6px 18px rgba(0,0,0,0.06)
        }
        h2{color:var(--accent);margin-bottom:12px}
        .msg{padding:12px;border-radius:8px;margin-bottom:12px}
        .msg.error{background:#ffecec;color:#900;border:1px solid #f5c2c2}
        .msg.info{background:#e8f7f6;color:#066;border:1px solid #bfeae8}
        label{display:block;margin-bottom:6px;color:#333}
        
        input{
            width:100%;
            padding:10px;
            border:1px solid #ddd;
            border-radius:6px;
            margin-bottom:10px
        }

        button{
            background:var(--accent);
            color:#fff;
            padding:10px 14px;
            border-radius:8px;
            border:0;
            cursor:pointer
        }

        /* FOOTER */
        footer{
            background:#fff;
            padding:20px 40px;
            font-size:14px;
            color:#222;
            border-top:1px solid #ddd;
            margin-top:auto
        }
        .footer-bottom{
            display:flex;
            justify-content:space-between;
            align-items:center;
            flex-wrap:wrap;
            font-size:13px
        }
        .footer-links a{
            margin-left:15px;
            color:#222;
            font-weight:500;
            text-decoration:none
        }
        .footer-links a:hover{text-decoration:underline}

        @media(max-width:700px){
            header,footer{padding:14px 18px};
            main{padding:20px 12px}
        }
    </style>
</head>
<body>

<header>
    <div class="logo">
        <a href="forgot.php" style="text-decoration:none;color:var(--accent);font-size:18px;margin-right:8px">⬅</a>
        📚 Pustakaia
    </div>
</header>

<main>
    <div class="box">
        <h2>Reset Password</h2>

        <?php if (!$valid): ?>
            <div class="msg error"><?= htmlspecialchars($msg) ?></div>
            <p>Kalau link sudah kadaluarsa, silakan kembali ke halaman 
                <a href="forgot.php">Lupa Password</a> dan minta link baru.</p>

        <?php else: ?>
            <div class="msg info">Silakan masukkan password baru untuk akun <b><?= htmlspecialchars($email) ?></b>.</div>

            <form method="post" action="reset_submit.php">
                <input type="hidden" name="email" value="<?= htmlspecialchars($email) ?>">
                <input type="hidden" name="token" value="<?= htmlspecialchars($token) ?>">

                <label>Password baru</label>
                <input type="password" name="password" required minlength="6" placeholder="Minimal 6 karakter">

                <label>Ulangi password</label>
                <input type="password" name="password2" required minlength="6" placeholder="Ulangi password">

                <button type="submit">Simpan Password Baru</button>
            </form>
        <?php endif; ?>

    </div>
</main>

<footer>
    <div class="footer-bottom">
        <p>© <?= date('Y') ?>, Pustakaia | Website developed by Grup 3</p>
        <div class="footer-links">
            <a href="#">Terms of Use</a>
            <a href="#">Privacy Policy</a>
        </div>
    </div>
</footer>

</body>
</html>
