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

// Check if user is logged in and is admin
if (!isset($_SESSION['user_id']) || $_SESSION['user']['role'] !== 'admin') {
    header('Location: dashboard.php');
    exit();
}

// Handle user deletion
if (isset($_GET['id'])) {
    $user_id = $_GET['id'];
    
    // Don't allow deleting yourself
    if ($user_id == $_SESSION['user_id']) {
        header('Location: all_users.php?error=cannot_delete_self');
        exit();
    }
    
    // Delete user
    $delete_sql = "DELETE FROM users WHERE id = $user_id";
    if ($con->query($delete_sql)) {
        header('Location: all_users.php?success=user_deleted');
    } else {
        header('Location: all_users.php?error=delete_failed');
    }
    exit();
}
?>
