document.addEventListener("DOMContentLoaded", function () { const fab = document.getElementById("chatbotFab"); const panel = document.getElementById("chatbotPanel"); const dim = document.getElementById("chatbotDim"); const closeBtn = document.getElementById("chatbotClose"); const body = document.getElementById("chatbotBody"); const quickBtns = document.querySelectorAll(".chatbot_quick_btn"); const replies = document.getElementById("chatbotReplies"); if (!fab || !panel || !dim || !closeBtn || !body || !replies) return; function openChatbot() { panel.classList.add("is-open"); panel.setAttribute("aria-hidden", "false"); document.body.style.overflow = "hidden"; scrollToBottom(); } function closeChatbot() { panel.classList.remove("is-open"); panel.setAttribute("aria-hidden", "true"); document.body.style.overflow = ""; } function scrollToBottom() { setTimeout(function () { body.scrollTop = body.scrollHeight; }, 50); } function addMessage(text, type) { const msg = document.createElement("div"); msg.className = "chatbot_msg " + type; msg.innerHTML = text.replace(/\n/g, "
"); body.appendChild(msg); scrollToBottom(); } function getReplyByKey(key) { if (key === "startup") return replies.dataset.replyStartup; if (key === "price") return replies.dataset.replyPrice; if (key === "process") return replies.dataset.replyProcess; if (key === "consult") return replies.dataset.replyConsult; if (key === "store") return replies.dataset.replyStore; return replies.dataset.replyDefault; } fab.addEventListener("click", openChatbot); closeBtn.addEventListener("click", closeChatbot); dim.addEventListener("click", closeChatbot); quickBtns.forEach(function (btn) { btn.addEventListener("click", function () { const text = btn.getAttribute("data-msg") || ""; const replyKey = btn.getAttribute("data-reply") || "default"; if (!panel.classList.contains("is-open")) { openChatbot(); } addMessage(text, "user"); setTimeout(function () { addMessage(getReplyByKey(replyKey), "bot"); }, 400); }); }); document.addEventListener("keydown", function (e) { if (e.key === "Escape" && panel.classList.contains("is-open")) { closeChatbot(); } }); });