<div class="content-wrapper">
    <section class="content-header">
        <h1>Historial de Cobros</h1>
    </section>

    <section class="content">

        <!-- ============================================================ -->
        <!-- CABECERA SOLO PARA IMPRESIÓN -->
        <!-- ============================================================ -->
        <div id="cabeceraImpresion" class="print-only">
            <table style="width:100%; border:none; margin-bottom:10px;">
                <tr>
                    <td style="width:90px; vertical-align:top; border:none;">
                        <?php if(!empty($empresa->logo)): ?>
                            <img src="<?php echo base_url('uploads/empresa/'.$empresa->logo); ?>" style="max-width:80px; max-height:80px;">
                        <?php endif; ?>
                    </td>
                    <td style="vertical-align:top; border:none;">
                        <div style="font-size:15px; font-weight:bold;"><?php echo !empty($empresa->nombre) ? $empresa->nombre : ''; ?></div>
                        <div style="font-size:11px; color:#555;">RUC: <?php echo !empty($empresa->ruc) ? $empresa->ruc : ''; ?></div>
                        <div style="font-size:11px; color:#555;"><?php echo !empty($empresa->direccion_fiscal) ? $empresa->direccion_fiscal : ''; ?></div>
                    </td>
                    <td style="text-align:right; vertical-align:top; border:none;">
                        <div style="font-size:16px; font-weight:bold; text-transform:uppercase;">Historial de Cobros</div>
                        <div style="font-size:11px; color:#555;">Emitido: <?php echo $fecha_reporte; ?></div>
                        <div style="font-size:11px; color:#555;">
                            Período: <?php echo date('d/m/Y', strtotime($fecha_desde)); ?> al <?php echo date('d/m/Y', strtotime($fecha_hasta)); ?>
                            <?php if($cliente_id): ?>
                                &nbsp;|&nbsp; Cliente:
                                <?php
                                    foreach($clientes as $c) {
                                        if ($c->id == $cliente_id) { echo $c->razon_social; break; }
                                    }
                                ?>
                            <?php endif; ?>
                            <?php if($medio_pago_id): ?>
                                &nbsp;|&nbsp; Medio de pago:
                                <?php
                                    foreach($medios_pago as $m) {
                                        if ($m->id == $medio_pago_id) { echo $m->nombre; break; }
                                    }
                                ?>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
            </table>
            <hr style="margin:6px 0 12px; border-top:2px solid #333;">
        </div>

        <!-- FILTROS -->
        <div class="row no-print" style="margin-bottom:5px !important; height: 45px;">
            <div class="col-md-12">
                <div class="panel panel-primary" id="panelFiltros">
                    <div class="panel-heading"><strong><i class="fa fa-filter"></i> Filtros</strong></div>
                    <div class="panel-body">
                        <form method="get" action="<?php echo base_url('index.php/cobranzas/historial_cobros'); ?>">
                            <div class="row">
                                <div class="col-md-2 form-group">
                                    <label>Fecha desde</label>
                                    <input type="date" name="fecha_desde" class="form-control" value="<?php echo $fecha_desde; ?>">
                                </div>
                                <div class="col-md-2 form-group">
                                    <label>Fecha hasta</label>
                                    <input type="date" name="fecha_hasta" class="form-control" value="<?php echo $fecha_hasta; ?>">
                                </div>
                                <div class="col-md-2 form-group">
                                    <label>Cliente</label>
                                    <select name="cliente_id" class="form-control">
                                        <option value="">Todos</option>
                                        <?php foreach($clientes as $item): ?>
                                            <option value="<?php echo $item->id; ?>" <?php echo ($cliente_id == $item->id) ? 'selected' : ''; ?>>
                                                <?php echo $item->razon_social; ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="col-md-2 form-group">
                                    <label>Medio de pago</label>
                                    <select name="medio_pago_id" class="form-control">
                                        <option value="">Todos</option>
                                        <?php foreach($medios_pago as $item): ?>
                                            <option value="<?php echo $item->id; ?>" <?php echo ($medio_pago_id == $item->id) ? 'selected' : ''; ?>>
                                                <?php echo $item->nombre; ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="col-md-2 form-group">
                                    <label>Cuenta bancaria</label>
                                    <select name="banco_cuenta_id" class="form-control">
                                        <option value="">Todas</option>
                                        <?php foreach($cuentas as $item): ?>
                                            <option value="<?php echo $item->id; ?>" <?php echo ($banco_cuenta_id == $item->id) ? 'selected' : ''; ?>>
                                                <?php echo $item->banco . ' — ' . $item->descripcion; ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="col-md-2 form-group" style="padding-top:25px;">
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fa fa-search"></i> Filtrar
                                    </button>
                                    <button type="button" onclick="window.print()" class="btn btn-default">
                                        <i class="fa fa-print"></i> Imprimir
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <?php if(!empty($cobros)): ?>

        <!-- RESUMEN -->
        <div class="row no-print" id="cardsResumen" style="margin-bottom:5px !important; height: 45px;">
            <div class="col-md-4">
                <div class="panel panel-success">
                    <div class="panel-heading"><strong><i class="fa fa-money"></i> Total cobrado</strong></div>
                    <div class="panel-body text-center">
                        <h3>S/ <?php echo number_format($total_general, 2); ?></h3>
                        <small><?php echo count($cobros); ?> cobros registrados</small>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="panel panel-default">
                    <div class="panel-heading"><strong><i class="fa fa-credit-card"></i> Por medio de pago</strong></div>
                    <div class="panel-body" style="padding:8px;">
                        <table class="table table-condensed" style="margin:0;">
                            <?php foreach($por_medio_pago as $medio => $monto): ?>
                                <tr>
                                    <td><?php echo $medio; ?></td>
                                    <td class="text-right"><strong>S/ <?php echo number_format($monto, 2); ?></strong></td>
                                </tr>
                            <?php endforeach; ?>
                        </table>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="panel panel-default">
                    <div class="panel-heading"><strong><i class="fa fa-bank"></i> Por cuenta bancaria</strong></div>
                    <div class="panel-body" style="padding:8px;">
                        <?php if(!empty($por_cuenta)): ?>
                            <table class="table table-condensed" style="margin:0;">
                                <?php foreach($por_cuenta as $cuenta => $monto): ?>
                                    <tr>
                                        <td><small><?php echo $cuenta; ?></small></td>
                                        <td class="text-right"><strong>S/ <?php echo number_format($monto, 2); ?></strong></td>
                                    </tr>
                                <?php endforeach; ?>
                            </table>
                        <?php else: ?>
                            <p class="text-muted text-center" style="margin:0;">Sin cobros bancarios</p>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>

        <!-- RESUMEN PARA IMPRESIÓN -->
        <table class="print-only tabla-resumen-print">
            <tr>
                <td style="width:33%; vertical-align:top;">
                    <strong>Total cobrado:</strong> S/ <?php echo number_format($total_general, 2); ?>
                    (<?php echo count($cobros); ?> cobros)
                </td>
                <td style="width:33%; vertical-align:top;">
                    <strong>Por medio de pago:</strong><br>
                    <?php foreach($por_medio_pago as $medio => $monto): ?>
                        <?php echo $medio; ?>: S/ <?php echo number_format($monto, 2); ?><br>
                    <?php endforeach; ?>
                </td>
                <td style="width:33%; vertical-align:top;">
                    <?php if(!empty($por_cuenta)): ?>
                        <strong>Por cuenta bancaria:</strong><br>
                        <?php foreach($por_cuenta as $cuenta => $monto): ?>
                            <?php echo $cuenta; ?>: S/ <?php echo number_format($monto, 2); ?><br>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </td>
            </tr>
        </table>

        <!-- TABLA -->
        <div class="row" style="margin-bottom:5px !important;">
            <div class="col-md-12">
                <div class="panel panel-default reporte-panel">
                    <div class="panel-heading no-print">
                        <strong>Detalle de cobros</strong>
                        <span class="badge pull-right"><?php echo count($cobros); ?> registros</span>
                    </div>
                    <div class="panel-body">
                        <div class="table-responsive">
                            <table id="tablaCobros" class="table table-bordered table-hover table-striped tabla-reporte">
                                <thead>
                                    <tr>
                                        <th>Fecha</th>
                                        <th>Cliente</th>
                                        <th>Comprobante</th>
                                        <th>Medio de pago</th>
                                        <th>Cuenta bancaria</th>
                                        <th>Referencia</th>
                                        <th class="text-right">Importe</th>
                                        <th>Usuario</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach($cobros as $cobro): ?>
                                        <tr>
                                            <td><?php echo date('d/m/Y', strtotime($cobro->fecha_pago)); ?></td>
                                            <td>
                                                <?php echo $cobro->cliente; ?>
                                                <br><small class="text-muted"><?php echo $cobro->cliente_ruc; ?></small>
                                            </td>
                                            <td>
                                                <?php echo $cobro->tipo_comprobante; ?>
                                                <strong><?php echo ' ' . $cobro->serie . '-' . $cobro->numero; ?></strong>
                                            </td>
                                            <td><?php echo $cobro->medio_pago; ?></td>
                                            <td>
                                                <?php if(!empty($cobro->banco)): ?>
                                                    <?php echo $cobro->banco; ?><br>
                                                    <small><?php echo $cobro->cuenta_descripcion . ' — ' . $cobro->numero_cuenta; ?></small>
                                                <?php else: ?>
                                                    <span class="text-muted">-</span>
                                                <?php endif; ?>
                                            </td>
                                            <td><?php echo !empty($cobro->referencia) ? $cobro->referencia : '-'; ?></td>
                                            <td class="text-right">
                                                <strong>S/ <?php echo number_format((float)$cobro->importe, 2); ?></strong>
                                            </td>
                                            <td><?php echo $cobro->usuario_cobro ?? '-'; ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                                <tfoot>
                                    <tr style="font-weight:bold; background:#f9f9f9;">
                                        <th colspan="6" class="text-right">Total:</th>
                                        <th class="text-right">S/ <?php echo number_format($total_general, 2); ?></th>
                                        <th></th>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <?php else: ?>
        <div class="alert alert-info">
            <i class="fa fa-info-circle"></i> No se encontraron cobros para los filtros seleccionados.
        </div>
        <?php endif; ?>

        <!-- PIE DE PÁGINA SOLO IMPRESIÓN -->
        <div class="print-only" style="margin-top:15px; font-size:10px; color:#888; text-align:center; border-top:1px solid #ccc; padding-top:6px;">
            Reporte generado el <?php echo $fecha_reporte; ?> — <?php echo !empty($empresa->nombre) ? $empresa->nombre : ''; ?>
        </div>

    </section>
</div>

<style>
/* ============================================================ */
/* ESTILOS DE PANTALLA */
/* ============================================================ */
.print-only { display: none; }

/* ============================================================ */
/* ESTILOS DE IMPRESIÓN — REPORTE APAISADO */
/* ============================================================ */
@media print {

    @page {
        size: A4 landscape;
        margin: 10mm;
    }

    .main-header, .sidebar, .content-header, .main-footer,
    .no-print, #panelFiltros, #cardsResumen,
    .control-sidebar, .navbar,
    .dataTables_filter, .dataTables_length, .dataTables_paginate, .dataTables_info {
        display: none !important;
    }

    .content-wrapper {
        margin-left: 0 !important;
        padding: 0 !important;
    }

    .print-only { display: block !important; }

    .reporte-panel {
        border: 1px solid #999 !important;
        box-shadow: none !important;
        margin-bottom: 8px !important;
    }

    .tabla-reporte {
        font-size: 9px;
    }
    .tabla-reporte th, .tabla-reporte td {
        padding: 3px 4px !important;
    }
    .tabla-reporte thead tr,
    .tabla-reporte tfoot tr {
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .tabla-resumen-print {
        width: 100%;
        border: none;
        margin-bottom: 10px;
    }
    .tabla-resumen-print td {
        border: none;
        font-size: 10px;
        padding: 4px 10px;
    }
}
</style>

<script>
window.addEventListener('load', function () {
    $('#tablaCobros').DataTable({
        language: {
            lengthMenu: "Mostrar _MENU_ registros", zeroRecords: "No se encontraron cobros",
            info: "Mostrando _START_ al _END_ de _TOTAL_", infoEmpty: "Sin registros",
            infoFiltered: "(filtrado de _MAX_ registros)", search: "Buscar:",
            paginate: { first: "Primero", last: "Último", next: "Siguiente", previous: "Anterior" }
        },
        pageLength: 25, order: [[0, 'asc']]
    });
});
</script>