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

<!-- start page content -->
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body mb-n3">
                

                <div class="mb-3">
                    <form id="form-proyeccion" class="row g-3 mb-4">
                        <div class="col-md-3">
                            <label for="anio" class="form-label">Año</label>
                            <input type="number" id="anio" name="anio" class="form-control" 
                                value="{{ date('Y') }}" min="2000" max="{{ date('Y') }}">
                        </div>
                        <div class="col-md-3">
                            <label for="mes" class="form-label">Mes</label>
                            <select id="mes" name="mes" class="form-control">
                                @foreach (range(1,12) as $m)
                                    <option value="{{ $m }}" {{ date('m') == $m ? 'selected' : '' }}>
                                        {{ \Carbon\Carbon::create()->locale('es')->month($m)->translatedFormat('F') }}
                                    </option>
                                @endforeach
                            </select>
                        </div>

                        <div class="col-md-3 d-flex align-items-end">
                            <button type="submit" class="btn btn-primary w-100">Consultar</button>
                        </div>
                    </form>
                </div>


                 <div class="card shadow">
                    <div class="card-body">
                        <h5>Resultado</h5>
                        <div id="resultado">
                            <div class="alert alert-info">Seleccione un mes y año para ver la proyección.</div>
                        </div>
                    </div>
                </div>
            </div>
            <!--end card-body-->
        </div>
        <!--end card-->
    </div>
    <!--end col-->
</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 () {
        $("#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 () {
    $('#form-proyeccion').on('submit', function (e) {
        e.preventDefault();

        let anio = $('#anio').val();
        let mes  = $('#mes').val();

        $.ajax({
            url: "{{ route('reportes.proyeccion_recaudacion') }}",
            type: "GET",
            data: { anio: anio, mes: mes },
            beforeSend: function () {
                Swal.fire({
                    title: 'Consultando...',
                    text: 'Por favor espere',
                    allowOutsideClick: false,
                    didOpen: () => {
                        Swal.showLoading();
                    }
                });
            },
            success: function (response) {
                Swal.close();
                console.log("Respuesta del servidor:", response);

                if (response.success) {
                    $('#resultado').html(`
                        <table class="table table-bordered">
                            <thead class="table-light">
                                <tr>
                                    <th>Mes</th>
                                    <th>Total Facturado (S/.)</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td>${response.mes}</td>
                                    <td class="fw-bold">S/ ${parseFloat(response.total).toFixed(2)}</td>
                                </tr>
                            </tbody>
                        </table>
                    `);
                } else {
                    $('#resultado').html(`<div class="alert alert-warning">${response.message}</div>`);
                }
            },
            error: function (xhr, status, error) {
                Swal.close();
                console.error("Error AJAX:", xhr.responseText);
                Swal.fire('Error', 'Ocurrió un problema al consultar los datos.', 'error');
            }
        });
    });
});
</script>

@endsection
