<?php
// change_confirm.php
require_once __DIR__ . '/../Login/config.php';
if (session_status() === PHP_SESSION_NONE) session_start();
if (!isset($_SESSION['user_id'])) {
    header('Location: ../Login/login.php');
    exit;
}

// ambil email user
$stmt = $pdo->prepare("SELECT email FROM users WHERE id = ? LIMIT 1");
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
$email = $user['email'] ?? '';

$msg = $_SESSION['change_confirm_msg'] ?? '';
unset($_SESSION['change_confirm_msg']);
?>
<!doctype html>
<html lang="id">
<head>
<meta charset="utf-8">
<title>Konfirmasi Kode Ganti Password - Pustakaia</title>
<style>
body{font-family:Arial,Helvetica,sans-serif;background:#f4f6f6;padding:20px}
.box{max-width:520px;margin:30px auto;background:#fff;padding:20px;border-radius:8px;box-shadow:0 6px 18px rgba(0,0,0,0.06)}
label{display:block;margin-bottom:6px}
input{width:100%;padding:10px;border:1px solid #ddd;border-radius:6px;margin-bottom:10px}
button{background:#069797;color:#fff;padding:10px 14px;border:0;border-radius:8px;cursor:pointer}
.note{font-size:13px;color:#666}
.msg{padding:10px;border-radius:6px;margin-bottom:10px}
.msg.info{background:#e8f7f6;color:#066}
.msg.error{background:#ffecec;color:#900}
</style>
</head>
<body>
  <div class="box">
    <h2>Masukkan Kode Konfirmasi</h2>
    <?php if ($msg): ?>
      <div class="msg info"><?= htmlspecialchars($msg) ?></div>
    <?php endif; ?>
    <p class="note">Kami sudah mengirim kode 6 digit ke email: <strong><?= htmlspecialchars($email) ?></strong>. Masukkan kode tersebut lalu isi password baru.</p>
    <form method="post" action="change_confirm_submit.php">
      <label>Kode (6 digit)</label>
      <input type="text" name="token" pattern="\d{6}" maxlength="6" required placeholder="123456">
      <label>Password baru</label>
      <input type="password" name="password" minlength="6" required placeholder="Minimal 6 karakter">
      <label>Ulangi password</label>
      <input type="password" name="password2" minlength="6" required placeholder="Ulangi password">
      <div style="margin-top:10px">
        <button type="submit">Ganti Password</button>
      </div>
    </form>
  </div>
</body>
</html>
