<div class="content-wrapper">
    <section class="content-header">
        <h1>Comisiones por Ventas</h1>
    </section>

    <section class="content">

        <!-- Filtro de fechas -->
        <div class="panel panel-default">
            <div class="panel-body" style="padding:10px 15px;">
                <form method="get" action="<?php echo base_url('index.php/finanzas/comisiones'); ?>"
                      class="form-inline">
                    <div class="form-group" style="margin-right:10px;">
                        <label style="margin-right:5px;">Desde:</label>
                        <input type="date" name="desde" class="form-control input-sm"
                               value="<?php echo $desde; ?>">
                    </div>
                    <div class="form-group" style="margin-right:10px;">
                        <label style="margin-right:5px;">Hasta:</label>
                        <input type="date" name="hasta" class="form-control input-sm"
                               value="<?php echo $hasta; ?>">
                    </div>
                    <button type="submit" class="btn btn-primary btn-sm">
                        <i class="fa fa-search"></i> Filtrar
                    </button>
                    <a href="<?php echo base_url('index.php/finanzas/comisiones'); ?>"
                       class="btn btn-default btn-sm" style="margin-left:5px;">
                        <i class="fa fa-refresh"></i> Período actual
                    </a>
                    <span class="text-muted" style="margin-left:15px; font-size:12px;">
                        <i class="fa fa-calendar"></i>
                        Período configurado: <strong><?php echo $config->comision_periodo; ?></strong>
                        &nbsp;|&nbsp;
                        Comisión: <strong><?php echo $config->comision_porcentaje; ?>%</strong>
                        &nbsp;|&nbsp;
                        <?php echo date('d/m/Y', strtotime($periodo['desde'])); ?> —
                        <?php echo date('d/m/Y', strtotime($periodo['hasta'])); ?>
                    </span>
                </form>
            </div>
        </div>

        <!-- Tabla de vendedores -->
        <div class="panel panel-primary">
            <div class="panel-heading">
                <strong><i class="fa fa-users"></i> Resumen por vendedor</strong>
            </div>
            <div class="panel-body">
                <div class="table-responsive">
                    <table id="tablaComisiones" class="table table-bordered table-hover table-striped">
                        <thead>
                            <tr>
                                <th>Vendedor</th>
                                <th class="text-right">Cuota</th>
                                <th class="text-right">Total abonado</th>
                                <th class="text-center">% Cumplimiento</th>
                                <th class="text-right">Comisión base</th>
                                <th class="text-right">Bono</th>
                                <th class="text-right">Total a pagar</th>
                                <th>Comprobantes</th>
                                <th style="width:80px;">Acciones</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($vendedores as $v):
                                $pct = $v->comision_cuota > 0
                                    ? round(((float)$v->total_abonado / (float)$v->comision_cuota) * 100, 1)
                                    : 0;
                                $bono = $this->Comisiones_model->calcular_bono(
                                    $v->vendedor_id,
                                    (float)$v->total_abonado,
                                    (float)$v->comision_cuota
                                );
                                $total_pagar = (float)$v->total_comision + $bono;
                                $color_pct = $pct >= 100 ? 'success' : ($pct >= 50 ? 'warning' : 'danger');
                            ?>
                                <tr>
                                    <td><?php echo $v->vendedor_nombre; ?></td>
                                    <td class="text-right">S/ <?php echo number_format($v->comision_cuota, 2); ?></td>
                                    <td class="text-right">S/ <?php echo number_format($v->total_abonado, 2); ?></td>
                                    <td class="text-center">
                                        <div class="progress" style="margin:0; height:18px;">
                                            <div class="progress-bar progress-bar-<?php echo $color_pct; ?>"
                                                 style="width:<?php echo min($pct, 100); ?>%; line-height:18px; font-size:11px;">
                                                <?php echo $pct; ?>%
                                            </div>
                                        </div>
                                    </td>
                                    <td class="text-right">S/ <?php echo number_format($v->total_comision, 2); ?></td>
                                    <td class="text-right">
                                        <?php if ($bono > 0): ?>
                                            <span class="text-success">
                                                <i class="fa fa-trophy"></i>
                                                S/ <?php echo number_format($bono, 2); ?>
                                            </span>
                                        <?php else: ?>
                                            <span class="text-muted">—</span>
                                        <?php endif; ?>
                                    </td>
                                    <td class="text-right text-bold">
                                        S/ <?php echo number_format($total_pagar, 2); ?>
                                    </td>
                                    <td class="text-center"><?php echo $v->nro_comprobantes; ?></td>
                                    <td>
                                        <a href="<?php echo base_url('index.php/finanzas/comisiones/ver/'.$v->vendedor_id.'?desde='.$desde.'&hasta='.$hasta); ?>"
                                           class="btn btn-info btn-xs" title="Ver detalle">
                                            <i class="fa fa-eye"></i>
                                        </a>
                                        <a href="<?php echo base_url('index.php/finanzas/comisiones/tramos/'.$v->vendedor_id); ?>"
                                           class="btn btn-warning btn-xs" title="Configurar tramos">
                                            <i class="fa fa-cogs"></i>
                                        </a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

    </section>
</div>

<script>
window.addEventListener('load', function () {
    if (typeof $.fn.DataTable !== 'undefined') {
        $('#tablaComisiones').DataTable({
            language: {
                lengthMenu: "Mostrar _MENU_ registros",
                zeroRecords: "Sin registros",
                info: "Mostrando del _START_ al _END_ de _TOTAL_",
                search: "Buscar:",
                paginate: { first:"Primero", last:"Último", next:"Siguiente", previous:"Anterior" }
            },
            pageLength: 25,
            order: [[0, 'asc']],
            columnDefs: [{ orderable: false, targets: [3, 8] }]
        });
    }
});
</script>