<?php



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

// POST /admin/stock/update.php
// Body: { id, field, value }

requireAdmin();

$data = body();

if (empty($data['id']) || empty($data['field']) || !isset($data['value'])) {
    jsonErr(400, 'ID, field y value requeridos.');
}

$allowedFields = ['cants', 'cantf', 'ent_stock', 'sal_stock', 'salidas', 'sal_otros'];

if (!in_array($data['field'], $allowedFields)) {
    jsonErr(400, 'Campo no permitido para actualizar.');
}

$db = getDB();

$sql = "UPDATE inventario SET {$data['field']} = ? WHERE idunic = ?";
$stmt = $db->prepare($sql);
$stmt->execute([$data['value'], $data['id']]);

if ($stmt->rowCount() === 0) {
    jsonErr(404, 'Registro de inventario no encontrado.');
}

jsonOk(['message' => 'Stock actualizado.']);
