<?php
$menuList = [
  'menu_dashboard' => 'Dashboard',
  'menu_result' => 'Data Result',
  'menu_edit_time' => 'Edit Result Time',
  'menu_markets' => 'Kelola Pasaran',
  'menu_admin' => 'Kelola Admin',
];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $admin_id = $_GET['id'];
    $access = $_POST['access'] ?? [];

    // Hapus semua akses lama
    $db->prepare("DELETE FROM admin_permissions WHERE admin_id = ?")->execute([$admin_id]);

    // Tambahkan akses baru
    $stmt = $db->prepare("INSERT INTO admin_permissions (admin_id, menu_key) VALUES (?, ?)");
    foreach ($access as $menu_key) {
        $stmt->execute([$admin_id, $menu_key]);
    }

    echo "<script>alert('Akses berhasil diperbarui'); window.location='manage_admin.php';</script>";
}

// Ambil akses admin saat ini
$stmt = $db->prepare("SELECT menu_key FROM admin_permissions WHERE admin_id = ?");
$stmt->execute([$_GET['id']]);
$currentAccess = $stmt->fetchAll(PDO::FETCH_COLUMN);
?>

<form method="post">
  <?php foreach ($menuList as $key => $label): ?>
    <div class="form-check">
      <input class="form-check-input" type="checkbox" name="access[]" value="<?= $key ?>" <?= in_array($key, $currentAccess) ? 'checked' : '' ?>>
      <label class="form-check-label"><?= $label ?></label>
    </div>
  <?php endforeach; ?>
  <button type="submit" class="btn btn-primary mt-3">Simpan Akses</button>
</form>
