<?php
header('Content-Type: application/json; charset=utf-8');
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
require __DIR__ . '/../includes/session_control.php';
require_once __DIR__ . '/../cotizaciones/api/_node.php';

$idDet = isset($_POST['id']) ? (int)$_POST['id'] : 0;
if (!$idDet) {
    http_response_code(400);
    echo json_encode(['success' => false, 'error' => 'Parámetros inválidos']);
    exit;
}

$token = cpn_node_token_or_fail();
$base = cpn_node_api_base();
$url = $base . '/api/ventas/detalle/' . urlencode((string)$idDet);
$r = cpn_node_request('DELETE', $url, $token);

if (!isset($r['ok']) || $r['ok'] !== true) {
    http_response_code($r['_http'] ?? 500);
    $msg = $r['error']['message'] ?? 'Error eliminando ítem.';
    echo json_encode(['success' => false, 'error' => $msg]);
    exit;
}

echo json_encode(['success' => true]);
