
<?php $__env->startSection('title'); ?> Inicio <?php $__env->stopSection(); ?>
<?php $__env->startSection('content'); ?>
<!-- this is breadcrumbs -->
<?php $__env->startComponent('components.breadcrumb'); ?>
<?php $__env->slot('li_1'); ?> Inicio <?php $__env->endSlot(); ?>
<?php $__env->slot('li_2'); ?> Panel Catastro <?php $__env->endSlot(); ?>
<?php $__env->slot('title'); ?> Registro de Pagos <?php $__env->endSlot(); ?>
<?php echo $__env->renderComponent(); ?>

<!-- start page content -->
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body mb-n3">
                <button type="button" class="btn btn-primary btn-sm px-4 mt-0 mb-3" 
                    data-bs-toggle="modal" 
                    data-bs-target="#modalAbrirCaja">
                    <i class="fas fa-plus"></i>  Abrir Caja
                </button>

            


                <div class="table-responsive">
                    <table id="tablaRecibos" class="table table-striped mb-0 table-hover">
                        <thead class="table-light">
                            <tr>
                                <th>ID</th>
                                <th>Nombre</th>
                                <th>Saldo Inicial</th>
                                <th>Saldo Actual</th>
                                <th>Estado</th>
                                <th>Acciones</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $__empty_1 = true; $__currentLoopData = $cajas; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $caja): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                                <tr>
                                    <td><?php echo e($caja->id_caja); ?></td>
                                    <td><?php echo e($caja->nombre); ?></td>
                                    <td>S/ <?php echo e(number_format($caja->saldo_inicial, 2)); ?></td>
                                    <td>S/ <?php echo e(number_format($caja->saldo_actual, 2)); ?></td>
                                    <td>
                                        <?php if($caja->estado === 'abierta'): ?>
                                            <span class="badge bg-success">Abierta</span>
                                        <?php else: ?>
                                            <span class="badge bg-secondary">Cerrada</span>
                                        <?php endif; ?>
                                    </td>
                                    <td>
                                        <a href="route('caja.historial', $caja->id_caja)" class="btn btn-info btn-sm">
                                            <i class="fas fa-history"></i> Historial
                                        </a>
                                        <?php if($caja->estado === 'abierta'): ?>
                                            <button type="button" 
                                                    class="btn btn-danger btn-sm btn-cerrar-caja" 
                                                    data-id="<?php echo e($caja->id_caja); ?>">
                                                <i class="fas fa-lock"></i> Cerrar
                                            </button>
                                        <?php else: ?>
                                            <button class="btn btn-secondary btn-sm" disabled>
                                                <i class="fas fa-lock"></i> Cerrada
                                            </button>
                                        <?php endif; ?>

                                    </td>
                                </tr>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                                
                            <?php endif; ?>
                        </tbody>
                    </table>






                </div>
            </div>
            <!--end card-body-->
        </div>
        <!--end card-->
    </div>
    <!--end col-->
</div>



<!-- Modal -->

<!-- Modal Abrir Caja -->
<div class="modal fade" id="modalAbrirCaja" tabindex="-1" aria-labelledby="modalAbrirCajaLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header bg-primary text-white">
        <h5 class="modal-title" id="modalAbrirCajaLabel">
          <i class="fas fa-cash-register"></i> Abrir Caja
        </h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Cerrar"></button>
      </div>
      <div class="modal-body">
        <form id="formAbrirCaja">
          <?php echo csrf_field(); ?>
          <div class="row">
            <div class="col-md-6 mb-3">
              <label for="nombre" class="form-label">Nombre de Caja</label>
              <input type="text" name="nombre" id="nombre" class="form-control" required>
            </div>
            <div class="col-md-6 mb-3">
              <label for="saldo_inicial" class="form-label">Saldo Inicial</label>
              <input type="number" step="0.01" name="saldo_inicial" id="saldo_inicial" class="form-control" required>
            </div>
            <div class="col-12 mb-3">
              <label for="descripcion" class="form-label">Descripción</label>
              <input type="text" name="descripcion" id="descripcion" class="form-control">
            </div>
          </div>
          <button type="submit" class="btn btn-primary w-100">
            <i class="fas fa-check"></i> Guardar
          </button>
        </form>
      </div>
    </div>
  </div>
</div>










<!--end row-->


<?php $__env->stopSection(); ?>
<?php $__env->startSection('script'); ?>
<!-- jQuery y DataTables -->

<!-- jQuery y DataTables -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>

<!-- DataTables Buttons -->
<script src="https://cdn.datatables.net/buttons/2.3.6/js/dataTables.buttons.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/2.3.6/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.3.6/js/buttons.print.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.3.6/js/buttons.colVis.min.js"></script>

<!-- DataTables CSS -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.3.6/css/buttons.dataTables.min.css">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<!-- tablas -->
<script>
    $(document).ready(function () {
        $("#tablaRecibos").DataTable({
            "responsive": true,
            "lengthChange": true,
            "autoWidth": false,
            "buttons": [
                {
                    extend: 'collection',
                    text: 'Opciones',
                    buttons: [
                        { extend: 'copy', text: 'Copiar' },
                        { extend: 'csv', text: 'CSV' },
                        { extend: 'excel', text: 'Excel', title: 'Periodos Académicos' },
                        { extend: 'pdf', text: 'PDF' },
                        { extend: 'print', text: 'Imprimir' }
                    ]
                },
                { extend: 'colvis', text: 'Visibilidad de Columna' }
            ],
            "paging": true,
            "searching": true,
            "info": true,
            "language": {
                "search": "Buscar:",
                "lengthMenu": "Mostrar _MENU_ registros por página",
                "info": "Mostrando _START_ a _END_ de _TOTAL_ entradas",
                "paginate": {
                    "first": "Primero",
                    "previous": "Anterior",
                    "next": "Siguiente",
                    "last": "Último"
                }
            }
        }).buttons().container().appendTo('#tablaRecibos_wrapper .col-md-6:eq(0)');
    });
</script>

<script>
$(document).ready(function () {
    $('#formAbrirCaja').submit(function(e){
        e.preventDefault();

        $.ajax({
            url: "<?php echo e(route('caja.abrir')); ?>",
            method: "POST",
            data: $(this).serialize(),
            success: function(response){
                $('#modalAbrirCaja').modal('hide');
                Swal.fire({
                    icon: 'success',
                    title: 'Caja abierta',
                    text: response.message ?? 'Caja abierta correctamente',
                    timer: 2000,
                    showConfirmButton: false
                });
                setTimeout(() => location.reload(), 2000);
            },
            error: function(xhr){
                let errors = xhr.responseJSON?.errors;
                let mensaje = 'Ocurrió un error al abrir la caja';
                if(errors){
                    mensaje = Object.values(errors).join('<br>');
                }
                Swal.fire({
                    icon: 'error',
                    title: 'Error',
                    html: mensaje
                });
            }
        });
    });
});
</script>
<script>
$(document).on('click', '.btn-cerrar-caja', function() {
    let id = $(this).data('id');

    Swal.fire({
        title: '¿Cerrar caja?',
        text: "Esta acción no se puede deshacer.",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Sí, cerrar',
        cancelButtonText: 'Cancelar'
    }).then((result) => {
        if (result.isConfirmed) {
            $.ajax({
                url: "<?php echo e(url('caja/cerrar')); ?>/" + id,
                type: "POST",
                data: {
                    _token: "<?php echo e(csrf_token()); ?>"
                },
                success: function(response) {
                    Swal.fire({
                        title: '¡Caja cerrada!',
                        text: response.message,
                        icon: 'success',
                        timer: 1500,
                        showConfirmButton: false
                    }).then(() => {
                        location.reload();
                    });
                },
                error: function(xhr) {
                    Swal.fire({
                        title: 'Error',
                        text: 'No se pudo cerrar la caja.',
                        icon: 'error'
                    });
                }
            });
        }
    });
});
</script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.master', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH D:\SISTEMAS\agua\resources\views/caja/index.blade.php ENDPATH**/ ?>