<?php
require_once('includes/load.php');

if(isset($_GET['part_number'])) {
    $part_number = $db->escape($_GET['part_number']);
    
    // Get product info including category
    $product = find_by_part_number('products', $part_number);
    
    if($product) {
        $category = find_by_id('categories', $product['categorie_id']);
        echo json_encode([
            'success' => true,
            'categorie_id' => $product['categorie_id'],
            'category_name' => $category['name'],
            'product_name' => $product['name']
        ]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Part number not found']);
    }
} elseif(isset($_GET['id'])) {
    $category = find_by_id('categories', (int)$_GET['id']);
    if($category) {
        echo json_encode(['success' => true, 'category_name' => $category['name']]);
    } else {
        echo json_encode(['success' => false, 'message' => 'Category not found']);
    }
}
?>