<?php
ob_start();
require_once '../layouts/session.php';
require_once '../layouts/dbconn.php';
ob_end_clean();
header('Content-Type: application/json');

if (!isset($_SESSION['user_id'])) { echo json_encode(['items' => []]); exit; }

$result = $conn->query("
    SELECT inventory_id, item_name, generic_name, strength, dosage_form,
           current_stock, unit_of_measure, selling_price,
           COALESCE(buying_price, 0) AS buying_price
    FROM pharmacy_inventory
    WHERE is_active = 1
    ORDER BY item_name
");

$items = [];
while ($r = $result->fetch_assoc()) $items[] = $r;
echo json_encode(['items' => $items]);
