<?php
/**
 * modules/contractors/delete_contractors.php
 */

$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=contractors&page=list_contractors') . "&status=access_denied");
    exit;
}

$target_id    = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$action       = isset($_GET['action']) ? $_GET['action'] : '';
$current_user = (int)$_SESSION['user_id'];

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

header("Location: index.php?s=" . encrypt_url('mod=contractors&page=list_contractors') . "&status=updated");
exit;
?>