<?php
session_start();
require_once 'config.php';

// Cek login
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
    header('Location: login.php');
    exit;
}

// Ambil daftar admin
$admins = $db->query("SELECT id, username FROM admin_users")->fetchAll(PDO::FETCH_ASSOC);

// Daftar menu yang bisa diatur aksesnya
$menus = [
    'dashboard' => 'Dashboard',
    'result' => 'Data Result',
    'edit_result_time' => 'Edit Result Time',
    'pasaran' => 'Pasaran',
    'markets' => 'Kelola Pasaran',
    'logout' => 'Logout'
];

// Handle update akses
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['admin_id'])) {
    $admin_id = $_POST['admin_id'];
    $selected = isset($_POST['permissions']) ? $_POST['permissions'] : [];

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

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

    $success = "Akses berhasil diperbarui.";
}

// Jika memilih admin, ambil hak aksesnya
$selected_admin_id = $_GET['admin_id'] ?? '';
$admin_permissions = [];

if ($selected_admin_id) {
    $stmt = $db->prepare("SELECT menu_key FROM admin_permissions WHERE admin_id = ?");
    $stmt->execute([$selected_admin_id]);
    $admin_permissions = $stmt->fetchAll(PDO::FETCH_COLUMN);
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Kelola Akses Admin</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">
</head>
<body class="p-4">
<div class="container">
    <h3>Kelola Akses Admin</h3>
    <?php if (isset($success)): ?>
        <div class="alert alert-success"><?= $success ?></div>
    <?php endif; ?>

    <form method="get" class="form-inline mb-4">
        <label class="mr-2">Pilih Admin:</label>
        <select name="admin_id" class="form-control mr-2" onchange="this.form.submit()">
            <option value="">-- Pilih --</option>
            <?php foreach ($admins as $admin): ?>
                <option value="<?= $admin['id'] ?>" <?= $selected_admin_id == $admin['id'] ? 'selected' : '' ?>>
                    <?= htmlspecialchars($admin['username']) ?>
                </option>
            <?php endforeach; ?>
        </select>
    </form>

    <?php if ($selected_admin_id): ?>
    <form method="post">
        <input type="hidden" name="admin_id" value="<?= $selected_admin_id ?>">
        <div class="form-group">
            <label><strong>Pilih Menu yang Bisa Diakses:</strong></label><br>
            <?php foreach ($menus as $key => $label): ?>
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="permissions[]" value="<?= $key ?>"
                        <?= in_array($key, $admin_permissions) ? 'checked' : '' ?>>
                    <label class="form-check-label"><?= $label ?></label>
                </div>
            <?php endforeach; ?>
        </div>
        <button type="submit" class="btn btn-primary">Simpan Akses</button>
    </form>
    <?php endif; ?>
</div>
</body>
</html>
