function initChatInterface() { // Create main chat container const chatContainer = document.createElement("div"); chatContainer.id = "chat-container"; chatContainer.style.cssText = ` position: fixed; bottom: 20px; right: 20px; width: 380px; max-width: 90vw; height: 500px; max-height: 70vh; background-color: white; border-radius: 16px; box-shadow: 0 5px 25px rgba(0,0,0,0.15); z-index: 1000; display: flex; flex-direction: column; font-family: 'Segoe UI', Roboto, sans-serif; overflow: hidden; transition: all 0.3s ease; transform: translateY(20px); opacity: 0; `; // Chat header with gradient chatContainer.innerHTML = `
💬

Furniture Expert

We're online to help

`; document.body.appendChild(chatContainer); // Floating chat button with animation const chatButton = document.createElement("button"); chatButton.id = "chat-button"; chatButton.innerHTML = ` 💬 `; chatButton.style.cssText = ` position: fixed; bottom: 20px; right: 20px; width: 60px; height: 60px; background: linear-gradient(135deg, #d39e00ff 0%, #f4a742 100%); color: white; border: none; border-radius: 50%; cursor: pointer; box-shadow: 0 4px 15px rgba(0,0,0,0.2); z-index: 999; display: flex; align-items: center; justify-content: center; font-size: 30px; font-weight: 600; transition: all 0.3s ease; animation: pulse 2s infinite, float 3s ease-in-out infinite; display: none; `; // Add pulse animation const style = document.createElement("style"); style.textContent = ` @keyframes pulse { 0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(244, 167, 66, 0.7); } 70% { transform: scale(1.05); box-shadow: 0 0 0 15px rgba(244, 167, 66, 0); } 100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(244, 167, 66, 0); } } @keyframes float { 0% { transform: translateY(0px); } 50% { transform: translateY(-5px); } 100% { transform: translateY(0px); } } @keyframes popIn { 0% { transform: translateY(20px); opacity: 0; } 100% { transform: translateY(0); opacity: 1; } } `; document.head.appendChild(style); document.body.appendChild(chatButton); // Show pop-up animation when page loads setTimeout(() => { chatContainer.style.animation = "popIn 0.4s ease-out forwards"; // Add welcome message displayAssistantResponse(`
Assalam-o-Alaikum! 👋 I'm your personal furniture assistant.
I can help you:
How can I help you today?
`); showSuggestions([ "Show sofas under 50,000", "Best plants for home", "New arrivals", "Help me order", ]); }, 1000); // Toggle chat visibility const chatHeader = document.getElementById("chat-header"); const minimizeBtn = document.getElementById("minimize-chat"); const closeBtn = document.getElementById("close-chat"); chatHeader.addEventListener("click", () => { if (chatContainer.style.display === "none") { chatContainer.style.display = "flex"; minimizeBtn.textContent = "−"; } }); minimizeBtn.addEventListener("click", (e) => { e.stopPropagation(); chatContainer.style.height = chatContainer.style.height === "60px" ? "500px" : "60px"; minimizeBtn.textContent = chatContainer.style.height === "60px" ? "+" : "−"; }); closeBtn.addEventListener("click", (e) => { e.stopPropagation(); chatContainer.style.display = "none"; chatButton.style.display = "flex"; }); chatButton.addEventListener("click", () => { chatContainer.style.display = "flex"; chatButton.style.display = "none"; minimizeBtn.textContent = "−"; chatContainer.style.height = "500px"; }); // Add typing indicator function window.showTypingIndicator = function () { const chatBox = document.getElementById("chat-box"); const typingDiv = document.createElement("div"); typingDiv.id = "typing-indicator"; typingDiv.style.cssText = ` text-align: left; margin: 10px 0; display: flex; align-items: center; gap: 8px; `; typingDiv.innerHTML = `
`; chatBox.appendChild(typingDiv); chatBox.scrollTop = chatBox.scrollHeight; // Add typing dots animation const typingStyle = document.createElement("style"); typingStyle.textContent = ` .typing-dots { display: flex; gap: 4px; } .typing-dots div { width: 8px; height: 8px; background: #666; border-radius: 50%; animation: typingAnimation 1.4s infinite ease-in-out; } .typing-dots div:nth-child(1) { animation-delay: 0s; } .typing-dots div:nth-child(2) { animation-delay: 0.2s; } .typing-dots div:nth-child(3) { animation-delay: 0.4s; } @keyframes typingAnimation { 0%, 60%, 100% { transform: translateY(0); } 30% { transform: translateY(-5px); } } `; document.head.appendChild(typingStyle); }; // Remove typing indicator function window.hideTypingIndicator = function () { const typingIndicator = document.getElementById("typing-indicator"); if (typingIndicator) { typingIndicator.remove(); } }; } // ======= Enhanced Chat Message Handling ======= async function sendChatMessage() { const input = document.getElementById("user-input"); const message = input.value.trim(); const imageUpload = document.getElementById("image-upload"); const chatBox = document.getElementById("chat-box"); if (imageUpload.files[0]) { // Handle image upload const file = imageUpload.files[0]; const reader = new FileReader(); reader.onload = async function (e) { // Display user's image in chat chatBox.innerHTML += `
You
`; showTypingIndicator(); try { const response = await fetch("/chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ image: e.target.result }), }); const data = await response.json(); hideTypingIndicator(); displayAssistantResponse(data.reply); // Show suggestions if available if (data.suggestions) { showSuggestions(data.suggestions); } } catch (error) { hideTypingIndicator(); displayAssistantResponse( "❌ Couldn't process the image. Please try again or describe what you're looking for." ); } imageUpload.value = ""; chatBox.scrollTop = chatBox.scrollHeight; }; reader.readAsDataURL(file); } else if (message) { // Handle text message chatBox.innerHTML += `
${message}
You
`; input.value = ""; showTypingIndicator(); try { const response = await fetch("/chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ message: message }), }); const data = await response.json(); hideTypingIndicator(); displayAssistantResponse(data.reply); // Show suggestions if available if (data.suggestions) { showSuggestions(data.suggestions); } } catch (error) { hideTypingIndicator(); displayAssistantResponse( "❌ Sorry, I'm having trouble responding. Please try again in a moment." ); } } } // Enhanced assistant response display function displayAssistantResponse(response) { const chatBox = document.getElementById("chat-box"); // Format links properly if (typeof response === "string") { // Convert URLs to clickable links response = response.replace( /(https?:\/\/[^\s]+)/g, '$1' ); // In your chat interface code function displayAssistantResponse(response) { const chatBox = document.getElementById("chat-box"); const msgDiv = document.createElement("div"); msgDiv.className = "assistant-message"; msgDiv.innerHTML = `

${response.message || response.reply}

`; if (response.link) { const link = document.createElement("a"); link.href = response.link.includes('
Products matching your search:
`; products.forEach((product) => { html += `
${product.name}
Rs. ${product.price.toLocaleString()}
View Details →
`; }); html += `
Need help deciding? Just ask!
`; response = html; } catch (e) { console.error("Product parsing error:", e); } } } // Add timestamp const now = new Date(); const timeString = now.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", }); chatBox.innerHTML += `
${response}
Assistant • ${timeString}
`; chatBox.scrollTop = chatBox.scrollHeight; } // Show quick reply suggestions function showSuggestions(suggestions) { const suggestionBox = document.getElementById("suggestion-box"); suggestionBox.innerHTML = ""; suggestions.forEach((suggestion) => { const button = document.createElement("button"); button.textContent = suggestion; button.style.cssText = ` padding: 8px 12px; background: white; border: 1px solid #ddd; border-radius: 16px; font-size: 13px; cursor: pointer; white-space: nowrap; transition: all 0.2s; `; button.addEventListener("mouseenter", () => { button.style.background = "#f5f5f5"; }); button.addEventListener("mouseleave", () => { button.style.background = "white"; }); button.addEventListener("click", () => { document.getElementById("user-input").value = suggestion; sendChatMessage(); }); suggestionBox.appendChild(button); }); } // ======= Event Listeners ======= document.addEventListener("DOMContentLoaded", () => { initChatInterface(); // Handle Enter key in input const input = document.getElementById("user-input"); input?.addEventListener("keypress", (e) => { if (e.key === "Enter") { sendChatMessage(); } }); // Handle send button click const sendButton = document.getElementById("send-button"); sendButton?.addEventListener("click", sendChatMessage); // Handle image upload const imageUpload = document.getElementById("image-upload"); imageUpload?.addEventListener("change", () => { if (imageUpload.files[0]) { sendChatMessage(); } }); }); // ======= Navbar Section ======= const menuIcon = document.getElementById("menuIcon"); const navLinks = document.querySelector(".nav-links"); menuIcon.addEventListener("click", () => { navLinks.classList.toggle("active"); }); // ======= Product Cards Section ======= document.addEventListener("DOMContentLoaded", () => { const items = { plant: [ "/static/images/plant3.png", "/static/images/plant4.webp", "/static/images/plant5.png", "/static/images/plant6.png", ], table: [ "/static/images/table2.png", "/static/images/table3.webp", "/static/images/table4.webp", "/static/images/table5.png", ], lamp: [ "/static/images/lamp1.webp", "/static/images/lamp2.webp", "/static/images/lamp3.webp", "/static/images/lamp4.png", "/static/images/lamp5.webp", "/static/images/lamp6.webp", ], sofa: [ "/static/images/sofa3.webp", "/static/images/sofa4.webp", "/static/images/sofa5.png", "/static/images/sofa6.png", ], }; document.querySelectorAll(".plus-btn").forEach((button) => { button.addEventListener("click", () => { const item = button.dataset.item; const card = document.getElementById(`${item}-card`); const mainImage = button.previousElementSibling; // Hide if already active if (card.classList.contains("active")) { card.innerHTML = ""; card.classList.remove("active"); return; } // 🔥 Apply card styling directly from JS Object.assign(card.style, { display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(80px, 1fr))", gap: "10px", padding: "10px", border: "2px solid #ccc", borderRadius: "10px", marginTop: "12px", maxWidth: "100%", backgroundColor: "#f5f5f5", }); card.innerHTML = ""; items[item].forEach((imgSrc) => { const img = document.createElement("img"); img.src = imgSrc; img.alt = `${item} option`; // 🧠 Apply image styles Object.assign(img.style, { width: "100%", maxWidth: "80px", height: "80px", objectFit: "cover", borderRadius: "8px", cursor: "pointer", transition: "transform 0.2s, border 0.2s", border: "2px solid transparent", }); img.addEventListener("mouseenter", () => { img.style.transform = "scale(1.1)"; img.style.border = "2px solid #48c6ef"; }); img.addEventListener("mouseleave", () => { img.style.transform = "scale(1)"; img.style.border = "2px solid transparent"; }); img.addEventListener("click", () => { mainImage.src = imgSrc; }); card.appendChild(img); }); card.classList.add("active"); }); }); });