<?php
/**
 * modules/users/delete_users.php
 * Handles Soft Delete (Suspend) and Restore (Activate) Actions
 */

// 1. STRICT SECURITY KICK-OUT
$p = $GLOBALS['my_privs'] ?? ['can_del' => 0];
if (in_array(1, $_SESSION['all_role_ids'] ?? [])) {
    $p['can_del'] = 1;
}

if ($p['can_del'] == 0) {
    header("Location: index.php?s=" . encrypt_url('mod=users&page=list_users') . "&status=access_denied");
    exit;
}

// 2. PROCESS ACTION
$target_user_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$action         = isset($_GET['action']) ? $_GET['action'] : '';
$current_user   = (int)$_SESSION['user_id'];

// Prevent users from suspending themselves or suspending the absolute Super Admin (ID 1)
if ($target_user_id > 0 && $target_user_id != $current_user && $target_user_id != 1) {
    
    $new_status = ($action === 'suspend') ? 'SUSPENDED' : 'ACTIVE';
    $sql = "UPDATE TBL_USERS 
            SET STATUS = '$new_status', 
                UPDATED_BY = $current_user, 
                UPDATED_DATE = CURRENT_TIMESTAMP 
            WHERE USER_ID = $target_user_id";
            
    $db->query($sql);
}

// 3. REDIRECT BACK TO LIST
header("Location: index.php?s=" . encrypt_url('mod=users&page=list_users') . "&status=updated");
exit;
?>