<?php
header('Content-Type: application/json');
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
require_once __DIR__ . '/../includes/session_control.php';
require_once __DIR__ . '/../cotizaciones/api/_node.php';

$token = cpn_node_token_or_fail();
$base = cpn_node_api_base();

$id    = isset($_POST['id']) ? intval($_POST['id']) : 0;
$field = $_POST['field'] ?? '';
$value = $_POST['value'] ?? '';

$r = cpn_node_request('PATCH', $base . '/api/ordenes/pedidos/detalle/' . urlencode((string)$id), $token, [
    'field' => (string)$field,
    'value' => $value,
]);
if (!isset($r['ok']) || $r['ok'] !== true) {
    $msg = $r['error']['message'] ?? 'Error actualizando detalle.';
    echo json_encode(['success' => false, 'error' => $msg]);
    exit;
}

echo json_encode(['success' => true]);
