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

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

$(document).ready(function () {
    // ========== TABLA 1: Verificación ==========
    $(".btnVeriConChillVisc").click(function (e) {
        e.preventDefault();

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

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

        let tablaVeriConChillVisc = $("#tblVeriConChillVisc").DataTable({
            "ajax": {
                "url": "../../BACKEND/controltablas/FOR-S-CCI-90.php",
                "type": "POST",
                "data": { opcion: 18 },
                "dataSrc": ""
            },
            "columns": [
                { "data": "id", "width": "50px" },
                { "data": "fecha", "width": "100px" },
                { "data": "hora", "width": "80px" },
                { "data": "hm1" }, { "data": "hm2" }, { "data": "hm3" }, { "data": "hm4" },
                { "data": "hm5" }, { "data": "hm6" }, { "data": "hm7" }, { "data": "hm8" },
                { "data": "hm9" }, { "data": "hm10" }, { "data": "hm11" }, { "data": "hm12" },
                { "data": "hm13" }, { "data": "hm14" }, { "data": "hm15" }, { "data": "hm16" },
                { "data": "hm17" }, { "data": "htemp" }, { "data": "TrazabilidadH" },
                { "data": "mm1" }, { "data": "mm2" }, { "data": "mm3" }, { "data": "mm4" },
                { "data": "mm5" }, { "data": "mm6" }, { "data": "mm7" }, { "data": "mm8" },
                { "data": "mm9" }, { "data": "mm10" }, { "data": "mm11" }, { "data": "mm12" },
                { "data": "mm13" }, { "data": "mm14" }, { "data": "mm15" }, { "data": "mm16" },
                { "data": "mm17" }, { "data": "mtemp" }, { "data": "TrazabilidadM" },
                { "data": "pm1" }, { "data": "pm2" }, { "data": "pm3" }, { "data": "pm4" },
                { "data": "pm5" }, { "data": "pm6" }, { "data": "pm7" }, { "data": "pm8" },
                { "data": "pm9" }, { "data": "pm10" }, { "data": "pm11" }, { "data": "pm12" },
                { "data": "pm13" }, { "data": "pm14" }, { "data": "pm15" }, { "data": "pm16" },
                { "data": "pm17" }, { "data": "ptemp" }, { "data": "TrazabilidadP" },
                { "data": "cm1" }, { "data": "cm2" }, { "data": "cm3" }, { "data": "cm4" },
                { "data": "cm5" }, { "data": "cm6" }, { "data": "cm7" }, { "data": "cm8" },
                { "data": "cm9" }, { "data": "cm10" }, { "data": "cm11" }, { "data": "cm12" },
                { "data": "cm13" }, { "data": "cm14" }, { "data": "cm15" }, { "data": "cm16" },
                { "data": "cm17" }, { "data": "ctemp" }, { "data": "TrazabilidadC" },
                { "data": "observacion" },
                { "data": "reponsable" },
                { "data": "aprobo" },
                { "data": "general" },

                {
                    "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>";
                    }
                }
            ],
            "columnDefs": [{ "targets": "_all", "defaultContent": "-" }],
            "language": {
                "emptyTable": "No hay información",
                "info": "Mostrando _START_ a _END_ de _TOTAL_ registros",
                "lengthMenu": "Mostrar _MENU_ registros",
                "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: " CONTROL DE PROCESO ENFRIAMIENTO DE VISCERAS",
                    text: '<i class="fa-lg fa-solid fa-file-excel"></i> GENERAR EXCEL',
                    className: "btn btn-success",
                    exportOptions: {
                        columns: ':not(:last-child):not(:nth-last-child(2))'
                    }
                }
            ],
            "initComplete": function () {
                swal.close();
            }
        });

        // ELIMINAR
        $('#tblVeriConChillVisc tbody').off('click', '.btnBorrar').on('click', '.btnBorrar', function (e) {
            e.preventDefault();
            e.stopPropagation();
            let data = tablaVeriConChillVisc.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-90.php",
                        type: "POST",
                        data: { opcion: 20, id: data.id },
                        success: function () {
                            tablaVeriConChillVisc.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
        $('#tblVeriConChillVisc tbody').off('click', '.btnEditar').on('click', '.btnEditar', function (e) {
            e.preventDefault();
            e.stopPropagation();
            let $row = $(this).closest('tr');
            $row.find('td').each(function (index) {
                if (index > 0 && index < 83) {
                    let valorActual = $(this).text();
                    let nombreColumna = tablaVeriConChillVisc.settings().init().columns[index].data;
                    $(this).html(`
                            <input type="text" 
                                class="form-control form-control-sm" 
                                style="min-width: 70px; width: 100%; padding: 2px;" 
                                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
        $('#tblVeriConChillVisc tbody').off('click', '.btnGuardar').on('click', '.btnGuardar', function (e) {
            e.preventDefault();
            e.stopPropagation();
            let $row = $(this).closest('tr');
            let id = tablaVeriConChillVisc.row($row).data().id;
            let datosActualizados = { opcion: 21, id: id };
            $row.find('input').each(function () {
                datosActualizados[$(this).data('campo')] = $(this).val();
            });
            $.ajax({
                url: "../../BACKEND/controltablas/FOR-S-CCI-90.php",
                type: "POST",
                data: datosActualizados,
                success: function () {
                    tablaVeriConChillVisc.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
                    });
                    tablaVeriConChillVisc.ajax.reload(null, false);
                }
            });
            return false;
        });

        // CANCELAR
        $('#tblVeriConChillVisc tbody').off('click', '.btnCancelar').on('click', '.btnCancelar', function (e) {
            e.preventDefault();
            e.stopPropagation();
            tablaVeriConChillVisc.ajax.reload(null, false);
            return false;
        });
    });

    // ========== TABLA 2: Control ==========
    $(".btnConChillVisc").click(function (e) {
        e.preventDefault();

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

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

        let tablaConChillVisc = $("#tblConChillVisc").DataTable({
            "ajax": {
                "url": "../../BACKEND/controltablas/FOR-S-CCI-90.php",
                "type": "POST",
                "data": { opcion: 19 },
                "dataSrc": ""
            },
            "columns": [
                { "data": "id", "width": "50px" },
                { "data": "fecha", "width": "100px" },
                { "data": "hora", "width": "80px" },
                { "data": "tipoAve" },
                { "data": "lote" },
                { "data": "hm1" }, { "data": "hm2" }, { "data": "hm3" }, { "data": "hm4" }, { "data": "hm5" },
                { "data": "htemp" },
                { "data": "generalmoni1" },
                { "data": "mm1" }, { "data": "mm2" }, { "data": "mm3" }, { "data": "mm4" }, { "data": "mm5" },
                { "data": "mtemp" },
                { "data": "generalmoni2" },
                { "data": "pm1" }, { "data": "pm2" }, { "data": "pm3" }, { "data": "pm4" }, { "data": "pm5" },
                { "data": "ptemp" },
                { "data": "generalmoni3" },
                { "data": "cm1" }, { "data": "cm2" }, { "data": "cm3" }, { "data": "cm4" }, { "data": "cm5" },
                { "data": "ctemp" },
                { "data": "general" },
                { "data": "codigo" },
                { "data": "responsable" },
                { "data": "observaciones" },
                { "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": {
                "emptyTable": "No hay información",
                "info": "Mostrando _START_ a _END_ de _TOTAL_ registros",
                "lengthMenu": "Mostrar _MENU_ registros",
                "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: "Control Vísceras",
                    text: '<i class="fa-lg fa-solid fa-file-excel"></i> GENERAR EXCEL',
                    className: "btn btn-success",
                    exportOptions: {
                        columns: ':not(:last-child):not(:nth-last-child(2))'
                    }
                }
            ],
            "initComplete": function () {
                swal.close();
            }
        });

        // ELIMINAR
        $('#tblConChillVisc tbody').off('click', '.btnBorrar').on('click', '.btnBorrar', function (e) {
            e.preventDefault();
            e.stopPropagation();
            let data = tablaConChillVisc.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-90.php",
                        type: "POST",
                        data: { opcion: 22, id: data.id },
                        success: function () {
                            tablaConChillVisc.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
        $('#tblConChillVisc tbody').off('click', '.btnEditar').on('click', '.btnEditar', function (e) {
            e.preventDefault();
            e.stopPropagation();
            let $row = $(this).closest('tr');
            $row.find('td').each(function (index) {
                if (index > 0 && index < 38) {
                    let valorActual = $(this).text();
                    let nombreColumna = tablaConChillVisc.settings().init().columns[index].data;

                    $(this).html(`<input type="text" class="form-control form-control-sm" value="${valorActual}" data-campo="${nombreColumna}">`);
                    $(this).html(`
                            <input type="text" 
                                class="form-control form-control-sm" 
                                style="min-width: 70px; width: 100%; padding: 2px;" 
                                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
        $('#tblConChillVisc tbody').off('click', '.btnGuardar').on('click', '.btnGuardar', function (e) {
            e.preventDefault();
            e.stopPropagation();
            let $row = $(this).closest('tr');
            let id = tablaConChillVisc.row($row).data().id;
            let datosActualizados = { opcion: 23, id: id };
            $row.find('input').each(function () {
                datosActualizados[$(this).data('campo')] = $(this).val();
            });
            $.ajax({
                url: "../../BACKEND/controltablas/FOR-S-CCI-90.php",
                type: "POST",
                data: datosActualizados,
                success: function () {
                    tablaConChillVisc.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
                    });
                    tablaConChillVisc.ajax.reload(null, false);
                }
            });
            return false;
        });

        // CANCELAR
        $('#tblConChillVisc tbody').off('click', '.btnCancelar').on('click', '.btnCancelar', function (e) {
            e.preventDefault();
            e.stopPropagation();
            tablaConChillVisc.ajax.reload(null, false);
            return false;
        });
    });
});