async function postJson(url, body){ const res = await fetch(url, {method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify(body)}); const data = await res.json().catch(()=>({ok:false,error:'Respuesta inválida'})); if(!res.ok || !data.ok) throw new Error(data.error || 'Error'); return data; } function addBubble(cls, text){ const div = document.createElement('div'); div.className = 'bubble ' + cls; div.textContent = text; document.getElementById('chatLog').appendChild(div); document.getElementById('chatLog').scrollTop = 999999; } let session = null; function levelHint(){ return "Escala 0–4: 0=No existe, 1=Informal, 2=Parcial, 3=Definido+mensual, 4=Controlado+KPIs."; } function syncAnonUI(){ const anon = document.getElementById('anon_mode').checked; const leadFields = document.getElementById('leadFields'); const consent = document.getElementById('consent'); if(anon){ leadFields.style.display = 'none'; consent.checked = false; consent.disabled = true; } else { leadFields.style.display = ''; consent.disabled = false; } } document.getElementById('anon_mode').addEventListener('change', syncAnonUI); syncAnonUI(); document.getElementById('btnStart').addEventListener('click', async ()=>{ const msg0 = document.getElementById('msg0'); msg0.textContent = ''; try{ const anon = document.getElementById('anon_mode').checked; const consent = (!anon && document.getElementById('consent').checked) ? 1 : 0; if(!anon && consent !== 1){ throw new Error('Para registrar tus datos debes marcar el consentimiento. O activa modo anónimo.'); } const payload = anon ? { anonymous: 1, tipo_negocio: document.getElementById('tipo_negocio').value, consent: 0 } : { nombre: document.getElementById('nombre').value, email: document.getElementById('email').value, whatsapp: document.getElementById('whatsapp').value, negocio: document.getElementById('negocio').value, tipo_negocio: document.getElementById('tipo_negocio').value, consent }; session = await postJson('../api/init_lead.php', payload); localStorage.setItem('sp_eval360_session', JSON.stringify({ chat_session_id: session.chat_session_id, session_token: session.session_token, diagnosis_id: session.diagnosis_id, public_token: session.public_token })); document.getElementById('chatBox').style.display = 'block'; addBubble('bot', 'Iniciemos el diagnóstico. Responde con números cuando aplique.'); addBubble('bot', levelHint()); const step = await postJson('../api/chat_step.php', { chat_session_id: session.chat_session_id, session_token: session.session_token }); addBubble('bot', step.next.question); } catch(e){ msg0.textContent = e.message; } }); async function sendAnswer(){ const msg = document.getElementById('msg'); msg.textContent = ''; try{ if(!session) throw new Error('Inicia el chat primero.'); const input = document.getElementById('answer'); const a = input.value; if(a.trim() === '') return; input.value = ''; addBubble('user', a); const step = await postJson('../api/chat_step.php', { chat_session_id: session.chat_session_id, session_token: session.session_token, answer: a }); if(step.is_complete){ addBubble('bot', 'Listo. Calculando resultados...'); await postJson('../api/calc_score.php', { chat_session_id: session.chat_session_id, session_token: session.session_token, diagnosis_id: session.diagnosis_id }); try { await postJson('../api/generate_plan.php', { diagnosis_id: session.diagnosis_id }); } catch(e){} window.location.href = 'result.php'; return; } if(step.next && step.next.input_type === 'level'){ addBubble('bot', levelHint()); } addBubble('bot', step.next.question); } catch(e){ msg.textContent = e.message; } } document.getElementById('send').addEventListener('click', sendAnswer); document.getElementById('answer').addEventListener('keydown', (ev)=>{ if(ev.key === 'Enter') sendAnswer(); });