<?php
require_once 'db_connect.php';

header('Content-Type: application/json');

if (!isset($_GET['category_id']) || !is_numeric($_GET['category_id'])) {
    echo json_encode(['status' => 'error', 'message' => 'معرف التصنيف غير صالح']);
    exit();
}

$category_id = intval($_GET['category_id']);

try {
    $stmt = $conn->prepare("SELECT id, name FROM items WHERE category_id = :category_id");
    $stmt->execute([':category_id' => $category_id]);
    $items = $stmt->fetchAll(PDO::FETCH_ASSOC);

    if ($items) {
        echo json_encode(['status' => 'success', 'items' => $items]);
    } else {
        echo json_encode(['status' => 'success', 'items' => []]);
    }
} catch (PDOException $e) {
    error_log("Error fetching items: " . $e->getMessage());
    echo json_encode(['status' => 'error', 'message' => 'حدث خطأ أثناء جلب الأصناف']);
}
?>