Ciao, vuoi informazioni sul nostro servizio?
Oppure hai intenzione di sviluppare un prodotto?
`;
modalMessages.appendChild(holder);
modalMessages.scrollTop=modalMessages.scrollHeight;
}
function hideTypingModal(){ const el=document.getElementById("typingInline"); if(el) el.remove(); }
function openModal(){
mask.style.display="block";
modal.style.display="block";
document.body.style.overflow="hidden";
renderModal();
setTimeout(()=>modalInput.focus(),50);
}
function closeModal(){
mask.style.display="none";
modal.style.display="none";
document.body.style.overflow="";
}
function sendMessage(fromPopup=false){
const src = fromPopup ? modalInput : input;
const text = src.value.trim();
if(!text) return;
ensureChatId();
// aggiorna stato e UI (solo popup)
HISTORY.push({role:'user', text});
src.value="";
renderModal();
showTypingModal();
fetch(webhookUrl,{
method:"POST",
headers:{ "Content-Type":"application/json" },
body: JSON.stringify({ question: text, chat_id: CHAT_ID })
})
.then(r=>r.json())
.then(data=>{
hideTypingModal();
const payload = Array.isArray(data) ? (data[0]||{}) : (data||{});
const reply = payload.reply || {};
const raw = reply.details || payload.data || reply.text || "";
const html = /{
hideTypingModal();
HISTORY.push({role:'bot', html:"
⚠️ Errore di connessione.
"}); renderModal();
});
if(!fromPopup) openModal();
}
// eventi
input.addEventListener("keypress", e=>{ if(e.key==="Enter") sendMessage(false); });
miniSendBtn.addEventListener("click", ()=>sendMessage(false));
openFullChatIcon.addEventListener("click", openModal);
modalInput.addEventListener("keypress", e=>{ if(e.key==="Enter") sendMessage(true); });
modalSendBtn.addEventListener("click", ()=>sendMessage(true));
closeBtn.addEventListener("click", closeModal);
mask.addEventListener("click", closeModal);
// cestino
trashBtn.addEventListener("click", ()=>{
if(confirm("Eliminare questa chat?")){
HISTORY = [];
renderModal();
input.value=""; modalInput.value="";
}
});