
<?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 Facturación <?php $__env->endSlot(); ?>
<?php $__env->slot('title'); ?> Tarifas <?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="#modalCrearTarifa">
                    <i class="fas fa-plus"></i>  Nueva Tarifa
                </button>
                <div class="table-responsive">
                    <table id="tablaSectores" class="table table-striped mb-0 table-hover">
                        <thead class="table-light">
                            <tr>
                                <th>Categoría</th>
                                <th>Monto Fijo</th>
                                <th>Monto Variable</th>
                                <th>Vigencia</th>
                                <th>Estado</th>
                                <th>Acciones</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php $__currentLoopData = $tarifas; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $t): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <tr>
                            <td><?php echo e($t->categoriaTarifa->nombre_categoria ?? '-'); ?></td>
                            <td>S/ <?php echo e(number_format($t->monto_fijo, 2)); ?></td>
                            <td>S/ <?php echo e(number_format($t->monto_variable, 2)); ?></td>
                            <td><?php echo e($t->vigencia_inicio); ?> a <?php echo e($t->vigencia_fin ?? 'Indefinida'); ?></td>
                            <td><?php echo e($t->estado); ?></td>
                            <td>
                                <button class="btn btn-sm btn-warning btn-editar"
                                data-id="<?php echo e($t->id_tarifa); ?>"
                                data-id_categoria="<?php echo e($t->id_categoria_tarifa); ?>"
                                data-fijo="<?php echo e($t->monto_fijo); ?>"
                                data-variable="<?php echo e($t->monto_variable); ?>"
                                data-inicio="<?php echo e($t->vigencia_inicio); ?>"
                                data-fin="<?php echo e($t->vigencia_fin); ?>"
                                data-estado="<?php echo e($t->estado); ?>"
                                data-bs-toggle="modal" data-bs-target="#modalEditarTarifa">
                                <i class="las la-edit"></i>
                                </button>
                                <button class="btn btn-sm btn-danger btn-eliminar" data-id="<?php echo e($t->id_tarifa); ?>">
                                <i class="las la-trash"></i>
                                </button>
                            </td>
                            </tr>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                        </tbody>
                    </table>


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

<!-- Modal Crear -->
<div class="modal fade" id="modalCrearTarifa" tabindex="-1">
  <div class="modal-dialog">
    <form id="formCrearTarifa" class="modal-content">
      <?php echo csrf_field(); ?>
      <div class="modal-header"><h5 class="modal-title">Nueva Tarifa</h5></div>
      <div class="modal-body">
        <div class="mb-2">
          <label class="form-label">Categoría</label>
          <select name="id_categoria_tarifa" class="form-select" required>
            <option value="">Seleccione</option>
            <?php $__currentLoopData = $categorias; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $c): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
              <option value="<?php echo e($c->id_categoria_tarifa); ?>"><?php echo e($c->nombre_categoria); ?></option>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
          </select>
        </div>
        <div class="mb-2">
          <label>Monto Fijo</label>
          <input type="number" step="0.01" name="monto_fijo" class="form-control" required>
        </div>
        <div class="mb-2">
          <label>Monto Variable (por m³)</label>
          <input type="number" step="0.01" name="monto_variable" class="form-control" required>
        </div>
        <div class="mb-2">
          <label>Vigencia Inicio</label>
          <input type="date" name="vigencia_inicio" class="form-control" required>
        </div>
        <div class="mb-2">
          <label>Vigencia Fin</label>
          <input type="date" name="vigencia_fin" class="form-control">
        </div>
        <div class="mb-2">
          <label>Estado</label>
          <select name="estado" class="form-select">
            <option value="Activo">Activo</option>
            <option value="Inactivo">Inactivo</option>
          </select>
        </div>
      </div>
      <div class="modal-footer">
        <button class="btn btn-primary">Guardar</button>
        <button class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
      </div>
    </form>
  </div>
</div>

<!-- Modal Editar -->
<div class="modal fade" id="modalEditarTarifa" tabindex="-1">
  <div class="modal-dialog">
    <form id="formEditarTarifa" class="modal-content">
      <?php echo csrf_field(); ?> <?php echo method_field('PUT'); ?>
      <input type="hidden" id="edit_id">
      <div class="modal-header"><h5 class="modal-title">Editar Tarifa</h5></div>
      <div class="modal-body">
        <div class="mb-2">
          <label class="form-label">Categoría</label>
          <select name="id_categoria_tarifa" id="edit_categoria" class="form-select" required>
            <?php $__currentLoopData = $categorias; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $c): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
              <option value="<?php echo e($c->id_categoria_tarifa); ?>"><?php echo e($c->nombre_categoria); ?></option>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
          </select>
        </div>
        <div class="mb-2">
          <label>Monto Fijo</label>
          <input type="number" step="0.01" name="monto_fijo" id="edit_fijo" class="form-control" required>
        </div>
        <div class="mb-2">
          <label>Monto Variable</label>
          <input type="number" step="0.01" name="monto_variable" id="edit_variable" class="form-control" required>
        </div>
        <div class="mb-2">
          <label>Vigencia Inicio</label>
          <input type="date" name="vigencia_inicio" id="edit_inicio" class="form-control" required>
        </div>
        <div class="mb-2">
          <label>Vigencia Fin</label>
          <input type="date" name="vigencia_fin" id="edit_fin" class="form-control">
        </div>
        <div class="mb-2">
          <label>Estado</label>
          <select name="estado" id="edit_estado" class="form-select">
            <option value="Activo">Activo</option>
            <option value="Inactivo">Inactivo</option>
          </select>
        </div>
      </div>
      <div class="modal-footer">
        <button class="btn btn-warning">Actualizar</button>
        <button class="btn btn-secondary" data-bs-dismiss="modal">Cancelar</button>
      </div>
    </form>
  </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 () {
        $("#tablaSectores").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('#tablaSectores_wrapper .col-md-6:eq(0)');
    });
</script>
<!-- agregar -->
<script>
    $(function () {
    $('#formCrearTarifa').submit(function (e) {
        e.preventDefault();
        $.post("<?php echo e(route('tarifas.store')); ?>", $(this).serialize())
        .done(() => {
            $('#modalCrearTarifa').modal('hide');
            Swal.fire('Registrado', 'Tarifa creada correctamente', 'success').then(() => location.reload());
        })
        .fail(xhr => {
            let msj = xhr.responseJSON?.errors
                ? Object.values(xhr.responseJSON.errors).map(e => `• ${e}`).join('<br>')
                : 'Error inesperado.';
            Swal.fire('Error', msj, 'error');
        });
    });

    $('.btn-editar').click(function () {
        $('#edit_id').val($(this).data('id'));
        $('#edit_categoria').val($(this).data('id_categoria'));
        $('#edit_fijo').val($(this).data('fijo'));
        $('#edit_variable').val($(this).data('variable'));
        $('#edit_inicio').val($(this).data('inicio'));
        $('#edit_fin').val($(this).data('fin'));
        $('#edit_estado').val($(this).data('estado'));
    });

    $('#formEditarTarifa').submit(function (e) {
        e.preventDefault();
        let id = $('#edit_id').val();
        $.ajax({
        url: `/tarifas/${id}`,
        method: 'POST',
        data: $(this).serialize(),
        success: () => {
            $('#modalEditarTarifa').modal('hide');
            Swal.fire('Actualizado', 'Tarifa modificada correctamente', 'success').then(() => location.reload());
        },
        error: xhr => {
            let msj = xhr.responseJSON?.errors
                ? Object.values(xhr.responseJSON.errors).map(e => `• ${e}`).join('<br>')
                : 'Error inesperado.';
            Swal.fire('Error', msj, 'error');
        }
        });
    });

    $('.btn-eliminar').click(function () {
        let id = $(this).data('id');
        Swal.fire({
        title: '¿Eliminar tarifa?',
        text: 'Esta acción no se puede deshacer',
        icon: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Sí, eliminar',
        cancelButtonText: 'Cancelar'
        }).then(result => {
        if (result.isConfirmed) {
            $.ajax({
            url: `/tarifas/${id}`,
            method: 'POST',
            data: {
                _token: '<?php echo e(csrf_token()); ?>',
                _method: 'DELETE'
            },
            success: () => {
                Swal.fire('Eliminado', 'Tarifa eliminada correctamente', 'success').then(() => location.reload());
            },
            error: () => {
                Swal.fire('Error', 'No se pudo eliminar la tarifa', '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/tarifas/index.blade.php ENDPATH**/ ?>