<?php
require_once __DIR__ . '/../kkn-system/includes/config.php';
require_once __DIR__ . '/../kkn-system/includes/db.php';
require_once __DIR__ . '/../kkn-system/includes/auth.php';

if (isLoggedIn()) {
    header('Location: ' . BASE_URL . '/kkn-dashboard');
    exit;
}

$step   = $_GET['step'] ?? 'input';
$error  = '';
$success= '';

// Step 1: Verifikasi NIM
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['cari_nim'])) {
    $nim = trim($_POST['nim'] ?? '');
    if (!$nim) {
        $error = 'NIM wajib diisi.';
    } else {
        $mhs = fetch("SELECT m.*, u.id as user_id FROM kkn_mahasiswa m JOIN kkn_users u ON u.id=m.user_id WHERE m.nim=? AND u.role='mahasiswa'", [$nim]);
        if (!$mhs) {
            $error = 'NIM tidak ditemukan dalam sistem.';
        } else {
            // Reset password = NIM
            $hashed = password_hash($nim, PASSWORD_BCRYPT);
            query("UPDATE kkn_users SET password=? WHERE id=?", [$hashed, $mhs['user_id']]);
            $success = true;
            $nama    = $mhs['nama'];
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reset Password — Sistem KKN UMUS 2026</title>
<link rel="icon" type="image/png" href="<?= BASE_URL ?>/kkn-system/assets/logo-smart-kkn.png">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Playfair+Display:wght@700&display=swap" rel="stylesheet">
<style>
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
body{font-family:'Inter',sans-serif;min-height:100vh;background:linear-gradient(135deg,#0D2E5C 0%,#1A56A4 100%);display:flex;align-items:center;justify-content:center;padding:1rem;}
.card{background:#fff;border-radius:16px;padding:2.5rem;width:100%;max-width:420px;box-shadow:0 20px 60px rgba(0,0,0,.25);}
.logo{text-align:center;margin-bottom:1.5rem;}
.logo img{width:80px;height:80px;object-fit:contain;border-radius:12px;}
.logo h1{font-family:'Playfair Display',serif;font-size:1.2rem;color:#0D2E5C;margin-top:.5rem;}
.logo p{font-size:.75rem;color:#8A96A8;margin-top:2px;}
.form-group{margin-bottom:1.25rem;}
.form-group label{display:block;font-size:.82rem;font-weight:600;color:#2C3545;margin-bottom:.4rem;}
.form-group input{width:100%;padding:11px 14px;border:1.5px solid #E2E8F0;border-radius:8px;font-size:.9rem;font-family:'Inter',sans-serif;outline:none;transition:border-color .2s;}
.form-group input:focus{border-color:#1A56A4;}
.btn{width:100%;padding:13px;background:#0D2E5C;color:#fff;border:none;border-radius:8px;font-size:.95rem;font-weight:700;cursor:pointer;font-family:'Inter',sans-serif;transition:background .2s;}
.btn:hover{background:#1A56A4;}
.error{background:#FFF5F5;border:1px solid #FED7D7;color:#C53030;padding:10px 14px;border-radius:8px;font-size:.83rem;margin-bottom:1rem;}
.success-box{background:#F0FFF4;border:1px solid #C6F6D5;border-radius:12px;padding:1.5rem;text-align:center;}
.success-box .icon{font-size:2.5rem;margin-bottom:.75rem;}
.success-box h3{color:#276749;font-size:1rem;font-weight:700;margin-bottom:.5rem;}
.success-box p{font-size:.82rem;color:#4A5568;line-height:1.6;}
.success-box .nim-badge{display:inline-block;background:#0D2E5C;color:#fff;padding:6px 16px;border-radius:20px;font-size:.85rem;font-weight:700;margin:.75rem 0;font-family:'Playfair Display',serif;}
.divider{border:none;border-top:1px solid #F0F4F8;margin:1.25rem 0;}
.links{text-align:center;font-size:.82rem;color:#8A96A8;}
.links a{color:#1A56A4;font-weight:600;text-decoration:none;}
.info-box{background:#EEF4FF;border:1px solid #C3D5F5;border-radius:8px;padding:.875rem 1rem;font-size:.8rem;color:#1e40af;margin-bottom:1.25rem;line-height:1.6;}
</style>
</head>
<body>
<div class="card">
  <div class="logo">
    <img src="<?= BASE_URL ?>/kkn-system/assets/logo-smart-kkn.png" alt="SMART-KKN">
    <h1>Reset Password</h1>
    <p>Sistem KKN UMUS 2026</p>
  </div>

  <?php if ($success): ?>
  <div class="success-box">
    <div class="icon">✅</div>
    <h3>Password Berhasil Direset!</h3>
    <p>Halo <strong><?= htmlspecialchars($nama) ?></strong>, password Anda telah direset menjadi NIM Anda:</p>
    <div class="nim-badge"><?= htmlspecialchars($nim) ?></div>
    <p>Silakan login dengan NIM sebagai password, lalu segera ganti password baru melalui menu <strong>Profil Saya</strong>.</p>
  </div>
  <hr class="divider">
  <div class="links"><a href="<?= BASE_URL ?>/kkn-login">← Kembali ke Login</a></div>

  <?php else: ?>
  <div class="info-box">
    <strong>ℹ️ Cara reset password:</strong><br>
    Masukkan NIM Anda. Password akan direset menjadi NIM Anda sendiri. Setelah login, segera ganti password baru melalui menu <strong>Profil Saya</strong>.
  </div>

  <?php if ($error): ?>
  <div class="error">⚠ <?= htmlspecialchars($error) ?></div>
  <?php endif; ?>

  <form method="POST">
    <div class="form-group">
      <label>NIM Mahasiswa</label>
      <input type="text" name="nim" placeholder="Masukkan NIM Anda" value="<?= htmlspecialchars($_POST['nim'] ?? '') ?>" required autofocus>
    </div>
    <button type="submit" name="cari_nim" class="btn">Reset Password</button>
  </form>
  <hr class="divider">
  <div class="links"><a href="<?= BASE_URL ?>/kkn-login">← Kembali ke Login</a></div>
  <?php endif; ?>
</div>
</body>
</html>
