<?php
// ventas_resumen.php
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}
require_once __DIR__ . '/../includes/session_control.php';
require_once __DIR__ . '/../cotizaciones/api/_node.php';

$idVenta = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if (!$idVenta) die('ID de venta inválido');

$token = cpn_node_token_or_fail();
$base = cpn_node_api_base();
$url = $base . '/api/ventas/ordenes/' . urlencode((string)$idVenta) . '/resumen';
$r = cpn_node_request('GET', $url, $token);

if (!isset($r['ok']) || $r['ok'] !== true) {
    http_response_code($r['_http'] ?? 500);
    die('Error consultando resumen en Node');
}

$resumen = $r['data'] ?? null;
if (!is_array($resumen)) {
    http_response_code(500);
    die('Resumen inválido');
}
?>




<div id="ventasresumen">
    <div class="card mb-2">
        <div class="card-header small py-2">Resumen</div>
        <div class="card-body p-2 small" style="font-size: 0.85rem;">

            <!-- RESUMEN PRINCIPAL -->
            <div class="section">
                <dl class="row mb-0">
                    <dt class="col-6">Subtotal</dt>
                    <dd class="col-6 text-end">
                        $ <?= number_format($resumen['subtotal'], 0, ',', '.') ?>
                    </dd>
                    <dt class="col-6">Impuestos</dt>
                    <dd class="col-6 text-end">
                        $ <?= number_format($resumen['impuesto'], 0, ',', '.') ?>
                    </dd>
                    <dt class="col-6">Envío</dt>
                    <dd class="col-6 text-end">
                        $ <?= number_format($resumen['envio'], 0, ',', '.') ?>
                    </dd>
                    <dt class="col-6">Total</dt>
                    <dd class="col-6 text-end fw-bold">
                        $ <?= number_format($resumen['total'], 0, ',', '.') ?>
                    </dd>
                </dl>
            </div>

            <!-- PAGOS -->
            <div class="section">
                <div class="toggle-header" data-bs-toggle="collapse" data-bs-target="#collapsePagos">
                    <span>Pagos <i class="bi bi-chevron-down"></i></span>
                    <span>$ <?= number_format($resumen['pagos'], 0, ',', '.') ?></span>
                </div>
                <div id="collapsePagos" class="collapse">
                    <button id="addPagoBtn" class="btn btn-sm btn-primary mb-2">+</button>
                    <table id="tablaPagos" class="table table-sm table-bordered">
                        <thead>
                            <tr>
                                <th>Fecha</th>
                                <th>Tipo</th>
                                <th>Medio</th>
                                <th>Ref</th>
                                <th class="text-end">Valor</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>

            <!-- DEDUCCIONES -->
            <div class="section">
                <div class="toggle-header" data-bs-toggle="collapse" data-bs-target="#collapseDeducciones">
                    <span>Deducciones <i class="bi bi-chevron-down"></i></span>
                    <span>$ <?= number_format($resumen['deducciones'], 0, ',', '.') ?></span>
                </div>
                <div id="collapseDeducciones" class="collapse">
                    <button id="addDedBtn" class="btn btn-sm btn-primary mb-2">+</button>
                    <table id="tablaDeducciones" class="table table-sm table-bordered">
                        <thead>
                            <tr>
                                <th>Fecha</th>
                                <th>Concepto</th>
                                <th class="text-end">% </th>
                                <th class="text-end">Valor</th>
                                <th>Ref</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>

            <!-- SALDO -->
            <div class="section d-flex justify-content-between">
                <strong>Saldo</strong>
                <span class="<?= ($resumen['saldo'] != 0 ? 'text-danger fw-bold' : '') ?>">
                    $ <?= number_format($resumen['saldo'], 0, ',', '.') ?>
                </span>
            </div>
        </div>
    </div>
</div>