<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/config.php';

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($id <= 0) {
    header('Location: list.php?message=not_found');
    exit;
}

$stmt = $conn->prepare("SELECT status FROM categories WHERE id = ? LIMIT 1");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$category = $result->fetch_assoc();

if (!$category) {
    header('Location: list.php?message=not_found');
    exit;
}

$new_status = ((int)$category['status'] === 1) ? 0 : 1;

$update_stmt = $conn->prepare("UPDATE categories SET status = ? WHERE id = ?");
$update_stmt->bind_param("ii", $new_status, $id);
$update_stmt->execute();

header('Location: list.php?message=status_updated');
exit;