// Floating Chatbot Helper class ChatbotHelper { constructor() { this.isOpen = false; this.isMinimized = false; this.messages = []; this.init(); } init() { this.createFloatingButton(); this.createChatModal(); this.bindEvents(); this.addWelcomeMessage(); } createFloatingButton() { // Create floating button this.floatingBtn = document.createElement('div'); this.floatingBtn.className = 'chatbot-floating-btn'; this.floatingBtn.innerHTML = ` Help `; document.body.appendChild(this.floatingBtn); } createChatModal() { // Create modal container this.modal = document.createElement('div'); this.modal.className = 'chatbot-modal'; this.modal.innerHTML = `
`; document.body.appendChild(this.modal); } bindEvents() { // Floating button click this.floatingBtn.addEventListener('click', () => { this.toggleChat(); }); // Close button document.getElementById('closeBtn').addEventListener('click', () => { this.closeChat(); }); // Minimize button document.getElementById('minimizeBtn').addEventListener('click', () => { this.minimizeChat(); }); // Send button document.getElementById('chatbotSendBtn').addEventListener('click', () => { this.sendMessage(); }); // Enter key in input document.getElementById('chatbotInput').addEventListener('keypress', (e) => { if (e.key === 'Enter') { this.sendMessage(); } }); // Input change document.getElementById('chatbotInput').addEventListener('input', (e) => { const sendBtn = document.getElementById('chatbotSendBtn'); sendBtn.disabled = !e.target.value.trim(); }); // Quick question buttons document.querySelectorAll('.quick-question-btn').forEach(btn => { btn.addEventListener('click', (e) => { const question = e.target.getAttribute('data-question'); document.getElementById('chatbotInput').value = question; this.sendMessage(); }); }); // Click outside to close this.modal.addEventListener('click', (e) => { if (e.target === this.modal) { this.closeChat(); } }); } addWelcomeMessage() { this.addMessage('Hi! I\'m here to help with OncoConnect questions. Ask me about accounts, image uploads, trials, or challenges!', 'bot'); } toggleChat() { if (this.isOpen) { this.closeChat(); } else { this.openChat(); } } openChat() { this.isOpen = true; this.isMinimized = false; this.modal.classList.add('show'); this.floatingBtn.classList.add('active'); document.getElementById('chatbotInput').focus(); } closeChat() { this.isOpen = false; this.isMinimized = false; this.modal.classList.remove('show'); this.floatingBtn.classList.remove('active'); } minimizeChat() { this.isMinimized = !this.isMinimized; this.modal.classList.toggle('minimized', this.isMinimized); } async sendMessage() { const input = document.getElementById('chatbotInput'); const message = input.value.trim(); if (!message) return; // Add user message this.addMessage(message, 'user'); input.value = ''; // Disable send button const sendBtn = document.getElementById('chatbotSendBtn'); sendBtn.disabled = true; // Show typing indicator this.showTyping(); try { const response = await fetch('http://localhost:5000/chat', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ message: message }) }); if (!response.ok) { throw new Error('Network response was not ok'); } const data = await response.json(); this.hideTyping(); this.addMessage(data.response, 'bot'); } catch (error) { this.hideTyping(); this.addMessage('Sorry, I encountered an error. Please try again or contact support for assistance.', 'bot'); console.error('Chatbot error:', error); } sendBtn.disabled = false; } addMessage(text, sender) { const messagesContainer = document.getElementById('chatbotMessages'); const messageDiv = document.createElement('div'); messageDiv.className = `chatbot-message ${sender}-message`; if (sender === 'user') { messageDiv.innerHTML = ` `; } else { messageDiv.innerHTML = ` `; } messagesContainer.appendChild(messageDiv); messagesContainer.scrollTop = messagesContainer.scrollHeight; } showTyping() { document.getElementById('chatbotTyping').style.display = 'block'; const messagesContainer = document.getElementById('chatbotMessages'); messagesContainer.scrollTop = messagesContainer.scrollHeight; } hideTyping() { document.getElementById('chatbotTyping').style.display = 'none'; } escapeHtml(text) { const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } getCurrentTime() { const now = new Date(); return now.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); } } // Initialize chatbot when DOM is loaded document.addEventListener('DOMContentLoaded', function() { window.chatbot = new ChatbotHelper(); });