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

Full Path: C:/wamp64/www/CALIDADWEB/CALIDADWEB/FRONTED/INPUTS/FOR-S-CCI-66.js
File size: 6.39 KB
MIME-type: text/plain
Charset: utf-8

document.addEventListener("DOMContentLoaded", function () {

    const modulos = {
        "EMPAQUE VISCERA": ["empaqueViscera1", "empaqueViscera2", "empaqueVisceraCuchillo", "empaqueVisceraPala"],
        "EMPAQUE CANAL": ["empaqueCanal1", "empaqueCanal2", "empaqueCanal3", "empaqueCanal4", "palaChiller"],
        "DESPRESE Y MARINADO": [
            "marinado1", "marinado2", "marinado3", "marinado4", "marinado5", "marinado6",
            "marinadoCuchillo1", "marinadoCuchillo2", "marinadoCuchillo3", "marinadoCuchillo4",
            "marinadoCuchillo5", "marinadoCuchillo6", "entradaTrolley", "salidaTrolley"
        ],
        "CARNES MOLIDAS": ["carnesMolidas"],
        "FILETEO POLLO": [
            "fileteoPollo", "fileteoPolloTablas",
            "fileteoPolloCuchillo1", "fileteoPolloCuchillo2",
            "fileteoPolloCuchillo3", "fileteoPolloCuchillo4"
        ],
        "FILETEO PAVOS": [
            "fileteoPavos", "fileteoPavos2",
            "fileteoPavosCuchillo1", "fileteoPavosCuchillo2",
            "fileteoPavosCuchillo3", "fileteoPavosCuchillo4",
            "fileteoPavosCuchillo5", "fileteoPavosCuchillo6",
            "fileteoPavosCuchillo7", "fileteoPavosCuchillo8",
            "fileteoPavosCuchillo9", "fileteoPavosCuchillo10",
            "fileteoPavosCuchillo11", "fileteoPavosCuchillo12",
            "fileteoPavosCuchillo13", "fileteoPavosCuchillo14"
        ],
        "DESPACHOS": ["despachos", "iqf", "iqf2", "cambioEmpaque", "reseleccion"],
        "POST PROCESO": [
            "pediluvioPosproceso1", "pediluvioPosproceso2",
            "posproceso1", "posproceso2", "posprocesoCuchillo"
        ]
    };

    const generales = ["zonaBlanca", "basculaProduccion", "pediluvioHielo", "palaHielo"];
    const selectcloroPuntos = document.getElementById("selectcloroPuntos");
    const todosLosIds = Object.values(modulos).flat();

    // 1. FUNCI脫N DE COLOR: Define estrictamente los colores deseados. (CORREGIDA)
    function aplicarColorSegunValor(selectElement) {
        var valorSeleccionado = selectElement.value;

        // Limpiar estilos no relacionados con el color
        selectElement.style.color = "black";
        selectElement.style.border = "";
        selectElement.style.opacity = "";
        selectElement.style.cursor = "";
        
        // 馃毃 Color base/por defecto: BLANCO. 
        selectElement.style.backgroundColor = "white"; 

        // Aplicar el color seg煤n la opci贸n seleccionada
        if (valorSeleccionado === "CUMPLE") {
            selectElement.style.backgroundColor = "lightgreen";
        } else if (valorSeleccionado === "NO CUMPLE") {
            selectElement.style.backgroundColor = "lightcoral";
        } else if (valorSeleccionado === "NO CUMPLE POR LIMPIEZA") {
            selectElement.style.backgroundColor = "lightblue";
        } else if (valorSeleccionado === "N/A") {
            // N/A es el 煤nico que debe ser gris.
            selectElement.style.backgroundColor = "gray"; 
        }
        // Si valorSeleccionado es "", se queda con el fondo blanco.
    }
    
    // 2. FUNCI脫N DE ESTILO DE DESHABILITADO: (Se mantiene, aunque ya no deshabilitemos campos de m贸dulo)
    function aplicarEstilosDeshabilitado(el) {
        el.style.border = "gray";
        el.style.opacity = ""; 
        el.style.cursor = "";
        el.style.color = "black"; 
    }

    // 3. FUNCI脫N PARA ASEGURAR OPCI脫N "N/A"
    function asegurarNA(select) {
        if (!select) return;
        const existe = Array.from(select.options).some(opt => opt.value === "N/A");
        if (!existe) {
            const opt = document.createElement("option");
            opt.value = "N/A";
            opt.textContent = "N/A";
            select.appendChild(opt);
        }
    }

    // 4. FUNCI脫N PRINCIPAL DE HABILITACI脫N/BLOQUEO (Maneja UN solo m贸dulo)
    function habilitarModulo(nombreModulo) {
        const idsModuloActual = modulos[nombreModulo] || [];
        const idsHabilitados = new Set([...idsModuloActual, ...generales]);

        [...todosLosIds, ...generales].forEach(id => {
    const el = document.getElementById(id);
    if (!el) return;

    asegurarNA(el);

    // 馃攽 LISTENER SIEMPRE (una sola vez)
    if (!el.dataset.listenerAdded) {
        el.addEventListener('change', () => aplicarColorSegunValor(el));
        el.dataset.listenerAdded = 'true';
    }

    if (idsHabilitados.has(id)) {

        el.disabled = false;

        if (el.value === "N/A" || el.value === "") {
            el.value = "";
        }

        aplicarColorSegunValor(el);

    } else {

        el.disabled = false;

        if (el.value === "") {
            el.value = "N/A";
        }

        aplicarColorSegunValor(el);
        aplicarEstilosDeshabilitado(el);
    }
});

    }

    // 5. INICIALIZACI脫N
    
    // Inicializa todos los campos de m贸dulo como N/A para que se vean GRISES
    todosLosIds.forEach(id => {
        const el = document.getElementById(id);
        if (el) {
            asegurarNA(el);
            el.value = "N/A"; 
            el.disabled = false;
            aplicarColorSegunValor(el);
            aplicarEstilosDeshabilitado(el);
        }
    });

    // Asegura que los campos generales est茅n habilitados e inician en BLANCO
    generales.forEach(id => {
        const el = document.getElementById(id);
        if (el) {
            asegurarNA(el);
            el.disabled = false;
            el.value = ""; // Inicia vac铆o, lo que lo pinta BLANCO.
            aplicarColorSegunValor(el);
            el.addEventListener('change', () => aplicarColorSegunValor(el));
            el.dataset.listenerAdded = 'true';
        }
    });

    // Evento principal para cambiar de m贸dulo (Manejo de UN solo m贸dulo)
    selectcloroPuntos.addEventListener("change", function () {
        const modulo = this.value;
        if (modulo && modulos[modulo]) {
            habilitarModulo(modulo);
        } else {
            habilitarModulo(null); 
        }
    });
});

// funcion para el sweet alert
document.addEventListener('DOMContentLoaded', function () {

  if (typeof window.swalData !== 'undefined') {

    Swal.fire({
        title: window.swalData.title,
        text: window.swalData.text,
        icon: window.swalData.icon,
        confirmButtonText: 'Aceptar',
        confirmButtonColor: '#3085d6'
    });

  }

});