<div class="container-fluid py-4">
    <!-- HEADER CON BOTONES -->
    <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
        <h3 class="fw-bold mb-0">
            <i class="fas fa-receipt"></i> Gestión de Comprobantes
        </h3>
        
        <div class="d-flex flex-wrap gap-2">
            <a href="<?= $this->Url->build(['controller' => 'DailySummaries', 'action' => 'index']) ?>" class="btn btn-outline-dark">
                <i class="fas fa-calendar-check"></i> Resúmenes
            </a>

            <a href="<?= $this->Url->build([
                'action' => 'anulaciones'
            ]) ?>" class="btn btn-outline-danger">
                <i class="fas fa-ban"></i> Anulaciones
            </a>
            <a href="<?= $this->Url->build(['action' => 'add']) ?>" class="btn btn-info">
                <i class="fas fa-plus-circle"></i> Nuevo Comprobante
            </a>

            <a href="<?= $this->Url->build(['controller' => 'Cajas', 'action' => 'index']) ?>" class="btn btn-outline-primary">
                <i class="fas fa-cash-register"></i> Caja
            </a>

            

            <a href="<?= $this->Url->build(['controller' => 'PagosPersonal', 'action' => 'index']) ?>" class="btn btn-outline-warning">
                <i class="fas fa-money-bill-wave"></i> Pagos
            </a>

            <!-- AHORA (va a la vista de confirmación) -->
<a href="<?= $this->Url->build([
    'action' => 'previsualizarResumen',
    '?' => ['company_id' => 1, 'fecha' => date('Y-m-d')]
]) ?>" class="btn btn-outline-success">
    <i class="fas fa-paper-plane"></i> Resumen Diario
</a>
        </div>
    </div>

        <!-- FILTRO POR TIPO -->
    <div class="mb-3 d-flex gap-2 flex-wrap">
        <a href="<?= $this->Url->build(['action' => 'index']) ?>" 
        class="btn btn-sm <?= !$this->request->getQuery('tipo') ? 'btn-dark' : 'btn-outline-dark' ?>">
            Todos
        </a>
        <a href="<?= $this->Url->build(['action' => 'index', '?' => ['tipo' => 'RI']]) ?>" 
        class="btn btn-sm <?= $this->request->getQuery('tipo') === 'RI' ? 'btn-dark' : 'btn-outline-dark' ?>">
            Recibos Internos
        </a>
        <a href="<?= $this->Url->build(['action' => 'index', '?' => ['tipo' => '03']]) ?>" 
        class="btn btn-sm <?= $this->request->getQuery('tipo') === '03' ? 'btn-warning' : 'btn-outline-warning' ?>">
            Boletas
        </a>
        <a href="<?= $this->Url->build(['action' => 'index', '?' => ['tipo' => '01']]) ?>" 
        class="btn btn-sm <?= $this->request->getQuery('tipo') === '01' ? 'btn-primary' : 'btn-outline-primary' ?>">
            Facturas
        </a>
    </div>

    <!-- TABLA DE COMPROBANTES -->
    <div class="table-responsive card">
        <table class="table table-striped mb-0">

                    <thead class="bg-info text-white">
                        <tr>
                            <th>ID</th>
                            <th>Serie</th>
                            <th>Número</th>
                            <th>Tipo</th>
                            <th>Cliente</th>
                            <th>Origen</th>
                            <th>Total</th>
                            <th>Estado</th>
                            <th class="text-center">Acciones</th>
                        </tr>
                    </thead>

                    <tbody>
                        <?php foreach ($invoices as $inv): ?>
                        <tr>

                            <td>
                                <span class="badge bg-secondary">#<?= $inv->id ?></span>
                            </td>

                            <td><?= h($inv->serie ?: 'RI') ?></td>

                            <td><?= h($inv->correlativo ?: $inv->id) ?></td>

                            <td>
                                <?php
                                if ($inv->tipo_doc === '01') echo 'Factura';
                                elseif ($inv->tipo_doc === '03') echo 'Boleta';
                                else echo 'Recibo Interno';
                                ?>
                            </td>

                            <td><?= h($inv->cliente_nombre) ?></td>

                            <td>
                                <?php if (!empty($inv->presupuestos_invoices)): ?>
                                    <?php foreach ($inv->presupuestos_invoices as $pi): ?>
                                        <?= $this->Html->link(
                                            'Presupuesto #' . $pi->presupuesto_id,
                                            ['controller' => 'Presupuestos', 'action' => 'view', $pi->presupuesto_id],
                                            ['class' => 'badge bg-info text-dark text-decoration-none']
                                        ) ?>
                                    <?php endforeach; ?>
                                <?php else: ?>
                                    <span class="text-muted">—</span>
                                <?php endif; ?>
                            </td>

                            <td>
                                <strong>S/ <?= number_format((float)$inv->total, 2) ?></strong>
                            </td>

                            <td>
                                <?php
                                $color = 'secondary';

                                if ($inv->estado === 'ACEPTADO') $color = 'success';
                                elseif ($inv->estado === 'RECHAZADO') $color = 'danger';
                                elseif ($inv->estado === 'PENDIENTE_RESUMEN') $color = 'warning';
                                elseif ($inv->estado === 'EN_RESUMEN') $color = 'info';
                                elseif ($inv->estado === 'RECIBO_INTERNO') $color = 'dark';
                                ?>
                                <span class="badge bg-<?= $color ?>">
                                    <?= h($inv->estado) ?>
                                </span>
                            </td>

                            <td class="text-center">

                                <a href="<?= $this->Url->build(['action' => 'view', $inv->id]) ?>"
                                   class="btn btn-info btn-sm">
                                   <i class="fas fa-eye"></i>
                                </a>

                               

                                <?php if ($inv->xml_path): ?>
                                    <a href="<?= $this->Url->build('/' . ltrim($inv->xml_path, '/')) ?>" target="_blank"
                                       class="btn btn-secondary btn-sm">
                                       <i class="fas fa-code"></i> XML
                                    </a>
                                <?php endif; ?>

                                <?php if ($inv->cdr_path): ?>
                                    <a href="<?= $this->Url->build('/' . ltrim($inv->cdr_path, '/')) ?>" target="_blank"
                                       class="btn btn-secondary btn-sm">
                                       <i class="fas fa-file"></i> CDR
                                    </a>
                                <?php endif; ?>

                                <a href="<?= $this->Url->build(['action' => 'pdf', $inv->id]) ?>"
                                   target="_blank"
                                   class="btn btn-danger btn-sm">
                                   <i class="fas fa-file-pdf"></i>
                                </a>

                                <?php if ($inv->tipo_doc === 'RI' && $inv->estado === 'RECIBO_INTERNO'): ?>
                                    <a href="<?= $this->Url->build(['action' => 'anularReciboInterno', $inv->id]) ?>"
                                    class="btn btn-danger btn-sm"
                                    onclick="return confirm('¿Anular Recibo Interno #<?= $inv->correlativo ?>?\n\n⚠️ Recuerda registrar el EGRESO en Caja.')">
                                        <i class="fas fa-trash"></i>
                                    </a>
                                <?php endif; ?>


                            </td>

                        </tr>
                        <?php endforeach; ?>
                    </tbody>

                        </table>
                    </div>
    </div>

    <!-- PAGINADOR -->
    <div class="paginator mt-3">
        <ul class="pagination">
            <?= $this->Paginator->first('<< ' . __('Primero')) ?>
            <?= $this->Paginator->prev('< ' . __('Anterior')) ?>
            <?= $this->Paginator->numbers() ?>
            <?= $this->Paginator->next(__('Siguiente') . ' >') ?>
            <?= $this->Paginator->last(__('Último') . ' >>') ?>
        </ul>
        <p><?= $this->Paginator->counter(__('Página {{page}} de {{pages}}, mostrando {{current}} registro(s) de un total de {{count}}')) ?></p>
    </div>
</div>