<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<title>Setup Password — WebGIS Pontianak</title>
<style>
body{font-family:monospace;background:#0f172a;color:#e2e8f0;padding:40px;max-width:600px;margin:0 auto}
h2{color:#3b82f6;margin-bottom:20px}
.box{background:#1e293b;border:1px solid #334155;border-radius:10px;padding:20px;margin-bottom:16px}
.ok{color:#10b981}.err{color:#ef4444}.warn{color:#f59e0b}
table{width:100%;border-collapse:collapse;margin-top:12px}
td,th{padding:8px 12px;border:1px solid #334155;font-size:13px}
th{background:#0f172a;color:#7c93b5}
.btn{display:inline-block;margin-top:20px;padding:10px 24px;background:#3b82f6;color:#fff;border:none;border-radius:8px;font-size:14px;font-weight:700;cursor:pointer;font-family:monospace;text-decoration:none}
.btn:hover{background:#2563eb}
.del{color:#ef4444;font-size:12px;margin-top:16px;padding:10px;background:#450a0a;border:1px solid #7f1d1d;border-radius:8px}
</style>
</head>
<body>
<?php
require_once 'includes/config.php';

$action = $_GET['action'] ?? '';

if ($action === 'setup') {
    // Hash password "password123" yang benar
    $hash = password_hash('password123', PASSWORD_DEFAULT);
    $hash_e = $conn->real_escape_string($hash);

    // Update semua user dengan hash yang valid
    if ($conn->query("UPDATE users SET password_hash='$hash_e'")) {
        echo '<div class="box"><span class="ok">✅ Semua password berhasil di-set ke: <strong>password123</strong></span></div>';
    } else {
        echo '<div class="box"><span class="err">❌ Gagal: '.$conn->error.'</span></div>';
    }
}
?>

<h2>🔧 Setup Password WebGIS Pontianak</h2>

<?php
// Cek koneksi
if ($conn->connect_error) {
    echo '<div class="box"><span class="err">❌ Koneksi DB gagal: '.$conn->connect_error.'</span></div>';
    exit;
}
echo '<div class="box"><span class="ok">✅ Koneksi database berhasil</span></div>';

// Tampilkan semua user
$res = $conn->query("SELECT id, nama_lengkap, username, role, is_active FROM users ORDER BY id");
?>

<div class="box">
  <b>Daftar Akun yang Tersedia:</b>
  <table>
    <tr><th>#</th><th>Nama</th><th>Username</th><th>Role</th><th>Aktif</th></tr>
    <?php while($r = $res->fetch_assoc()): ?>
    <tr>
      <td><?= $r['id'] ?></td>
      <td><?= htmlspecialchars($r['nama_lengkap']) ?></td>
      <td><strong><?= htmlspecialchars($r['username']) ?></strong></td>
      <td><?= $r['role'] ?></td>
      <td><?= $r['is_active'] ? '<span class="ok">✅</span>' : '<span class="err">❌</span>' ?></td>
    </tr>
    <?php endwhile; ?>
  </table>
</div>

<div class="box">
  <b>Klik tombol di bawah untuk set semua password menjadi <span class="warn">password123</span>:</b><br><br>
  <a class="btn" href="?action=setup">🔐 Set Semua Password → password123</a>
</div>

<div class="del">
  ⚠️ <strong>PENTING:</strong> Setelah setup selesai, hapus file <code>setup_password.php</code> ini dari server Anda demi keamanan!
</div>

<?php if ($action === 'setup'): ?>
<div class="box" style="margin-top:20px">
  <b>✅ Sekarang bisa login dengan:</b>
  <table>
    <tr><th>Username</th><th>Password</th><th>Role</th></tr>
    <tr><td>admin</td><td>password123</td><td>Admin</td></tr>
    <tr><td>pengurus_mj</td><td>password123</td><td>Pengurus Ibadah</td></tr>
    <tr><td>pengurus_mm</td><td>password123</td><td>Pengurus Ibadah</td></tr>
    <tr><td>pengurus_gk</td><td>password123</td><td>Pengurus Ibadah</td></tr>
    <tr><td>pemda_ptk</td><td>password123</td><td>Pemerintah</td></tr>
    <tr><td>ahmad_yusuf</td><td>password123</td><td>Masyarakat</td></tr>
  </table>
  <br>
  <a class="btn" href="login.php" style="background:#10b981">🚀 Pergi ke Halaman Login</a>
</div>
<?php endif; ?>

</body>
</html>
