<?php $__env->startSection('content'); ?>
<div class="container-fluid">
	<div class="row">
		<div class="col-md-8 col-md-offset-2">
                    <div class="panel panel-default">
                        <div class="panel-heading"><center>Listado de Créditos del Cliente: <b><?php echo e($ccli." ".$dcli); ?></b></center></div>
                            <div class="panel-body">
                                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                                    <ul class="nav navbar-nav navbar-right">				
                                      <!--  <li><a href="<?php echo e(url('/')); ?>"> Opciones </a></li>--> 
                                       <!-- <li><a href="<?php echo e(url('/Mod_usuario')); ?>"> Atras </a></li>-->
                                    </ul>
                                </div>
                                <?php if(count($errors) > 0): ?>
                                        <div class="alert alert-danger">
						<strong>Whoops!</strong> Hay algunos problemas!!!.<br><br>
                                                    <ul>
							<?php foreach($errors->all() as $error): ?>
                                                            <li><?php echo e($error); ?></li>
							<?php endforeach; ?>
                                                    </ul>
					</div>
				    <?php endif; ?>
                                        
                                    <div class="table-responsive">                                        
                                        <table class="table table-striped table-hover table-bordered">
                                            <tr>
                                                <th class="text-center"><input type="checkbox" id="select-all"></th>
                                                <th class="text-center">Factura</th>
                                                <th class="text-center" colspan="3">Valor</th>
                                                <th class="text-center">Usuario</th>
                                                <th class="text-center" colspan="2">Fecha</th>
                                                <th class="text-center">Acción</th>
                                            </tr>
                                            
                                            <tr class="text-info">
                                                <th class="text-center"></th>
                                                <th class="text-center">#</th>
                                                <th class="text-center">Compra</th>
                                                <th class="text-center">Abonado</th>
                                                <th class="text-center">Restante</th>
                                                <th class="text-center">Fiador</th>
                                                <th class="text-center">Venta</th>
                                                <th class="text-center">Actualización</th>
                                                <th class="text-center">Abonar</th>
                                            </tr>
                                            
                                            <?php foreach($crecli as $item): ?>
                                            <tr>
                                                <td class="text-center"><input type="checkbox" class="credito-checkbox" data-restante="<?php echo e($item->vcompra - $item->vpagado); ?>" value="<?php echo e($item->IdVenta_Fin); ?>" name="ventas_id[]"></td>
                                                <td class="text-right"><a href="../detcrecli/<?php echo e($item->idcredito); ?>/<?php echo e($item->IdVenta_Fin); ?>/<?php echo e($ccli); ?>" target="_blank"><?php echo e($item->IdVenta_Fin); ?></a></td>
                                                <td class="text-right"><?php echo e($item->vcompra); ?></td>
                                                <td class="text-right"><?php echo e($item->vpagado); ?></td>
                                                <td class="text-right"><?php echo e($item->vcompra - $item->vpagado); ?></td>
                                                <td class="text-right"><?php echo e($item->Nombres); ?> <?php echo e($item->Apellidos); ?></td>
                                                <td class="text-right"><?php echo e($item->created_at); ?></td>
                                                <td class="text-right"><?php echo e($item->updated_at); ?></td>
                                                <td class="text-right"><a href="../abocrecli/<?php echo e($item->IdVenta_Fin); ?>/<?php echo e($ccli); ?>" target="_blank"><img src="<?php echo e(url('images/dine.png')); ?>" width="50px" height="30px" /></a></td>
                                            </tr>
                                            <?php endforeach; ?>
                                                <tr><td colspan="7"></td></tr>
                                                <tr>
                                                  <td class="text-right"></td>
                                                  <td class="text-right"><b>Total:</b></td>
                                                  <td class="text-right"><?php echo e($sumcom); ?></td>
                                                  <td class="text-right"><?php echo e($sumpag); ?></td>
                                                  <td class="text-right"><?php echo e($sumcom-$sumpag); ?></td>
                                                  <td colspan="4"></td>
                                                </tr>

                                                <tfoot>
                                <tr>
                                    <td colspan="4" class="text-right"><b>Total seleccionado:</b></td>
                                    <td class="text-right" id="total-seleccionado">0</td>
             
                                    <td colspan="4" class="text-center">
                                        <?php echo Form::open(['route' => 'credito.pagarTodo', 'method' => 'POST']); ?>

                                            <?php echo Form::hidden('cliente_id', $ccli); ?>

                                            <input type="hidden" name="ventas_id[]" id="ventas-seleccionadas">
                                            <?php echo Form::submit('Pagar Todo', ['class' => 'btn btn-success', 'onclick' => "return confirm('¿Estás seguro de que deseas pagar todos los créditos seleccionados?')"]); ?>

                                        <?php echo Form::close(); ?>

                                    </td>
                                </tr>

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

<script>
    document.getElementById('select-all').addEventListener('click', function() {
        const checkboxes = document.querySelectorAll('.credito-checkbox');
        checkboxes.forEach(chk => chk.checked = this.checked);
        calcularTotalSeleccionado();
    });

    document.querySelectorAll('.credito-checkbox').forEach(chk => {
        chk.addEventListener('change', calcularTotalSeleccionado);
    });

    function calcularTotalSeleccionado() {
        let total = 0;
        const checkboxes = document.querySelectorAll('.credito-checkbox:checked');
        const ids = [];

        checkboxes.forEach(chk => {
            total += parseFloat(chk.getAttribute('data-restante'));
            ids.push(chk.value);
        });

        document.getElementById('total-seleccionado').innerText = total.toFixed(2);
        document.getElementById('ventas-seleccionadas').value = ids.join(',');
    }
</script>
<?php $__env->stopSection(); ?>


<?php echo $__env->make('app', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>