//guardar etiqeuta function abrirEtiquetador() { const id_chat = document.getElementById('idchat').value; if (id_chat != "") { document.getElementById('etiqueta_id_chat').value = id_chat; fetch('get_etiquetas.php?id_chat=' + id_chat) .then(res => res.json()) .then(data => { const contenedor = document.getElementById('listaEtiquetas'); contenedor.innerHTML = ''; data.etiquetas.forEach(et => { const estaAsignada = et.asignada; // true o false const checkIcon = estaAsignada ? '✔ ' : ''; const borderClass = estaAsignada ? 'border border-light' : ''; contenedor.innerHTML += ` ${checkIcon}${et.nombre} `; }); $('#modalEtiquetas').modal('show'); }); } } function toggleEtiqueta(element) { const nombre = element.getAttribute('data-nombre'); const id_chat = document.getElementById('idchat').value; const estaAsignada = element.textContent.trim().startsWith('✔'); let nuevaAccion = estaAsignada ? 'remover' : 'asignar'; // Cambiar visual if (estaAsignada) { element.textContent = nombre; element.classList.remove('border', 'border-light'); } else { element.textContent = '✔ ' + nombre; element.classList.add('border', 'border-light'); } // Guardar o eliminar en DB fetch('toggle_etiqueta.php', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: `id_chat=${encodeURIComponent(id_chat)}&nombre=${encodeURIComponent(nombre)}&accion=${nuevaAccion}` }) .then(res => res.json()) .then(data => { $('#chats').load("consultas?CargaChats=si"); }); } function guardarEtiquetas() { const id_chat = document.getElementById('etiqueta_id_chat').value; const etiquetas = Array.from(document.querySelectorAll('#listaEtiquetas .badge')).map(el => el.textContent); fetch('guardar_etiquetas.php', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ id_chat, etiquetas }) }) .then(res => res.json()) .then(resp => { if (resp.success) { $('#modalEtiquetas').modal('hide'); location.reload(); // recarga los chats para actualizar visualmente } }); } document.querySelectorAll('.chat-item').forEach(chat => { chat.addEventListener('click', function() { // Quitar active actual const activeChat = document.querySelector('.chat-item.active'); if (activeChat) activeChat.classList.remove('active'); // Añadir active al clickeado this.classList.add('active'); // Obtener id del chat const idChat = this.getAttribute('data-chat-id'); cargarChat(idChat); }); });