<?php
session_start();
require_once(__DIR__ . "/../config/db.php");

if (!isset($_SESSION['csrf_token'])) {
    $_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}

$csrf_token = $_SESSION['csrf_token'];

if (!isset($_SESSION['admin_id'])) {
    header("Location: login");
    exit();
}

$admin_id = $_SESSION['admin_id'];

$getAdmin = "SELECT id, fullname, username, level, status 
             FROM admin 
             WHERE id = :admin_id";
$stmt = $db->prepare($getAdmin);
$stmt->bindParam(':admin_id', $admin_id, PDO::PARAM_INT);
$stmt->execute();
$admin = $stmt->fetch(PDO::FETCH_ASSOC);

if ($admin['level'] === 'developer') {
    $getAdminByLevel = "SELECT id, fullname, username, level, status 
                     FROM admin 
                     WHERE level IN ('admin', 'staff') 
                     ORDER BY id ASC";
} elseif ($admin['level'] === 'admin') {
    $getAdminByLevel = "SELECT id, fullname, username, level, status 
                     FROM admin 
                     WHERE level = 'staff' 
                     ORDER BY id ASC";
} else {
    $getAdminByLevel = "SELECT id, fullname, username, level, status 
                     FROM admin 
                     WHERE 1 = 0";
}

$stmt = $db->prepare($getAdminByLevel);
$stmt->execute();
$admins = $stmt->fetchAll(PDO::FETCH_ASSOC);
