function initChatInterface() {
// Create chat container
const chatContainer = document.createElement("div");
chatContainer.id = "chat-container";
chatContainer.style.cssText = `
position: fixed;
bottom: 24px;
right: 24px;
width: 380px;
max-width: 90vw;
height: 600px;
max-height: 80vh;
background-color: #ffffff;
border-radius: 20px;
box-shadow: 0 15px 40px rgba(0,0,0,0.12);
border: 1px solid rgba(0,0,0,0.05);
z-index: 10000;
display: flex;
flex-direction: column;
font-family: 'Segoe UI', Roboto, sans-serif;
overflow: hidden;
transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
transform: translateY(20px);
opacity: 0;
`;
// Chat header
chatContainer.innerHTML = `
`;
document.body.appendChild(chatContainer);
// Floating chat button
const chatButton = document.createElement("button");
chatButton.id = "chat-button";
chatButton.innerHTML = `
`;
chatButton.style.cssText = `
position: fixed;
bottom: 24px;
right: 24px;
width: 65px;
height: 65px;
background: #1d355d;
color: white;
border: none;
border-radius: 50%;
cursor: pointer;
box-shadow: 0 8px 25px rgba(29, 53, 93, 0.3);
z-index: 10000;
display: flex;
align-items: center;
justify-content: center;
font-size: 28px;
transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
animation: float 4s ease-in-out infinite;
display: none;
`;
const style = document.createElement("style");
style.textContent = `
@keyframes float {
0% { transform: translateY(0px); }
50% { transform: translateY(-8px); }
100% { transform: translateY(0px); }
}
@keyframes popIn {
0% { transform: translateY(30px); opacity: 0; }
100% { transform: translateY(0); opacity: 1; }
}
#minimize-chat:hover, #close-chat:hover {
background: rgba(255,255,255,0.1) !important;
}
#send-button:hover {
background: #D4A017 !important;
transform: scale(1.05);
}
#chat-button:hover {
transform: scale(1.1);
background: #D4A017 !important;
}
#user-input:focus {
border-color: #1d355d !important;
background: #ffffff !important;
}
label[for="image-upload"]:hover {
color: #1d355d !important;
}
`;
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:
- Find products
- Compare prices
- Suggest alternatives
- Place orders
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() {
try {
const response = await fetch("/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: message }),
});
const data = await response.json();
hideTypingIndicator();
// Handle WhatsApp-specific responses
if (data.reply && data.reply.includes("whatsapp")) {
displayAssistantResponse(
"I've sent the order details to your WhatsApp. Please check your messages!"
);
} else {
displayAssistantResponse(data.reply);
}
// Show suggestions if available
if (data.suggestions) {
showSuggestions(data.suggestions);
}
} catch (error) {
// ... error handling ...
}
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 += `
`;
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 with proper link handling
function displayAssistantResponse(response) {
const chatBox = document.getElementById("chat-box");
let formattedResponse = "";
// Handle object responses (from tools)
if (typeof response === "object" && response !== null) {
// Add the main message text
if (response.message) {
formattedResponse += `${response.message}
`;
}
// Handle link objects
if (response.link && typeof response.link === "object") {
const linkText = response.link.text || "View Details";
const linkUrl = response.link.url || "#";
formattedResponse += `
`;
}
// Handle direct string links in response
if (response.url) {
formattedResponse += `
`;
}
}
// Handle string responses
else if (typeof response === "string") {
// Convert URLs to clickable links
formattedResponse = response.replace(
/(https?:\/\/[^\s]+)/g,
'$1'
);
}
// Handle other response types
else {
formattedResponse = String(response);
}
// Add timestamp
const now = new Date();
const timeString = now.toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
});
// Append the final formatted message to the chat box
chatBox.innerHTML += `
${formattedResponse}
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 14px;
background: white;
color: #1d355d;
border: 1px solid #e2e8f0;
border-radius: 20px;
font-size: 13px;
font-weight: 500;
cursor: pointer;
white-space: nowrap;
transition: all 0.2s;
box-shadow: 0 2px 4px rgba(0,0,0,0.02);
flex-shrink: 0;
`;
button.addEventListener("mouseenter", () => {
button.style.background = "#1d355d";
button.style.color = "white";
button.style.borderColor = "#1d355d";
});
button.addEventListener("mouseleave", () => {
button.style.background = "white";
button.style.color = "#1d355d";
button.style.borderColor = "#e2e8f0";
});
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
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");
});
});
});