let db; let currentChatId = null; // ⚙️ Backend API Configuration const API_BASE_URL = "http://localhost:8000"; // Change this to your backend URL // ---------- IndexedDB Setup ---------- const request = indexedDB.open("ChatDB", 1); request.onupgradeneeded = (e) => { db = e.target.result; const store = db.createObjectStore("chats", { keyPath: "id", autoIncrement: true }); store.createIndex("title", "title", { unique: false }); }; request.onsuccess = (e) => { db = e.target.result; loadChatHistory(); }; request.onerror = (e) => { console.error("IndexedDB error:", e.target.error); }; // ---------- UI Elements ---------- const chatBox = document.getElementById("chat-box"); const userInput = document.getElementById("user-input"); const sendBtn = document.getElementById("send-btn"); const newChatBtn = document.getElementById("new-chat-btn"); const chatHistory = document.getElementById("chat-history"); const chatTitle = document.getElementById("chat-title"); // ---------- Core Chat Functions ---------- function addMessage(content, sender, cards = [], save = true) { const msg = document.createElement("div"); msg.classList.add("message", sender); const bubble = document.createElement("div"); bubble.classList.add("bubble"); bubble.textContent = content; msg.appendChild(bubble); // Add property cards if they exist if (cards && cards.length > 0) { const cardsContainer = document.createElement("div"); cardsContainer.classList.add("property-cards"); cards.forEach(card => { const cardEl = createPropertyCard(card); cardsContainer.appendChild(cardEl); }); msg.appendChild(cardsContainer); } chatBox.appendChild(msg); chatBox.scrollTop = chatBox.scrollHeight; if (save && currentChatId) saveMessageToDB(content, sender, cards); } function createPropertyCard(card) { const cardEl = document.createElement("div"); cardEl.classList.add("property-card"); // Clean up the data const title = card.title || card.project_name || 'Property'; const price = card.price || 'Price on request'; const location = card.city_locality || 'Location not specified'; // Clean BHK - remove underscores and format nicely let bhk = card.bhk || ''; bhk = bhk.replace(/_/g, ' ').trim(); // Clean status - format nicely let status = card.possession_status || ''; status = status.replace(/_/g, ' ') .toLowerCase() .split(' ') .map(word => word.charAt(0).toUpperCase() + word.slice(1)) .join(' '); // Filter out placeholder amenities let amenities = card.top_amenities || []; amenities = amenities.filter(a => a && a.toLowerCase() !== 'about property' && a.toLowerCase() !== 'property' && a.trim().length > 2 ); cardEl.innerHTML = `
📍 ${location}
${bhk ? `🏠 ${bhk}
` : ''} ${status ? `🔑 ${status}
` : ''} ${amenities.length > 0 ? `