<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION["role"]) || $_SESSION["role"] !== "superadmin") {
   header("Location: superadmin.php?page=applicants&message=$action");
    exit();
}

require_once "../php/db_connect.php";

$id = $_GET["id"] ?? null;
$action = $_GET["action"] ?? null;

if (!$id || !$action) {
    die("Invalid request.");
}

if ($action === "approve") {
    $status = "active";
} elseif ($action === "reject") {
    $status = "rejected";
} else {
    die("Invalid action.");
}

$stmt = $conn->prepare("UPDATE users SET status = ? WHERE id = ? AND role = 'admin'");
$stmt->bind_param("si", $status, $id);

if ($stmt->execute()) {
    header("Location: ../html/superadmin.php?page=applicants");
    exit();
} else {
    echo "Update failed: " . $stmt->error;
}
?>