<?php



require_once __DIR__ . '/../../config.php';

// GET /admin/stock/list.php
// Query params: centro, producto

requireAdmin();

$db = getDB();

$centro = $_GET['centro'] ?? '';
$producto = $_GET['producto'] ?? '';

$where = [];
$params = [];

if (!empty($centro)) {
    $where[] = 'i.centros_idcd = ?';
    $params[] = $centro;
}

if (!empty($producto)) {
    $where[] = 'i.productos_id_prod = ?';
    $params[] = $producto;
}

$whereClause = !empty($where) ? 'WHERE ' . implode(' AND ', $where) : '';

$stmt = $db->prepare("
    SELECT i.idunic, i.id_inv, i.productos_id_prod, i.cants, i.cantf, i.ent_stock, i.sal_stock,
           p.nombre as producto_nombre, p.precio,
           c.nombre as centro_nombre
    FROM inventario i
    LEFT JOIN productos p ON i.productos_id_prod = p.id_prod
    LEFT JOIN inversionista inv ON i.id_inv = inv.id_inver
    LEFT JOIN centros c ON inv.centros_idcd = c.idcd
    $whereClause
    ORDER BY i.productos_id_prod DESC
");

$stmt->execute($params);
$stock = $stmt->fetchAll();

jsonOk(['stock' => $stock]);
