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

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

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

if ($id <= 0) {
    redirect_to(admin_url('product_types/list.php'));
}

$data = product_types_get_one($conn, $id);

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

if (product_types_has_links($conn, $id)) {
    redirect_to(admin_url('product_types/list.php?delete_error=linked'));
}

$stmt = $conn->prepare("DELETE FROM product_types WHERE id = ?");
$stmt->bind_param("i", $id);

if ($stmt->execute()) {
    admin_log_activity('product_type.delete', 'حذف نوع منتج', 'تم حذف النوع/التصنيف الفرعي.', 'product_type', $id, [], true);
    redirect_to(admin_url('product_types/list.php?deleted=1'));
} else {
    redirect_to(admin_url('product_types/list.php?delete_error=failed'));
}