@extends('layouts.master')
@section('title') Inicio @endsection
@section('content')
<!-- this is breadcrumbs -->
@component('components.breadcrumb')
@slot('li_1') Inicio @endslot
@slot('li_2') Panel Facturación @endslot
@slot('title') Clase Tarifa @endslot
@endcomponent

<!-- 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="#modalCrear">
                    <i class="fas fa-plus"></i>  Nueva Clase Tarifa
                </button>
                <div class="table-responsive">
                    <table id="tablaSectores" class="table table-striped mb-0 table-hover">
                        <thead class="table-light">
                            <tr>
                                <th>#</th>
                                <th>Nombre</th>
                                <th>Descripción</th>
                                <th>Acciones</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($clases as $clase)
                                <tr>
                                    <td>{{ $loop->iteration }}</td>
                                    <td>{{ $clase->nombre_clase }}</td>
                                    <td>{{ $clase->descripcion }}</td>
                                    <td>
                                        <button class="btn btn-sm btn-warning btn-editar"
                                            data-id="{{ $clase->id_clase_tarifa }}"
                                            data-nombre="{{ $clase->nombre_clase }}"
                                            data-descripcion="{{ $clase->descripcion }}"
                                            data-bs-toggle="modal"
                                            data-bs-target="#modalEditar">
                                            <i class="las la-pen"></i>
                                        </button>

                                        <button class="btn btn-sm btn-danger btn-eliminar"
                                            data-id="{{ $clase->id_clase_tarifa }}">
                                            <i class="las la-trash-alt"></i>
                                        </button>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>


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

<!-- Modal Crear -->
<div class="modal fade" id="modalCrear" tabindex="-1">
    <div class="modal-dialog">
        <form id="formCrear" class="modal-content">
            @csrf
            <div class="modal-header">
                <h5 class="modal-title">Nueva Clase de Tarifa</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="mb-3">
                    <label>Nombre</label>
                    <input type="text" name="nombre_clase" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label>Descripción</label>
                    <textarea name="descripcion" class="form-control"></textarea>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary">Guardar</button>
                <button class="btn btn-secondary" data-bs-dismiss="modal" type="button">Cancelar</button>
            </div>
        </form>
    </div>
</div>

<!-- Modal Editar -->
<div class="modal fade" id="modalEditar" tabindex="-1">
    <div class="modal-dialog">
        <form id="formEditar" class="modal-content">
            @csrf
            @method('PUT')
            <input type="hidden" id="edit_id">
            <div class="modal-header">
                <h5 class="modal-title">Editar Clase</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="mb-3">
                    <label>Nombre</label>
                    <input type="text" name="nombre_clase" id="edit_nombre" class="form-control" required>
                </div>
                <div class="mb-3">
                    <label>Descripción</label>
                    <textarea name="descripcion" id="edit_descripcion" class="form-control"></textarea>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-warning">Actualizar</button>
                <button class="btn btn-secondary" data-bs-dismiss="modal" type="button">Cancelar</button>
            </div>
        </form>
    </div>
</div>



<!--end row-->


@endsection
@section('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>
$(document).ready(function () {
    // Crear
    $('#formCrear').submit(function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('clase-tarifa.store') }}",
            method: 'POST',
            data: $(this).serialize(),
            success: function () {
                $('#modalCrear').modal('hide');
                Swal.fire('Registrado', 'Clase de tarifa creada', 'success').then(() => location.reload());
            },
            error: function (xhr) {
                Swal.fire('Error', 'No se pudo guardar.', 'error');
            }
        });
    });

    // Cargar datos al editar
    $('.btn-editar').click(function () {
        $('#edit_id').val($(this).data('id'));
        $('#edit_nombre').val($(this).data('nombre'));
        $('#edit_descripcion').val($(this).data('descripcion'));
    });

    // Editar
    $('#formEditar').submit(function (e) {
        e.preventDefault();
        let id = $('#edit_id').val();
        $.ajax({
            url: `/clase-tarifa/${id}`,
            method: 'POST',
            data: $(this).serialize(),
            success: function () {
                $('#modalEditar').modal('hide');
                Swal.fire('Actualizado', 'Clase actualizada correctamente', 'success').then(() => location.reload());
            },
            error: function () {
                Swal.fire('Error', 'No se pudo actualizar.', 'error');
            }
        });
    });

    // Eliminar
    $('.btn-eliminar').click(function () {
        let id = $(this).data('id');
        Swal.fire({
            title: '¿Eliminar?',
            text: "Esta acción no se puede deshacer",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: 'Sí, eliminar',
            cancelButtonText: 'Cancelar'
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: `/clase-tarifa/${id}`,
                    method: 'POST',
                    data: {
                        _token: '{{ csrf_token() }}',
                        _method: 'DELETE'
                    },
                    success: function () {
                        Swal.fire('Eliminado', '', 'success').then(() => location.reload());
                    },
                    error: function () {
                        Swal.fire('Error', 'No se pudo eliminar.', 'error');
                    }
                });
            }
        });
    });
});
</script>






@endsection
