<?php
session_start();
if (!isset($_SESSION['username']) || $_SESSION['role'] !== 'admin') {
    header('Location: /public/index.php');
    exit;
}

require_once '../includes/db.php';

// Check if the user is being deleted
$user_id = $_GET['delete_user'] ?? null;
if (!$user_id) {
    die("Invalid user ID.");
}

// Delete the user
try {
    $stmt = $pdo->prepare("DELETE FROM users WHERE id = ?");
    $stmt->execute([$user_id]);
    header("Location: admin_user_management.php"); // Redirect to user management page
    exit;
} catch (PDOException $e) {
    $error_message = "Error deleting user: " . $e->getMessage();
}
?>

<h2>Delete User</h2>

<?php if (isset($error_message)): ?>
    <div class="error"><?= $error_message ?></div>
<?php endif; ?>

<p>Are you sure you want to delete this user? This action cannot be undone.</p>

<a href="admin_user_management.php" class="button">Back to User Management</a>

<?php include '../includes/footer.php'; ?>
