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

admin_require_post_action('product_type_toggle', admin_url('product_types/list.php'));

$id = isset($_POST['id']) ? (int)$_POST['id'] : 0;
$data = product_types_get_one($conn, $id);

if (!$data) {
    redirect_to(admin_url('product_types/list.php'));
}

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

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

admin_log_activity('product_type.toggle_status', 'تغيير حالة نوع منتج', 'تم تغيير حالة النوع/التصنيف الفرعي.', 'product_type', $id, ['status' => $new_status], true);
redirect_to(admin_url('product_types/list.php?toggled=1'));