File "FOR-S-CCI-62.js"

Full Path: C:/wamp64/www/CALIDADWEB/CALIDADWEB/FRONTED/CONTROLTABLAS.JS/FOR-S-CCI-62.js
File size: 8.66 KB
MIME-type: text/plain
Charset: utf-8


$(document).ready(function () {
    $(".btnLimpiezaDesinfeccion").click(function (e) {
        e.preventDefault();

        swal.fire({
            title: "Cargando datos...",
            allowOutsideClick: false,
            didOpen: () => { swal.showLoading(); }
        });

        if ($.fn.DataTable.isDataTable('#tblLimpiezaDesinfeccion')) {
            $('#tblLimpiezaDesinfeccion').DataTable().destroy();
        }

        let tablalimpieza = $("#tblLimpiezaDesinfeccion").DataTable({
            "ajax": {
                "url": "../../BACKEND/controltablas/FOR-S-CCI-62.php",
                "type": "POST",
                "data": { opcion:74 },
                "dataSrc": ""
            },
            "columns": [
                { "data": "id", "width": "50px" },
                { "data": "fecha", "width": "100px" },
                { data: "sustancia" },
                { data: "area" },
                { data: "cantidad" },
                { data: "observaciones" },
                { data: "entrego" },
                { data: "aprobo" },
                {
                    "data": null,
                    "orderable": false,
                    "width": "80px",
                    "className": "text-center",
                    "render": function () {
                        return "<button class='btn btn-danger btn-sm btnBorrar'><i class='fas fa-trash-alt'></i></button>";
                    }
                },
                {
                    "data": null,
                    "orderable": false,
                    "width": "80px",
                    "className": "text-center",
                    "render": function (data, type, row) {
                        return "<button class='btn btn-primary btn-sm btnEditar' data-id='" + row.id + "'><i class='fas fa-edit'></i></button>";
                    }
                }
            ],
            "language": {
                "decimal": "",
                "emptyTable": "No hay información",
                "info": "Mostrando _START_ a _END_ de _TOTAL_ registros",
                "infoEmpty": "Mostrando 0 a 0 de 0 registros",
                "infoFiltered": "(Filtrado de _MAX_ total registros)",
                "thousands": ",",
                "lengthMenu": "Mostrar _MENU_ registros",
                "loadingRecords": "Cargando...",
                "processing": "Procesando...",
                "search": "Buscar:",
                "zeroRecords": "Sin resultados encontrados",
                "paginate": {
                    "first": "Primero",
                    "last": "Último",
                    "next": "Siguiente",
                    "previous": "Anterior"
                }
            },
            "scrollX": true,
            "scrollCollapse": true,
            "autoWidth": false,
            "dom": "Bfrtip",
            "buttons": [
                {
                    extend: "pageLength",
                    className: "btn btn-warning"
                },
                {
                    extend: "excelHtml5",
                    autoFilter: true,
                    sheetName: " SUSTANCIA DE LIMPIEZA Y DESINFECCION OPERATIVAS",
                    title: " SUSTANCIA DE LIMPIEZA Y DESINFECCION OPERATIVAS",
                    text: '<i class="fa-lg fa-solid fa-file-excel"></i> GENERAR EXCEL',
                    className: "btn btn-success",
                    exportOptions: {
                        columns: [0, 1, 2, 3, 4, 5, 6, 7, 8]
                    }
                }
            ],
            "initComplete": function () {
                swal.close();
            }
        });

        // --- ELIMINAR ---
        $('#tblLimpiezaDesinfeccion tbody').off('click', '.btnBorrar').on('click', '.btnBorrar', function (e) {
            e.preventDefault();
            e.stopPropagation();

            let data = tablalimpieza.row($(this).parents('tr')).data();

            swal.fire({
                title: "¿Eliminar registro?",
                text: "ID: " + data.id,
                icon: "warning",
                showCancelButton: true,
                confirmButtonColor: "#d33",
                cancelButtonColor: "#3085d6",
                cancelButtonText: "Cancelar",
                confirmButtonText: "Sí, borrar"
            }).then((result) => {
                if (result.isConfirmed) {
                    $.ajax({
                        url: "../../BACKEND/controltablas/FOR-S-CCI-62.php",
                        type: "POST",
                        data: { opcion: 75, id: data.id },
                        success: function (response) {
                            tablalimpieza.ajax.reload(null, false);
                            swal.fire({
                                icon: "success",
                                title: "¡Éxito!",
                                text: "Registro eliminado correctamente",
                                showConfirmButton: false,
                                timer: 1500
                            });
                        },
                        error: function () {
                            swal.fire({
                                icon: "error",
                                title: "Error",
                                text: "No se pudo eliminar el registro",
                                showConfirmButton: false,
                                timer: 1500
                            });
                        }
                    });
                }
            });

            return false;
        });

        // --- EDITAR ---
        $('#tblLimpiezaDesinfeccion tbody').off('click', '.btnEditar').on('click', '.btnEditar', function (e) {
            e.preventDefault();
            e.stopPropagation();

            let $row = $(this).closest('tr');
            let data = tablalimpieza.row($row).data();

            $row.find('td').each(function (index) {
                if (index > 0 && index < 8) {
                    let valorActual = $(this).text();
                    let nombreColumna = tablalimpieza.settings().init().columns[index].data;
                    $(this).html(`<input type="text" class="form-control form-control-sm" value="${valorActual}" data-campo="${nombreColumna}">`);
                }
            });

            $(this).removeClass('btn-primary btnEditar')
                .addClass('btn-success btnGuardar')
                .html('<i class="fas fa-save"></i>');

            $row.find('.btnBorrar').removeClass('btn-danger btnBorrar')
                .addClass('btn-secondary btnCancelar')
                .html('<i class="fas fa-times"></i>');

            return false;
        });

        // --- GUARDAR ---
        $('#tblLimpiezaDesinfeccion tbody').off('click', '.btnGuardar').on('click', '.btnGuardar', function (e) {
            e.preventDefault();
            e.stopPropagation();

            let $row = $(this).closest('tr');
            let id = tablalimpieza.row($row).data().id;
            let datosActualizados = { opcion: 76, id: id };

            $row.find('input').each(function () {
                let campo = $(this).data('campo');
                let valor = $(this).val();
                datosActualizados[campo] = valor;
            });

            $.ajax({
                url: "../../BACKEND/controltablas/FOR-S-CCI-62.php",
                type: "POST",
                data: datosActualizados,
                success: function (response) {
                    tablalimpieza.ajax.reload(null, false);
                    swal.fire({
                        icon: "success",
                        title: "¡Éxito!",
                        text: "Registro actualizado correctamente",
                        showConfirmButton: false,
                        timer: 1500
                    });
                },
                error: function () {
                    swal.fire({
                        icon: "error",
                        title: "Error",
                        text: "No se pudo actualizar el registro",
                        showConfirmButton: false,
                        timer: 1500
                    });
                    tablalimpieza.ajax.reload(null, false);
                }
            });

            return false;
        });

        // --- CANCELAR ---
        $('#tblLimpiezaDesinfeccion tbody').off('click', '.btnCancelar').on('click', '.btnCancelar', function (e) {
            e.preventDefault();
            e.stopPropagation();

            tablalimpieza.ajax.reload(null, false);

            return false;
        });
    });
});