<?php
/**
 * modules/locations/delete_locations.php
 * Handles Soft Delete (Deactivate) 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=locations&page=list_locations') . "&status=access_denied");
    exit;
}

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

if ($target_location_id > 0) {
    $new_status = ($action === 'deactivate') ? 'INACTIVE' : 'ACTIVE';
    
    $sql = "UPDATE TBL_LOCATIONS 
            SET STATUS = '$new_status', 
                UPDATED_BY = $current_user, 
                UPDATED_DATE = CURRENT_TIMESTAMP 
            WHERE LOCATION_ID = $target_location_id";
            
    $db->query($sql);
}

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