<?php
/**
 * modules/categories/delete_categories.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=categories&page=list_categories') . "&status=access_denied");
    exit;
}

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

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

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