<?php $this->assign('title', 'Inicio'); ?>

<h3 class="mb-4"><i class="fas fa-home"></i> Bienvenido<?= !empty($clienteAutenticado) ? ', ' . h($clienteAutenticado->nombre_razon_social) : '' ?></h3>

<div class="row mb-4">
    <div class="col-md-4">
        <div class="card text-white" style="background: linear-gradient(135deg, #1d4ed8 0%, #1e3a8a 100%);">
            <div class="card-body">
                <h6 class="card-title mb-1"><i class="fas fa-file-invoice me-1"></i> Total de Comprobantes</h6>
                <h3 class="mb-0"><?= (int) $totalFacturas ?></h3>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card text-white" style="background: linear-gradient(135deg, #0ea5a4 0%, #0b8685 100%);">
            <div class="card-body">
                <h6 class="card-title mb-1"><i class="fas fa-calendar-check me-1"></i> Próximas Citas</h6>
                <h3 class="mb-0"><?= count($proximasCitas) ?></h3>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card text-white" style="background: linear-gradient(135deg, #f59e0b 0%, #b45309 100%);">
            <div class="card-body">
                <h6 class="card-title mb-1"><i class="fas fa-sync-alt me-1"></i> Pagos Pendientes</h6>
                <h3 class="mb-0"><?= count($proximosPagos) ?></h3>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-6">
        <h5>Comprobantes Recientes</h5>
        <?php if (!empty($facturasRecientes)): ?>
            <div class="table-responsive">
                <table class="table table-sm table-striped">
                    <thead>
                        <tr>
                            <th>Comprobante</th>
                            <th>Fecha</th>
                            <th>Total</th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($facturasRecientes as $f): ?>
                            <tr>
                                <td><?= h(($f->serie ?? '') . '-' . ($f->correlativo ?? $f->id)) ?></td>
                                <td><?= $f->created ? $f->created->format('d/m/Y') : '' ?></td>
                                <td>S/ <?= number_format((float) $f->total, 2) ?></td>
                                <td>
                                    <?= $this->Html->link('Ver', ['controller' => 'Facturas', 'action' => 'view', $f->id], ['class' => 'btn btn-sm btn-portal-primary']) ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?= $this->Html->link('Ver todos mis comprobantes', ['controller' => 'Facturas', 'action' => 'index'], ['class' => 'btn btn-sm btn-outline-secondary']) ?>
        <?php else: ?>
            <p class="text-muted">Todavía no tienes comprobantes registrados.</p>
        <?php endif; ?>
    </div>

    <div class="col-md-6">
        <h5>Próximas Citas</h5>
        <?php if (!empty($proximasCitas)): ?>
            <div class="table-responsive">
                <table class="table table-sm table-striped">
                    <thead>
                        <tr>
                            <th>Fecha</th>
                            <th>Personal</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($proximasCitas as $cita): ?>
                            <tr>
                                <td><?= h($cita->fecha_hora->format('d-m-Y H:i')) ?></td>
                                <td><?= h(($cita->personal->nombre ?? '') . ' ' . ($cita->personal->apellido ?? '')) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php else: ?>
            <p class="text-muted">No tienes citas programadas.</p>
        <?php endif; ?>
    </div>
</div>

<div class="row mt-4">
    <div class="col-md-12">
        <h5>Próximos Pagos</h5>
        <?php if (!empty($proximosPagos)): ?>
            <div class="table-responsive">
                <table class="table table-sm table-striped">
                    <thead>
                        <tr>
                            <th>Concepto</th>
                            <th>Fecha</th>
                            <th>Monto</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($proximosPagos as $cuota): ?>
                            <tr>
                                <td><?= h(($cuota->paquetes_pago->nombre ?? '') . ($cuota->titulo_referencial ? ' — ' . $cuota->titulo_referencial : '')) ?></td>
                                <td><?= $cuota->fecha_recordatorio?->format('d/m/Y') ?></td>
                                <td>S/ <?= number_format((float) $cuota->monto, 2) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?= $this->Html->link('Ver todos mis pagos', ['controller' => 'Pagos', 'action' => 'index'], ['class' => 'btn btn-sm btn-outline-secondary']) ?>
        <?php else: ?>
            <p class="text-muted">No tienes pagos pendientes.</p>
        <?php endif; ?>
    </div>
</div>
