<?php
require_once __DIR__ . '/../includes/auth.php';
require_role('admin');

require_once __DIR__ . '/../db/config.php';

if (!isset($_GET['id'])) {
    header("Location: products.php");
    exit;
}

$pdo = db();
$id = $_GET['id'];

// First, get the current status
$stmt = $pdo->prepare("SELECT is_active FROM products WHERE id = ?");
$stmt->execute([$id]);
$current_status = $stmt->fetchColumn();

if ($current_status === false) {
    die("Produkt nie znaleziony.");
}

// Flip the status
$new_status = $current_status ? 0 : 1;

$update_stmt = $pdo->prepare("UPDATE products SET is_active = ? WHERE id = ?");
$update_stmt->execute([$new_status, $id]);

header("Location: products.php");
exit;
