lokesh341 commited on
Commit
1207432
·
verified ·
1 Parent(s): 14f5506

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +251 -291
templates/index.html CHANGED
@@ -275,32 +275,6 @@
275
  background-color: #c82333;
276
  }
277
 
278
- .cart-item .ingredient-info-input {
279
- padding: 8px;
280
- border: 1px solid #b3d7ff;
281
- border-radius: 6px;
282
- font-size: 13px;
283
- width: 150px;
284
- outline: none;
285
- margin: 5px;
286
- }
287
-
288
- .submit-cart {
289
- margin: 10px 0;
290
- }
291
-
292
- .submit-cart input {
293
- padding: 8px;
294
- border: 1px solid #b3d7ff;
295
- border-radius: 6px;
296
- font-size: 14px;
297
- margin-right: 10px;
298
- }
299
-
300
- .quantity-input {
301
- width: 60px;
302
- }
303
-
304
  /* Responsive Design */
305
  @media (max-width: 480px) {
306
  .chat-container {
@@ -377,31 +351,6 @@
377
  width: 25px;
378
  height: 25px;
379
  }
380
-
381
- .cart-item .ingredient-info-input {
382
- width: 120px;
383
- font-size: 12px;
384
- }
385
-
386
- .submit-cart input {
387
- width: 120px;
388
- font-size: 13px;
389
- padding: 6px;
390
- }
391
-
392
- .quantity-input {
393
- width: 50px;
394
- }
395
-
396
- .submit-button {
397
- font-size: 13px;
398
- padding: 8px 15px;
399
- }
400
-
401
- .cart-item .remove-button {
402
- font-size: 11px;
403
- padding: 4px 8px;
404
- }
405
  }
406
 
407
  @media (min-width: 481px) and (max-width: 768px) {
@@ -444,8 +393,6 @@
444
  let conversation = [
445
  { role: 'bot', message: "Hi there! I'm Chef Bot! May I know your name?" }
446
  ];
447
- let userName = '';
448
- let selectedPreference = '';
449
  let selectedIngredients = [];
450
  let selectedMenuItem = null;
451
  let cart = [];
@@ -477,30 +424,38 @@
477
  userInput.value = '';
478
  setTimeout(() => handleResponse(message), 500);
479
  } else {
480
- addMessage('bot', 'Please type something! 😄');
481
  console.warn('Empty message!');
482
  }
483
- updateCartDisplay();
484
  }
485
 
486
  function handleResponse(userInput) {
487
- const lastMessage = userInput.toLowerCase();
488
  let botResponse = '';
489
  let options = [];
490
 
491
- if (!userName) { // Name input
492
- userName = userInput;
493
- botResponse = `Nice to meet you, ${userName}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
494
  options = [
495
  { text: 'Vegetarian', class: 'green' },
496
  { text: 'Non-Vegetarian', class: 'red' }
497
  ];
498
- } else if (!selectedPreference && (lastMessage.includes('vegetarian') || lastMessage.includes('non-vegetarian'))) {
499
- selectedPreference = lastMessage.includes('vegetarian') ? 'vegetarian' : 'non-vegetarian';
500
- conversation.push({ role: 'user', message: selectedPreference.charAt(0).toUpperCase() + selectedPreference.slice(1) });
501
- console.log(`Food preference selected: ${selectedPreference}`);
502
- botResponse = `Great choice! 🍽️ Here are some ingredients:`;
503
- fetchIngredients();
 
 
 
 
 
 
 
 
 
 
 
504
  return;
505
  } else if (lastMessage.includes('yes') && selectedMenuItem) {
506
  botResponse = 'Awesome! Let’s customize your dish. Here are some ingredients you can add:';
@@ -514,121 +469,49 @@
514
  { text: 'No', class: 'red' }
515
  ];
516
  selectedMenuItem = null;
517
- selectedIngredients = [];
518
- } else {
519
- botResponse = `Searching for "${userInput}"...`;
520
- fetchMenuItems(selectedPreference, userInput);
521
- return;
522
  }
523
 
524
  addMessage('bot', botResponse);
525
  if (options.length > 0) {
526
  displayOptions(options);
527
  }
528
- updateCartDisplay();
529
- }
530
-
531
- function fetchIngredients() {
532
- fetch('/get_ingredients', {
533
- method: 'POST',
534
- headers: { 'Content-Type': 'application/json' },
535
- body: JSON.stringify({ dietary_preference: 'both' })
536
- })
537
- .then(response => response.json())
538
- .then(data => {
539
- if (data.error) {
540
- addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
541
- } else {
542
- const ingredients = data.ingredients || [];
543
- addMessage('bot', 'Please select ingredients:');
544
- displayIngredientsList(ingredients);
545
- }
546
- })
547
- .catch(error => {
548
- addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
549
- });
550
  }
551
 
552
  function fetchCustomizationIngredients(foodPreference) {
553
  fetch('/get_ingredients', {
554
  method: 'POST',
555
- headers: { 'Content-Type': 'application/json' },
 
 
556
  body: JSON.stringify({ dietary_preference: foodPreference })
557
  })
558
- .then(response => response.json())
559
- .then(data => {
560
- if (data.error) {
561
- addMessage('bot', `Sorry, there was an error fetching customization ingredients: ${data.error}`);
562
- } else {
563
- const ingredients = data.ingredients || [];
564
- addMessage('bot', 'Select ingredients to customize your dish:');
565
- displayCustomizationIngredients(ingredients);
566
- displaySelectedIngredientsForCustomization();
567
- displayCustomizationInput();
568
- }
569
- })
570
- .catch(error => {
571
- addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
572
- });
573
- }
574
-
575
- function fetchMenuItems(dietaryPreference, searchTerm = '') {
576
- const payload = { dietary_preference: dietaryPreference };
577
- if (searchTerm) payload.search_term = searchTerm;
578
- fetch('/get_menu_items', {
579
- method: 'POST',
580
- headers: { 'Content-Type': 'application/json' },
581
- body: JSON.stringify(payload)
582
- })
583
- .then(response => response.json())
584
- .then(data => {
585
- if (data.error) {
586
- addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
587
- } else if (data.menu_items.length > 0) {
588
- addMessage('bot', `--- Found ${data.menu_items.length} item${data.menu_items.length > 1 ? 's' : ''} ---`);
589
- displayMenuItems(data.menu_items);
590
- } else {
591
- addMessage('bot', `No matches for "${searchTerm || dietaryPreference}". Try "paneer"!`);
592
- }
593
- })
594
- .catch(error => {
595
- addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
596
- });
597
- }
598
-
599
- function fetchRelatedMenuItems(ingredientName) {
600
- fetch('/get_related_menu_items', {
601
- method: 'POST',
602
- headers: { 'Content-Type': 'application/json' },
603
- body: JSON.stringify({ ingredient_name: ingredientName, dietary_preference: selectedPreference })
604
  })
605
- .then(response => response.json())
606
- .then(data => {
607
- if (data.error) {
608
- addMessage('bot', `Error: ${data.error}. Try another ingredient!`);
609
- } else if (data.menu_items.length > 0) {
610
- addMessage('bot', `--- Found ${data.menu_items.length} dish${data.menu_items.length > 1 ? 'es' : ''} with "${ingredientName}" ---`);
611
- displayMenuItems(data.menu_items);
612
- } else {
613
- addMessage('bot', `No dishes found with "${ingredientName}". Try another!`);
614
- }
615
- })
616
- .catch(error => {
617
- addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
618
- });
619
  }
620
 
621
- function displayIngredientsList(ingredients) {
622
  const chatMessages = document.getElementById('chatMessages');
623
  if (!chatMessages) {
624
- console.error('Chat messages container not found for ingredients!');
625
  return;
626
  }
627
 
628
- let ingredientsList = document.querySelector('.ingredients-list');
629
  if (!ingredientsList) {
630
  ingredientsList = document.createElement('div');
631
- ingredientsList.className = 'ingredients-list';
632
  chatMessages.appendChild(ingredientsList);
633
  } else {
634
  ingredientsList.innerHTML = '';
@@ -643,10 +526,12 @@
643
  const name = document.createElement('div');
644
  name.textContent = ingredient.name;
645
  const button = document.createElement('button');
646
- button.textContent = 'Select';
647
  button.onclick = () => {
648
- addMessage('bot', `Finding dishes with "${ingredient.name}"...`);
649
- fetchRelatedMenuItems(ingredient.name);
 
 
650
  };
651
  item.appendChild(img);
652
  item.appendChild(name);
@@ -655,17 +540,101 @@
655
  });
656
  }
657
 
658
- function displayCustomizationIngredients(ingredients) {
659
  const chatMessages = document.getElementById('chatMessages');
660
  if (!chatMessages) {
661
- console.error('Chat messages container not found for customization ingredients!');
662
  return;
663
  }
664
 
665
- let ingredientsList = document.querySelector('.customization-ingredients-list');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
666
  if (!ingredientsList) {
667
  ingredientsList = document.createElement('div');
668
- ingredientsList.className = 'customization-ingredients-list';
669
  chatMessages.appendChild(ingredientsList);
670
  } else {
671
  ingredientsList.innerHTML = '';
@@ -684,7 +653,7 @@
684
  button.onclick = () => {
685
  if (!selectedIngredients.some(item => item.name === ingredient.name)) {
686
  selectedIngredients.push(ingredient);
687
- displaySelectedIngredientsForCustomization();
688
  }
689
  };
690
  item.appendChild(img);
@@ -694,29 +663,6 @@
694
  });
695
  }
696
 
697
- function displaySelectedIngredientsForCustomization() {
698
- const chatMessages = document.getElementById('chatMessages');
699
- if (!chatMessages) {
700
- console.error('Chat messages container not found for selected ingredients!');
701
- return;
702
- }
703
-
704
- let selectedArea = document.querySelector('.selected-ingredients');
705
- if (!selectedArea) {
706
- selectedArea = document.createElement('div');
707
- selectedArea.className = 'selected-ingredients';
708
- chatMessages.appendChild(selectedArea);
709
- } else {
710
- selectedArea.innerHTML = '';
711
- }
712
-
713
- selectedIngredients.forEach(ingredient => {
714
- const div = document.createElement('div');
715
- div.textContent = ingredient.name;
716
- selectedArea.appendChild(div);
717
- });
718
- }
719
-
720
  function displayMenuItems(menuItems) {
721
  const chatMessages = document.getElementById('chatMessages');
722
  if (!chatMessages) {
@@ -759,6 +705,76 @@
759
  });
760
  }
761
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
762
  function displayCustomizationInput() {
763
  const chatMessages = document.getElementById('chatMessages');
764
  if (!chatMessages) {
@@ -776,6 +792,10 @@
776
  submitButton.textContent = 'Submit Instructions';
777
  submitButton.onclick = () => {
778
  const instructions = textarea.value.trim();
 
 
 
 
779
  addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
780
  addMessage('bot', 'Item added to cart! Would you like to order more?');
781
  const options = [
@@ -785,7 +805,6 @@
785
  displayOptions(options);
786
  selectedMenuItem = null;
787
  selectedIngredients = [];
788
- updateCartDisplay();
789
  };
790
  customizationArea.appendChild(textarea);
791
  customizationArea.appendChild(submitButton);
@@ -794,24 +813,17 @@
794
  }
795
 
796
  function addToCart(item) {
797
- const cartItem = {
798
- name: item.name,
799
- image_url: item.image_url || '',
800
- category: item.veg_nonveg || 'Not specified',
801
- description: item.description || 'No description available',
802
- instructions: item.instructions || '',
803
- ingredients: item.ingredients || [],
804
- quantity: 1,
805
- ingredientsInfo: ''
806
- };
807
- cart.push(cartItem);
808
  console.log('Cart:', cart);
809
- updateCartDisplay();
810
  }
811
 
812
- function updateCartDisplay() {
813
  const chatMessages = document.getElementById('chatMessages');
814
- if (!chatMessages) return;
 
 
 
815
 
816
  let cartArea = document.querySelector('.cart-items');
817
  if (!cartArea) {
@@ -830,116 +842,61 @@
830
  cart.forEach((item, index) => {
831
  const itemDiv = document.createElement('div');
832
  itemDiv.className = 'cart-item';
833
-
834
  const img = document.createElement('img');
835
  img.src = item.image_url || 'https://via.placeholder.com/30';
836
  img.alt = item.name;
837
- itemDiv.appendChild(img);
838
-
839
- const contentDiv = document.createElement('div');
840
- const itemText = item.instructions
841
- ? `${item.name} (${item.instructions})`
842
- : item.name;
843
- contentDiv.textContent = `${itemText} (Qty: ${item.quantity})`;
844
- if (item.ingredients.length > 0) {
845
- contentDiv.textContent += ` with ${item.ingredients.map(i => i.name).join(', ')}`;
846
- }
847
- itemDiv.appendChild(contentDiv);
848
-
849
- const infoInput = document.createElement('input');
850
- infoInput.type = 'text';
851
- infoInput.placeholder = 'Add ingredient details...';
852
- infoInput.className = 'ingredient-info-input';
853
- infoInput.value = item.ingredientsInfo || '';
854
- infoInput.addEventListener('input', (e) => {
855
- cart[index].ingredientsInfo = e.target.value;
856
- });
857
- itemDiv.appendChild(infoInput);
858
-
859
  const removeButton = document.createElement('button');
860
- removeButton.textContent = 'X';
861
  removeButton.className = 'remove-button';
 
862
  removeButton.onclick = () => {
863
  cart.splice(index, 1);
864
- addMessage('bot', `Removed "${item.name}" from cart.`);
865
- updateCartDisplay();
866
  };
 
 
867
  itemDiv.appendChild(removeButton);
868
-
869
  cartArea.appendChild(itemDiv);
870
  });
871
 
872
- let submitArea = document.querySelector('.submit-cart');
873
- if (!submitArea) {
874
- submitArea = document.createElement('div');
875
- submitArea.className = 'submit-cart';
876
- const orderNameInput = document.createElement('input');
877
- orderNameInput.type = 'text';
878
- orderNameInput.placeholder = 'Order Name';
879
- orderNameInput.className = 'order-name-input';
880
- const quantityInput = document.createElement('input');
881
- quantityInput.type = 'number';
882
- quantityInput.min = '1';
883
- quantityInput.value = '1';
884
- quantityInput.className = 'quantity-input';
885
- const submitButton = document.createElement('button');
886
- submitButton.textContent = 'Submit Cart';
887
- submitButton.className = 'submit-button';
888
- submitButton.onclick = () => {
889
- submitCart(orderNameInput.value, quantityInput.value);
890
- };
891
- submitArea.appendChild(orderNameInput);
892
- submitArea.appendChild(quantityInput);
893
- submitArea.appendChild(submitButton);
894
- chatMessages.appendChild(submitArea);
895
  }
896
  }
897
-
898
- chatMessages.scrollTop = chatMessages.scrollHeight;
899
  }
900
 
901
- function submitCart(customOrderName, quantity) {
902
  if (cart.length === 0) {
903
- addMessage('bot', 'No items in cart! Add some dishes! 😊');
904
  return;
905
  }
906
 
907
- if (confirm(`Submit ${cart.length} items (Qty: ${quantity}) as- const itemsToSubmit = cart.map(item => ({
908
- name: item.name,
909
- category: item.category,
910
- description: item.description,
911
- image_url: item.image_url,
912
- quantity: parseInt(quantity) || 1,
913
- instructions: item.instructions,
914
- ingredients: item.ingredients,
915
- ingredientsInfo: item.ingredientsInfo
916
- }));
917
-
918
  fetch('/submit_ingredients', {
919
  method: 'POST',
920
- headers: { 'Content-Type': 'application/json' },
921
- body: JSON.stringify({
922
- items: itemsToSubmit,
923
- custom_order_name: customOrderName,
924
- ingredients_info: ''
925
- })
926
  })
927
- .then(response => response.json())
928
- .then(data => {
929
- if (data.error) {
930
- addMessage('bot', `Submission failed: ${data.error}. Try again?`);
931
- } else {
932
- addMessage('bot', `Submitted ${data.ingredient_name}! What's next?`);
933
- cart = [];
934
- selectedIngredients = [];
935
- selectedMenuItem = null;
936
- updateCartDisplay();
937
- }
938
- })
939
- .catch(error => {
940
- addMessage('bot', `Submission error: ${error.message}. Retrying...`);
941
- setTimeout(() => submitCart(customOrderName, quantity), 2000);
942
- });
943
  }
944
 
945
  function displayOptions(options) {
@@ -955,6 +912,8 @@
955
  button.onclick = () => {
956
  addMessage('user', opt.text);
957
  conversation.push({ role: 'user', message: opt.text });
 
 
958
  setTimeout(() => handleResponse(opt.text), 500);
959
  };
960
  chatMessages.appendChild(button);
@@ -967,10 +926,11 @@
967
  conversation.pop();
968
  selectedIngredients = [];
969
  selectedMenuItem = null;
 
 
970
  setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
971
  };
972
  chatMessages.appendChild(backButton);
973
- chatMessages.scrollTop = chatMessages.scrollHeight;
974
  }
975
 
976
  document.getElementById('userInput').addEventListener('keypress', function(e) {
 
275
  background-color: #c82333;
276
  }
277
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278
  /* Responsive Design */
279
  @media (max-width: 480px) {
280
  .chat-container {
 
351
  width: 25px;
352
  height: 25px;
353
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
354
  }
355
 
356
  @media (min-width: 481px) and (max-width: 768px) {
 
393
  let conversation = [
394
  { role: 'bot', message: "Hi there! I'm Chef Bot! May I know your name?" }
395
  ];
 
 
396
  let selectedIngredients = [];
397
  let selectedMenuItem = null;
398
  let cart = [];
 
424
  userInput.value = '';
425
  setTimeout(() => handleResponse(message), 500);
426
  } else {
 
427
  console.warn('Empty message!');
428
  }
 
429
  }
430
 
431
  function handleResponse(userInput) {
432
+ const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
433
  let botResponse = '';
434
  let options = [];
435
 
436
+ if (conversation.length === 2) { // After name input
437
+ botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
 
438
  options = [
439
  { text: 'Vegetarian', class: 'green' },
440
  { text: 'Non-Vegetarian', class: 'red' }
441
  ];
442
+ } else if (lastMessage.includes('non-vegetarian')) {
443
+ conversation.push({ role: 'user', message: 'Non-Vegetarian' });
444
+ console.log("Food preference selected: Non-Vegetarian");
445
+ botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
446
+ fetchIngredients('non-vegetarian');
447
+ return;
448
+ } else if (lastMessage.includes('vegetarian')) {
449
+ conversation.push({ role: 'user', message: 'Vegetarian' });
450
+ console.log("Food preference selected: Vegetarian");
451
+ botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
452
+ fetchIngredients('vegetarian');
453
+ return;
454
+ } else if (lastMessage.includes('chicken') || lastMessage.includes('beef') || lastMessage.includes('lamb')) {
455
+ conversation.push({ role: 'user', message: lastMessage });
456
+ console.log(`Non-veg option selected: ${lastMessage}`);
457
+ botResponse = `Great! Here are some ${lastMessage} ingredients available:`;
458
+ fetchIngredients(lastMessage.toLowerCase());
459
  return;
460
  } else if (lastMessage.includes('yes') && selectedMenuItem) {
461
  botResponse = 'Awesome! Let’s customize your dish. Here are some ingredients you can add:';
 
469
  { text: 'No', class: 'red' }
470
  ];
471
  selectedMenuItem = null;
 
 
 
 
 
472
  }
473
 
474
  addMessage('bot', botResponse);
475
  if (options.length > 0) {
476
  displayOptions(options);
477
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478
  }
479
 
480
  function fetchCustomizationIngredients(foodPreference) {
481
  fetch('/get_ingredients', {
482
  method: 'POST',
483
+ headers: {
484
+ 'Content-Type': 'application/json',
485
+ },
486
  body: JSON.stringify({ dietary_preference: foodPreference })
487
  })
488
+ .then(response => response.json())
489
+ .then(data => {
490
+ if (data.error) {
491
+ addMessage('bot', `Sorry, there was an error fetching customization ingredients: ${data.error}`);
492
+ } else {
493
+ const ingredients = data.ingredients || [];
494
+ addMessage('bot', 'Select ingredients to customize your dish:');
495
+ displayCustomizationIngredients(ingredients);
496
+ displaySelectedIngredientsForCustomization();
497
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
498
  })
499
+ .catch(error => {
500
+ addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
501
+ });
 
 
 
 
 
 
 
 
 
 
 
502
  }
503
 
504
+ function displayCustomizationIngredients(ingredients) {
505
  const chatMessages = document.getElementById('chatMessages');
506
  if (!chatMessages) {
507
+ console.error('Chat messages container not found for customization ingredients!');
508
  return;
509
  }
510
 
511
+ let ingredientsList = document.querySelector('.customization-ingredients-list');
512
  if (!ingredientsList) {
513
  ingredientsList = document.createElement('div');
514
+ ingredientsList.className = 'customization-ingredients-list';
515
  chatMessages.appendChild(ingredientsList);
516
  } else {
517
  ingredientsList.innerHTML = '';
 
526
  const name = document.createElement('div');
527
  name.textContent = ingredient.name;
528
  const button = document.createElement('button');
529
+ button.textContent = 'Add';
530
  button.onclick = () => {
531
+ if (!selectedIngredients.some(item => item.name === ingredient.name)) {
532
+ selectedIngredients.push(ingredient);
533
+ displaySelectedIngredientsForCustomization();
534
+ }
535
  };
536
  item.appendChild(img);
537
  item.appendChild(name);
 
540
  });
541
  }
542
 
543
+ function displaySelectedIngredientsForCustomization() {
544
  const chatMessages = document.getElementById('chatMessages');
545
  if (!chatMessages) {
546
+ console.error('Chat messages container not found for selected ingredients!');
547
  return;
548
  }
549
 
550
+ let selectedArea = document.querySelector('.selected-ingredients');
551
+ if (!selectedArea) {
552
+ selectedArea = document.createElement('div');
553
+ selectedArea.className = 'selected-ingredients';
554
+ chatMessages.appendChild(selectedArea);
555
+ } else {
556
+ selectedArea.innerHTML = '';
557
+ }
558
+
559
+ selectedIngredients.forEach(ingredient => {
560
+ const div = document.createElement('div');
561
+ div.textContent = ingredient.name;
562
+ const removeButton = document.createElement('button');
563
+ removeButton.textContent = 'X';
564
+ removeButton.style.marginLeft = '5px';
565
+ removeButton.style.padding = '2px 5px';
566
+ removeButton.style.backgroundColor = '#dc3545';
567
+ removeButton.style.color = '#ffffff';
568
+ removeButton.style.border = 'none';
569
+ removeButton.style.borderRadius = '4px';
570
+ removeButton.style.cursor = 'pointer';
571
+ removeButton.onclick = () => {
572
+ selectedIngredients = selectedIngredients.filter(item => item.name !== ingredient.name);
573
+ displaySelectedIngredientsForCustomization();
574
+ };
575
+ div.appendChild(removeButton);
576
+ selectedArea.appendChild(div);
577
+ });
578
+ }
579
+
580
+ function fetchIngredients(foodPreference) {
581
+ fetch('/get_ingredients', {
582
+ method: 'POST',
583
+ headers: {
584
+ 'Content-Type': 'application/json',
585
+ },
586
+ body: JSON.stringify({ dietary_preference: foodPreference })
587
+ })
588
+ .then(response => response.json())
589
+ .then(data => {
590
+ if (data.error) {
591
+ addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
592
+ } else {
593
+ const ingredients = data.ingredients || [];
594
+ addMessage('bot', 'Please select ingredients:');
595
+ displayIngredientsList(ingredients);
596
+ displaySelectedIngredients();
597
+ }
598
+ })
599
+ .catch(error => {
600
+ addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
601
+ });
602
+ }
603
+
604
+ function fetchMenuItems(ingredientNames) {
605
+ fetch('/get_menu_items', {
606
+ method: 'POST',
607
+ headers: {
608
+ 'Content-Type': 'application/json',
609
+ },
610
+ body: JSON.stringify({ ingredient_names: ingredientNames })
611
+ })
612
+ .then(response => response.json())
613
+ .then(data => {
614
+ if (data.error) {
615
+ addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
616
+ } else {
617
+ const menuItems = data.menu_items || [];
618
+ addMessage('bot', 'Here are some dishes based on your selection:');
619
+ displayMenuItems(menuItems);
620
+ }
621
+ })
622
+ .catch(error => {
623
+ addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
624
+ });
625
+ }
626
+
627
+ function displayIngredientsList(ingredients) {
628
+ const chatMessages = document.getElementById('chatMessages');
629
+ if (!chatMessages) {
630
+ console.error('Chat messages container not found for ingredients!');
631
+ return;
632
+ }
633
+
634
+ let ingredientsList = document.querySelector('.ingredients-list');
635
  if (!ingredientsList) {
636
  ingredientsList = document.createElement('div');
637
+ ingredientsList.className = 'ingredients-list';
638
  chatMessages.appendChild(ingredientsList);
639
  } else {
640
  ingredientsList.innerHTML = '';
 
653
  button.onclick = () => {
654
  if (!selectedIngredients.some(item => item.name === ingredient.name)) {
655
  selectedIngredients.push(ingredient);
656
+ displaySelectedIngredients();
657
  }
658
  };
659
  item.appendChild(img);
 
663
  });
664
  }
665
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
666
  function displayMenuItems(menuItems) {
667
  const chatMessages = document.getElementById('chatMessages');
668
  if (!chatMessages) {
 
705
  });
706
  }
707
 
708
+ function displaySelectedIngredients() {
709
+ const chatMessages = document.getElementById('chatMessages');
710
+ if (!chatMessages) {
711
+ console.error('Chat messages container not found for selected ingredients!');
712
+ return;
713
+ }
714
+
715
+ let selectedArea = document.querySelector('.selected-ingredients');
716
+ if (!selectedArea) {
717
+ selectedArea = document.createElement('div');
718
+ selectedArea.className = 'selected-ingredients';
719
+ chatMessages.appendChild(selectedArea);
720
+ } else {
721
+ selectedArea.innerHTML = '';
722
+ }
723
+
724
+ selectedIngredients.forEach(ingredient => {
725
+ const div = document.createElement('div');
726
+ div.textContent = ingredient.name;
727
+ const removeButton = document.createElement('button');
728
+ removeButton.textContent = 'X';
729
+ removeButton.style.marginLeft = '5px';
730
+ removeButton.style.padding = '2px 5px';
731
+ removeButton.style.backgroundColor = '#dc3545';
732
+ removeButton.style.color = '#ffffff';
733
+ removeButton.style.border = 'none';
734
+ removeButton.style.borderRadius = '4px';
735
+ removeButton.style.cursor = 'pointer';
736
+ removeButton.onclick = () => {
737
+ selectedIngredients = selectedIngredients.filter(item => item.name !== ingredient.name);
738
+ displaySelectedIngredients();
739
+ };
740
+ div.appendChild(removeButton);
741
+ selectedArea.appendChild(div);
742
+ });
743
+
744
+ if (selectedIngredients.length > 0) {
745
+ let submitButton = document.querySelector('.submit-button');
746
+ if (!submitButton) {
747
+ submitButton = document.createElement('button');
748
+ submitButton.className = 'submit-button';
749
+ submitButton.textContent = 'Submit Ingredients';
750
+ submitButton.onclick = submitIngredients;
751
+ chatMessages.appendChild(submitButton);
752
+ }
753
+ }
754
+ }
755
+
756
+ function submitIngredients() {
757
+ fetch('/submit_ingredients', {
758
+ method: 'POST',
759
+ headers: {
760
+ 'Content-Type': 'application/json',
761
+ },
762
+ body: JSON.stringify({ ingredients: selectedIngredients })
763
+ })
764
+ .then(response => response.json())
765
+ .then(data => {
766
+ if (data.success) {
767
+ const ingredientNames = selectedIngredients.map(ingredient => ingredient.name.toLowerCase()).join(' ');
768
+ fetchMenuItems(ingredientNames);
769
+ } else {
770
+ addMessage('bot', 'There was an issue submitting your ingredients. Please try again.');
771
+ }
772
+ })
773
+ .catch(error => {
774
+ addMessage('bot', `Error: ${error.message}`);
775
+ });
776
+ }
777
+
778
  function displayCustomizationInput() {
779
  const chatMessages = document.getElementById('chatMessages');
780
  if (!chatMessages) {
 
792
  submitButton.textContent = 'Submit Instructions';
793
  submitButton.onclick = () => {
794
  const instructions = textarea.value.trim();
795
+ if (instructions) {
796
+ addMessage('user', instructions);
797
+ conversation.push({ role: 'user', message: instructions });
798
+ }
799
  addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
800
  addMessage('bot', 'Item added to cart! Would you like to order more?');
801
  const options = [
 
805
  displayOptions(options);
806
  selectedMenuItem = null;
807
  selectedIngredients = [];
 
808
  };
809
  customizationArea.appendChild(textarea);
810
  customizationArea.appendChild(submitButton);
 
813
  }
814
 
815
  function addToCart(item) {
816
+ cart.push(item);
 
 
 
 
 
 
 
 
 
 
817
  console.log('Cart:', cart);
818
+ displayCart();
819
  }
820
 
821
+ function displayCart() {
822
  const chatMessages = document.getElementById('chatMessages');
823
+ if (!chatMessages) {
824
+ console.error('Chat messages container not found for cart!');
825
+ return;
826
+ }
827
 
828
  let cartArea = document.querySelector('.cart-items');
829
  if (!cartArea) {
 
842
  cart.forEach((item, index) => {
843
  const itemDiv = document.createElement('div');
844
  itemDiv.className = 'cart-item';
 
845
  const img = document.createElement('img');
846
  img.src = item.image_url || 'https://via.placeholder.com/30';
847
  img.alt = item.name;
848
+ const name = document.createElement('div');
849
+ name.textContent = item.name + (item.instructions ? ` (${item.instructions})` : '');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
850
  const removeButton = document.createElement('button');
 
851
  removeButton.className = 'remove-button';
852
+ removeButton.textContent = 'X';
853
  removeButton.onclick = () => {
854
  cart.splice(index, 1);
855
+ displayCart();
 
856
  };
857
+ itemDiv.appendChild(img);
858
+ itemDiv.appendChild(name);
859
  itemDiv.appendChild(removeButton);
 
860
  cartArea.appendChild(itemDiv);
861
  });
862
 
863
+ let submitCartButton = document.querySelector('.submit-cart-button');
864
+ if (!submitCartButton) {
865
+ submitCartButton = document.createElement('button');
866
+ submitCartButton.className = 'submit-cart-button';
867
+ submitCartButton.textContent = 'Submit Cart';
868
+ submitCartButton.onclick = submitCart;
869
+ cartArea.appendChild(submitCartButton);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
870
  }
871
  }
 
 
872
  }
873
 
874
+ function submitCart() {
875
  if (cart.length === 0) {
876
+ addMessage('bot', 'Your cart is empty!');
877
  return;
878
  }
879
 
 
 
 
 
 
 
 
 
 
 
 
880
  fetch('/submit_ingredients', {
881
  method: 'POST',
882
+ headers: {
883
+ 'Content-Type': 'application/json',
884
+ },
885
+ body: JSON.stringify({ items: cart })
 
 
886
  })
887
+ .then(response => response.json())
888
+ .then(data => {
889
+ if (data.success) {
890
+ addMessage('bot', 'Cart submitted successfully!');
891
+ cart = [];
892
+ displayCart();
893
+ } else {
894
+ addMessage('bot', `Error submitting cart: ${data.error}`);
895
+ }
896
+ })
897
+ .catch(error => {
898
+ addMessage('bot', `Error submitting cart: ${error.message}`);
899
+ });
 
 
 
900
  }
901
 
902
  function displayOptions(options) {
 
912
  button.onclick = () => {
913
  addMessage('user', opt.text);
914
  conversation.push({ role: 'user', message: opt.text });
915
+ chatMessages.innerHTML = '';
916
+ conversation.forEach(msg => addMessage(msg.role, msg.message));
917
  setTimeout(() => handleResponse(opt.text), 500);
918
  };
919
  chatMessages.appendChild(button);
 
926
  conversation.pop();
927
  selectedIngredients = [];
928
  selectedMenuItem = null;
929
+ chatMessages.innerHTML = '';
930
+ conversation.forEach(msg => addMessage(msg.role, msg.message));
931
  setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
932
  };
933
  chatMessages.appendChild(backButton);
 
934
  }
935
 
936
  document.getElementById('userInput').addEventListener('keypress', function(e) {