let conversation = [ { role: 'bot', message: 'Hi there! I\'m Chef Bot! May I know your name?' } ]; let selectedItems = []; function addMessage(role, message) { const chatMessages = document.getElementById('chatMessages'); if (!chatMessages) { console.error('Chat messages container not found!'); return; } const messageDiv = document.createElement('div'); messageDiv.className = role === 'bot' ? 'bot-message' : 'user-message'; messageDiv.textContent = message; chatMessages.appendChild(messageDiv); chatMessages.scrollTop = chatMessages.scrollHeight; console.log(`Added ${role} message: ${message}`); } function sendMessage() { const userInput = document.getElementById('userInput'); if (!userInput) { console.error('User input field not found!'); return; } const message = userInput.value.trim(); if (message) { addMessage('user', message); conversation.push({ role: 'user', message: message }); userInput.value = ''; setTimeout(() => handleResponse(message), 500); } else { addMessage('bot', 'Please type something to continue! 😊'); } } function handleResponse(userInput) { const lowerInput = userInput.toLowerCase(); let botResponse = ''; if (conversation.length === 2) { botResponse = `Nice to meet you, ${userInput}! 😊 What type of food would you like to explore today?`; displayOptions([ { text: 'Vegetarian', class: 'green' }, { text: 'Non-Vegetarian', class: 'red' }, { text: 'Both', class: 'gray' } ]); } else if (lowerInput === 'vegetarian' || lowerInput === 'non-vegetarian' || lowerInput === 'both') { botResponse = `Awesome! Fetching some ${lowerInput} menu items for you...`; addMessage('bot', botResponse); fetchMenuItems(lowerInput); return; } else { botResponse = `Searching for "${userInput}"...`; addMessage('bot', botResponse); suggestItems(userInput); return; } addMessage('bot', botResponse); } function fetchMenuItems(dietaryPreference) { fetch('/get_menu_items', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ dietary_preference: dietaryPreference }) }) .then(response => response.json()) .then(data => { if (data.error) { addMessage('bot', `Oops! Couldn’t fetch ${dietaryPreference} items: ${data.error}. Try again?`); } else if (data.menu_items.length > 0) { addMessage('bot', `Here are some ${dietaryPreference} menu items:`); displayItemsList(data.menu_items, 'menuItemsList'); } else { addMessage('bot', `No ${dietaryPreference} items found. Try searching for something specific!`); } console.log(`Fetched menu items for ${dietaryPreference}:`, data.menu_items); }) .catch(error => { addMessage('bot', `Error connecting to Salesforce: ${error.message}. Retrying...`); setTimeout(() => fetchMenuItems(dietaryPreference), 2000); }); } function suggestItems(searchTerm) { fetch('/suggest_items', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ search_term: searchTerm }) }) .then(response => response.json()) .then(data => { if (data.error) { addMessage('bot', `Couldn’t find anything for "${searchTerm}": ${data.error}. Try another term?`); } else if (data.suggestions.length > 0) { addMessage('bot', `Found these suggestions for "${searchTerm}":`); displayItemsList(data.suggestions, 'suggestionsList'); } else { addMessage('bot', `No matches for "${searchTerm}". Try "chicken" or "paneer"?`); } console.log(`Suggestions for ${searchTerm}:`, data.suggestions); }) .catch(error => { addMessage('bot', `Error fetching suggestions: ${error.message}. Retrying...`); setTimeout(() => suggestItems(searchTerm), 2000); }); } function fetchItemDetails(itemName) { fetch('/get_item_details', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ item_name: itemName }) }) .then(response => response.json()) .then(data => { if (data.error) { addMessage('bot', `Sorry, couldn’t get details for "${itemName}": ${data.error}. Check the name or try another item!`); } else { const details = data.item_details; selectedItems.push(details); addMessage('bot', `Added "${itemName}"! Here are the details:`); displaySelectedItems(); console.log(`Details for ${itemName}:`, details); } }) .catch(error => { addMessage('bot', `Error fetching details for "${itemName}": ${error.message}. Retrying...`); setTimeout(() => fetchItemDetails(itemName), 2000); }); } function displayItemsList(items, containerId) { const container = document.getElementById(containerId); if (!container) { console.error(`${containerId} container not found!`); addMessage('bot', 'Display issue. Please try again!'); return; } container.innerHTML = ''; items.forEach(item => { const itemDiv = document.createElement('div'); itemDiv.className = containerId === 'menuItemsList' ? 'ingredient-item' : 'suggestion-item'; const img = document.createElement('img'); img.src = item.image_url || 'https://via.placeholder.com/60'; img.alt = item.name; const name = document.createElement('div'); name.textContent = item.name; name.style.textAlign = 'center'; name.style.marginTop = '5px'; name.style.fontSize = '12px'; const button = document.createElement('button'); button.textContent = 'Add'; button.onclick = () => fetchItemDetails(item.name); itemDiv.appendChild(img); itemDiv.appendChild(name); itemDiv.appendChild(button); container.appendChild(itemDiv); }); } function displaySelectedItems() { const selectedArea = document.getElementById('itemDetails'); if (!selectedArea) { console.error('Item details container not found!'); return; } selectedArea.innerHTML = ''; if (selectedItems.length === 0) { selectedArea.innerHTML = '