<?php $this->assign('title', 'Detalle de Factura'); ?>

<h3 class="mb-4"><i class="fas fa-file-invoice"></i> Comprobante <?= h(($factura->serie ?? '') . '-' . ($factura->correlativo ?? $factura->id)) ?></h3>

<div class="row mb-4">
    <div class="col-md-4">
        <p class="mb-1"><strong>Tipo:</strong></p>
        <p><?= $factura->tipo_doc === '01' ? 'Factura' : ($factura->tipo_doc === '03' ? 'Boleta' : 'Recibo Interno') ?></p>
    </div>
    <div class="col-md-4">
        <p class="mb-1"><strong>Fecha:</strong></p>
        <p><?= $factura->created ? $factura->created->format('d/m/Y') : '' ?></p>
    </div>
    <div class="col-md-4">
        <p class="mb-1"><strong>Estado:</strong></p>
        <p><span class="badge bg-secondary"><?= h($factura->estado) ?></span></p>
    </div>
</div>

<div class="table-responsive mb-4">
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Descripción</th>
                <th>Cantidad</th>
                <th>Precio Unit.</th>
                <th>Total</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($factura->invoice_items as $item): ?>
                <tr>
                    <td><?= h($item->descripcion) ?></td>
                    <td><?= h($item->cantidad) ?></td>
                    <td>S/ <?= number_format((float) $item->precio_unitario, 2) ?></td>
                    <td>S/ <?= number_format((float) $item->total, 2) ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr>
                <th colspan="3" class="text-end">Total</th>
                <th>S/ <?= number_format((float) $factura->total, 2) ?></th>
            </tr>
        </tfoot>
    </table>
</div>

<div class="d-flex gap-2">
    <?= $this->Html->link('<i class="fas fa-file-pdf"></i> Descargar PDF', ['action' => 'pdf', $factura->id], ['class' => 'btn btn-portal-primary', 'escape' => false, 'target' => '_blank']) ?>
    <?= $this->Html->link('Volver', ['action' => 'index'], ['class' => 'btn btn-secondary']) ?>
</div>
