<?php
require_once '../config.php';

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

try {
    $category_id = isset($_GET['category_id']) ? (int)$_GET['category_id'] : 0;
    
    if ($category_id > 0) {
        $stmt = $pdo->prepare("
            SELECT e.*, c.name as category_name 
            FROM equipment e 
            JOIN categories c ON e.category_id = c.id 
            WHERE e.category_id = ? AND e.stock_quantity > 0
            ORDER BY e.name
        ");
        $stmt->execute([$category_id]);
    } else {
        $stmt = $pdo->query("
            SELECT e.*, c.name as category_name 
            FROM equipment e 
            JOIN categories c ON e.category_id = c.id 
            WHERE e.stock_quantity > 0
            ORDER BY e.id DESC 
            LIMIT 12
        ");
    }
    
    $equipment = $stmt->fetchAll();
    echo json_encode($equipment);
    
} catch(PDOException $e) {
    echo json_encode(['error' => $e->getMessage()]);
}
?>