Spaces:
Sleeping
Sleeping
File size: 39,122 Bytes
388c4e1 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 727f9cd 90e6c0c edc8acc a2e8285 edc8acc 90e6c0c a2e8285 90e6c0c 727f9cd 90e6c0c a2e8285 edc8acc 90e6c0c 727f9cd a2e8285 727f9cd a2e8285 edc8acc 90e6c0c a2e8285 727f9cd a2e8285 727f9cd 90e6c0c a2e8285 1ceb849 edc8acc a2e8285 edc8acc a2e8285 90e6c0c edc8acc 90e6c0c a2e8285 727f9cd 109dc1d 90e6c0c 727f9cd a2e8285 90e6c0c 109dc1d 90e6c0c a2e8285 727f9cd 109dc1d 90e6c0c 109dc1d 90e6c0c 109dc1d 90e6c0c 727f9cd a2e8285 727f9cd a2e8285 727f9cd a2e8285 90e6c0c a2e8285 727f9cd a2e8285 727f9cd a2e8285 90e6c0c a2e8285 727f9cd 90e6c0c edc8acc a2e8285 1ceb849 edc8acc 90e6c0c a2e8285 edc8acc a2e8285 edc8acc 90e6c0c a2e8285 edc8acc a2e8285 edc8acc a2e8285 edc8acc 90e6c0c a2e8285 edc8acc a2e8285 727f9cd a2e8285 a4cbba7 a2e8285 a4cbba7 a2e8285 a4cbba7 a2e8285 a4cbba7 a2e8285 a4cbba7 a2e8285 a4cbba7 a2e8285 a4cbba7 a2e8285 a4cbba7 a2e8285 a4cbba7 a2e8285 a4cbba7 a2e8285 90e6c0c a4cbba7 a2e8285 a4cbba7 a2e8285 90e6c0c a4cbba7 a2e8285 a4cbba7 a2e8285 90e6c0c a2e8285 a4cbba7 a2e8285 90e6c0c a2e8285 a4cbba7 a2e8285 90e6c0c a4cbba7 a2e8285 109dc1d a2e8285 a4cbba7 a2e8285 90e6c0c a2e8285 a4cbba7 a2e8285 90e6c0c a2e8285 90e6c0c a2e8285 90e6c0c a2e8285 727f9cd 388c4e1 727f9cd 90e6c0c 388c4e1 90e6c0c 727f9cd 388c4e1 727f9cd 03c5a0e 727f9cd 90e6c0c 727f9cd 90e6c0c 29f904e 727f9cd edc8acc 727f9cd edc8acc 727f9cd edc8acc 727f9cd edc8acc 90e6c0c 727f9cd 90e6c0c 727f9cd 109dc1d 727f9cd 94143ed 6733ef4 94143ed 6733ef4 94143ed 09b75e6 94143ed eaca368 03c5a0e 109dc1d 90e6c0c 109dc1d 90e6c0c 109dc1d edc8acc 3774e44 1207432 727f9cd edc8acc 1207432 edc8acc 727f9cd 1207432 90e6c0c 1207432 90e6c0c 727f9cd 90e6c0c edc8acc 90e6c0c 1207432 90e6c0c 1207432 109dc1d 1207432 90e6c0c 109dc1d 90e6c0c 727f9cd 109dc1d 90e6c0c 109dc1d 90e6c0c 727f9cd 109dc1d 1207432 109dc1d 1207432 109dc1d 1207432 109dc1d 1207432 109dc1d 03c5a0e 109dc1d 03c5a0e 109dc1d 1207432 109dc1d 1207432 109dc1d 1207432 109dc1d 1207432 109dc1d 1207432 109dc1d f070a0e 109dc1d 1207432 90e6c0c 1207432 90e6c0c 727f9cd 90e6c0c 1207432 727f9cd edc8acc 90e6c0c edc8acc 90e6c0c f070a0e 90e6c0c 727f9cd 1207432 109dc1d 90e6c0c 109dc1d 90e6c0c 1207432 90e6c0c 1207432 90e6c0c 1207432 90e6c0c 1207432 90e6c0c a4cbba7 90e6c0c 1207432 109dc1d 90e6c0c 1207432 90e6c0c 1207432 90e6c0c 1207432 90e6c0c 1207432 90e6c0c 727f9cd edc8acc 1207432 29f904e 1207432 7448223 109dc1d 727f9cd 109dc1d 1207432 727f9cd 109dc1d 727f9cd 90e6c0c 7c4f93e 90e6c0c 7c4f93e 90e6c0c 68917f8 90e6c0c 7c4f93e 68917f8 90e6c0c 7c4f93e 68917f8 90e6c0c 109dc1d 7c4f93e 90e6c0c 68917f8 90e6c0c 7c4f93e 90e6c0c 727f9cd 90e6c0c 727f9cd 3f242d5 388c4e1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chef Bot</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: 'Segoe UI', Arial, sans-serif;
background-color: #f4f7fa;
overflow-x: hidden;
line-height: 1.5;
}
.chat-container {
width: 100%;
max-width: 800px;
height: 100vh;
margin: 0 auto;
background-color: #ffffff;
border-radius: 12px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
display: flex;
flex-direction: column;
overflow: hidden;
}
.chat-header {
background-color: #0078d4;
color: #ffffff;
padding: 12px;
text-align: center;
font-size: 18px;
font-weight: 600;
flex-shrink: 0;
}
.chat-messages {
flex: 1;
overflow-y: auto;
padding: 15px;
scroll-behavior: smooth;
}
.bot-message, .user-message {
padding: 10px 15px;
margin: 8px 0;
border-radius: 12px;
max-width: 80%;
font-size: 14px;
line-height: 1.4;
}
.bot-message {
background-color: #e8f0fe;
color: #333;
}
.user-message {
background-color: #0078d4;
color: #ffffff;
margin-left: auto;
}
.chat-input {
display: flex;
padding: 10px;
border-top: 1px solid #e0e0e0;
background-color: #f9f9f9;
flex-shrink: 0;
}
.chat-input input {
flex: 1;
padding: 10px;
border: 1px solid #ccc;
border-radius: 8px;
font-size: 14px;
outline: none;
}
.chat-input button {
padding: 10px 20px;
margin-left: 10px;
background-color: #0078d4;
color: #ffffff;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 14px;
transition: background-color 0.2s;
}
.chat-input button:hover {
background-color: #005ea2;
}
.ingredients-list, .menu-items-list, .customization-ingredients-list {
display: flex;
flex-wrap: nowrap;
overflow-x: auto;
padding: 10px;
margin: 10px 0;
background-color: #f5f7fa;
border-radius: 10px;
gap: 15px;
scroll-behavior: smooth;
}
.ingredient-item, .menu-item {
flex: 0 0 200px;
background-color: #ffffff;
border: 1px solid #e0e0e0;
border-radius: 10px;
padding: 10px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
transition: transform 0.2s;
text-align: center;
}
.ingredient-item:hover, .menu-item:hover {
transform: translateY(-2px);
}
.ingredient-item img, .menu-item img {
width: 100%;
height: 120px;
object-fit: cover;
border-radius: 8px;
margin-bottom: 8px;
}
.ingredient-item div, .menu-item div {
font-size: 14px;
font-weight: 600;
color: #333;
margin-bottom: 5px;
}
.ingredient-item button, .menu-item button {
padding: 8px;
background-color: #28a745;
color: #ffffff;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 12px;
width: 100%;
}
.ingredient-item button:hover, .menu-item button:hover {
background-color: #218838;
}
.selected-ingredients, .selected-customization-ingredients {
background-color: #f1f8ff;
padding: 15px;
border: 1px solid #b3d7ff;
border-radius: 10px;
margin: 10px 0;
font-size: 14px;
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.selected-ingredients div, .selected-customization-ingredients div {
background-color: #d6e9ff;
padding: 5px 10px;
border-radius: 6px;
font-size: 13px;
}
.customization-input {
margin: 10px 0;
}
.customization-input textarea, .selected-customization-ingredients textarea {
width: 100%;
padding: 8px;
border: 1px solid #b3d7ff;
border-radius: 6px;
font-size: 14px;
outline: none;
margin-bottom: 10px;
}
.customization-input button, .submit-button, .submit-customization-button, .submit-cart-button {
padding: 10px 20px;
background-color: #0078d4;
color: #ffffff;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 14px;
transition: background-color 0.2s;
}
.customization-input button:hover, .submit-button:hover, .submit-customization-button:hover, .submit-cart-button:hover {
background-color: #005ea2;
}
.option-button {
padding: 10px 20px;
color: #ffffff;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 14px;
margin: 5px;
transition: background-color 0.2s;
}
.option-button.green {
background-color: #28a745;
}
.option-button.green:hover {
background-color: #218838;
}
.option-button.red {
background-color: #dc3545;
}
.option-button.red:hover {
background-color: #c82333;
}
.cart-items {
background-color: #f1f8ff;
padding: 15px;
border: 1px solid #b3d7ff;
border-radius: 10px;
margin: 10px 0;
font-size: 14px;
}
.cart-item {
display: flex;
align-items: center;
background-color: #d6e9ff;
padding: 5px 10px;
border-radius: 6px;
font-size: 13px;
margin: 5px 0;
}
.cart-item img {
width: 30px;
height: 30px;
object-fit: cover;
border-radius: 6px;
margin-right: 8px;
}
.cart-item .remove-button {
padding: 5px 10px;
margin-left: 8px;
background-color: #dc3545;
color: #ffffff;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 12px;
}
.cart-item .remove-button:hover {
background-color: #c82333;
}
/* Responsive Design */
@media (max-width: 480px) {
.chat-container {
border-radius: 0;
box-shadow: none;
}
.chat-header {
font-size: 16px;
padding: 10px;
}
.chat-messages {
padding: 10px;
}
.bot-message, .user-message {
font-size: 13px;
padding: 8px 12px;
margin: 5px 0;
}
.chat-input input {
font-size: 13px;
padding: 8px;
}
.chat-input button {
font-size: 13px;
padding: 8px 15px;
}
.ingredients-list, .menu-items-list, .customization-ingredients-list {
flex-direction: column;
gap: 10px;
padding: 8px;
}
.ingredient-item, .menu-item {
flex: 0 0 auto;
width: 100%;
max-width: 250px;
}
.ingredient-item img, .menu-item img {
height: 100px;
}
.ingredient-item div, .menu-item div {
font-size: 13px;
}
.ingredient-item button, .menu-item button {
font-size: 11px;
padding: 6px;
}
.option-button {
font-size: 13px;
padding: 8px 15px;
}
.selected-ingredients, .selected-customization-ingredients, .cart-items {
padding: 10px;
gap: 8px;
}
.cart-item {
font-size: 12px;
padding: 4px 8px;
}
.cart-item img {
width: 25px;
height: 25px;
}
}
@media (min-width: 481px) and (max-width: 768px) {
.chat-container {
max-width: 600px;
}
.chat-header {
font-size: 17px;
padding: 11px;
}
.chat-messages {
padding: 12px;
}
.ingredient-item, .menu-item {
flex: 0 0 180px;
}
.ingredient-item img, .menu-item img {
height: 110px;
}
}
</style>
</head>
<body>
<div class="chat-container">
<div class="chat-header">🍳 Chef Bot</div>
<div class="chat-messages" id="chatMessages">
<div class="bot-message">Hi there! I'm Chef Bot! May I know your name?</div>
</div>
<div class="chat-input">
<input type="text" id="userInput" placeholder="Type your name or message...">
<button onclick="sendMessage()">Send</button>
</div>
</div>
<script>
let currentStep = 'greeting'; // other possible values: 'food_type', 'select_ingredients', 'menu_display', 'customization', 'post_cart'
let conversation = [
{ role: 'bot', message: "Hi there! I'm Chef Bot! May I know your name?" }
];
let selectedIngredients = [];
let selectedMenuItem = null;
let cart = [];
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 {
console.warn('Empty message!');
}
displayCart();
}
function handleResponse(userInput) {
const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
let botResponse = '';
let options = [];
// Check for previous states and handle accordingly
if (conversation.length === 2) { // After name input
currentStep = 'food_type';
botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
options = [
{ text: 'Vegetarian', class: 'green' },
{ text: 'Non-Vegetarian', class: 'red' }
];
} else if (lastMessage.includes('non-vegetarian')) {
console.log('User selected non-vegetarian option.');
currentStep = 'select_ingredients';
botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
fetchIngredients('non-vegetarian');
return;
} else if (lastMessage.includes('vegetarian')) {
console.log('User selected vegetarian option.');
currentStep = 'select_ingredients';
botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
fetchIngredients('vegetarian');
return;
} else if (lastMessage.includes('chicken') || lastMessage.includes('beef') || lastMessage.includes('lamb')) {
currentStep = 'select_ingredients';
botResponse = `Great! Here are some ${lastMessage} ingredients available:`;
fetchIngredients(lastMessage.toLowerCase());
return;
} else if (lastMessage.includes('yes') && selectedMenuItem) {
botResponse = 'Here are some ingredients to customize your dish:';
handleYesResponse();
return;
} else if (lastMessage.includes('no') && selectedMenuItem) {
// Submit the dish without customization
submitCustomizationIngredients();
return;
} else if (lastMessage.includes('yes') && currentStep === 'post_cart') {
// After user says 'yes', ask again for food preference (veg or non-veg)
botResponse = `Let's get started again! What type of food would you prefer this time?`;
options = [
{ text: 'Vegetarian', class: 'green' },
{ text: 'Non-Vegetarian', class: 'red' }
];
currentStep = 'food_type';
addMessage('bot', botResponse);
displayOptions(options);
return;
} else if (lastMessage.includes('no') && currentStep === 'post_cart') {
addMessage('bot', 'Awesome! 🧾 Here’s your final cart:');
displayCart(); // Optional: show final cart again
addMessage('bot', 'Thank you for your order! 👨🍳🍲');
currentStep = 'end'; // Step after cart
return;
} else if (currentStep === 'customization') {
currentStep = 'menu_display'; // Reverting to menu display after customization
console.log('Step changed to menu_display.');
fetchMenuItems({ ingredient_names: ingredientNames });
return;
}
addMessage('bot', botResponse);
if (options.length > 0) {
displayOptions(options);
}
displayCart();
}
function resetToPreviousStep() {
console.log('Resetting to previous step. Current step:', currentStep);
// Based on the currentStep, go back to the previous step
if (currentStep === 'food_type') {
currentStep = 'greeting';
conversation.push({ role: 'bot', message: "Hi there! I'm Chef Bot! May I know your name?" });
console.log('Step changed to greeting. Conversation updated.');
} else if (currentStep === 'select_ingredients') {
currentStep = 'food_type';
conversation.push({ role: 'bot', message: 'What type of food would you prefer?' });
console.log('Step changed to food_type. Conversation updated.');
} else if (currentStep === 'customization') {
currentStep = 'menu_display';
console.log('Step changed to menu_display.');
//fetchMenuItems({ ingredient_names: ingredientNames });
} else if (currentStep === 'post_cart') {
currentStep = 'food_type';
conversation.push({ role: 'bot', message: 'What type of food would you prefer?' });
console.log('Step changed to food_type after post_cart. Conversation updated.');
} else {
console.error('Unexpected currentStep value:', currentStep);
}
// Ensure the bot message is updated according to the previous step
setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
}
function handleYesResponse() {
if (!selectedMenuItem) {
addMessage('bot', 'No dish selected. Please choose a dish first.');
return;
}
const botResponse = `Here is your selected dish: ${selectedMenuItem.name}`;
addMessage('bot', botResponse);
// Display selected menu item
const chatMessages = document.getElementById('chatMessages');
const menuItemDiv = document.createElement('div');
menuItemDiv.className = 'menu-item';
const img = document.createElement('img');
img.src = selectedMenuItem.image_url || 'https://via.placeholder.com/120';
img.alt = selectedMenuItem.name;
const name = document.createElement('div');
name.textContent = selectedMenuItem.name;
menuItemDiv.appendChild(img);
menuItemDiv.appendChild(name);
chatMessages.appendChild(menuItemDiv);
// Fetch ingredients for customization
fetchIngredientsForCustomization('both');
}
function fetchIngredientsForCustomization(foodPreference) {
fetch('/get_ingredients', { // Reuse existing endpoint
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ dietary_preference: foodPreference })
})
.then(response => response.json())
.then(data => {
if (data.error) {
addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
} else {
const ingredients = data.ingredients || [];
addMessage('bot', 'Please select ingredients to customize:');
displayCustomizationIngredients(ingredients);
}
})
.catch(error => {
addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
});
}
function displayCustomizationIngredients(ingredients) {
const chatMessages = document.getElementById('chatMessages');
if (!chatMessages) {
console.error('Chat messages container not found for customization ingredients!');
return;
}
let ingredientsList = document.querySelector('.customization-ingredients-list');
if (!ingredientsList) {
ingredientsList = document.createElement('div');
ingredientsList.className = 'customization-ingredients-list';
chatMessages.appendChild(ingredientsList);
} else {
ingredientsList.innerHTML = '';
}
ingredients.forEach(ingredient => {
const item = document.createElement('div');
item.className = 'ingredient-item';
const img = document.createElement('img');
img.src = ingredient.image_url || 'https://via.placeholder.com/120';
img.alt = ingredient.name;
const name = document.createElement('div');
name.textContent = ingredient.name;
const button = document.createElement('button');
button.textContent = 'Add';
button.onclick = () => {
if (!selectedIngredients.some(item => item.name === ingredient.name)) {
selectedIngredients.push({ name: ingredient.name, image_url: ingredient.image_url });
displaySelectedCustomizationIngredients();
}
};
item.appendChild(img);
item.appendChild(name);
item.appendChild(button);
ingredientsList.appendChild(item);
});
displaySelectedCustomizationIngredients();
}
function displaySelectedCustomizationIngredients() {
const chatMessages = document.getElementById('chatMessages');
if (!chatMessages) {
console.error('Chat messages container not found for selected customization ingredients!');
return;
}
let selectedArea = document.querySelector('.selected-customization-ingredients');
if (!selectedArea) {
selectedArea = document.createElement('div');
selectedArea.className = 'selected-customization-ingredients';
chatMessages.appendChild(selectedArea);
} else {
selectedArea.innerHTML = '';
}
const selectedIngredientsText = selectedIngredients.length > 0
? `${selectedMenuItem.name} with ${selectedIngredients.map(ingredient => ingredient.name).join(', ')}`
: selectedMenuItem.name;
const ingredientsDiv = document.createElement('div');
ingredientsDiv.textContent = selectedIngredientsText;
selectedArea.appendChild(ingredientsDiv);
selectedIngredients.forEach(ingredient => {
const div = document.createElement('div');
div.textContent = ingredient.name;
const removeButton = document.createElement('button');
removeButton.textContent = 'X';
removeButton.style.marginLeft = '5px';
removeButton.style.padding = '2px 5px';
removeButton.style.backgroundColor = '#dc3545';
removeButton.style.color = '#ffffff';
removeButton.style.border = 'none';
removeButton.style.borderRadius = '4px';
removeButton.style.cursor = 'pointer';
removeButton.onclick = () => {
selectedIngredients = selectedIngredients.filter(item => item.name !== ingredient.name);
displaySelectedCustomizationIngredients();
};
div.appendChild(removeButton);
selectedArea.appendChild(div);
});
if (!document.querySelector('.submit-customization-button')) {
const textarea = document.createElement('textarea');
textarea.placeholder = 'Enter any special instructions...';
selectedArea.appendChild(textarea);
const submitButton = document.createElement('button');
submitButton.className = 'submit-customization-button';
submitButton.textContent = 'Submit Customization';
submitButton.onclick = submitCustomizationIngredients;
selectedArea.appendChild(submitButton);
}
}
function submitCustomizationIngredients() {
if (!selectedMenuItem) {
addMessage('bot', 'No dish selected. Please choose a dish first.');
return;
}
const textarea = document.querySelector('.selected-customization-ingredients textarea');
const instructions = textarea ? textarea.value.trim() : '';
fetch('/submit_customization_ingredients', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
menu_item: selectedMenuItem,
ingredients: selectedIngredients,
instructions: instructions
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
currentStep = 'post_cart';
addMessage('bot', 'Customization submitted successfully! Item added to cart. Would you like to order more?');
const options = [
{ text: 'Yes', class: 'green' },
{ text: 'No', class: 'red' }
];
displayOptions(options);
selectedMenuItem = null;
selectedIngredients = [];
displayCart();
} else {
addMessage('bot', `Error: ${data.error}`);
}
})
.catch(error => {
addMessage('bot', `Error submitting customization: ${error.message}`);
});
}
function fetchIngredients(foodPreference) {
fetch('/get_ingredients', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ dietary_preference: foodPreference })
})
.then(response => response.json())
.then(data => {
if (data.error) {
addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
} else {
const ingredients = data.ingredients || [];
addMessage('bot', 'Please select ingredients:');
displayIngredientsList(ingredients);
displaySelectedIngredients();
}
})
.catch(error => {
addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
});
}
function fetchMenuItems(params) {
fetch('/get_menu_items', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(params)
})
.then(response => response.json())
.then(data => {
if (data.error) {
addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
} else {
const menuItems = data.menu_items || [];
addMessage('bot', 'Here are some dishes based on your selection:');
currentStep = 'menu_display';
displayMenuItems(menuItems);
}
})
.catch(error => {
addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
});
}
function displayIngredientsList(ingredients) {
const chatMessages = document.getElementById('chatMessages');
if (!chatMessages) {
console.error('Chat messages container not found for ingredients!');
return;
}
let ingredientsList = document.querySelector('.ingredients-list');
if (!ingredientsList) {
ingredientsList = document.createElement('div');
ingredientsList.className = 'ingredients-list';
chatMessages.appendChild(ingredientsList);
} else {
ingredientsList.innerHTML = '';
}
ingredients.forEach(ingredient => {
const item = document.createElement('div');
item.className = 'ingredient-item';
const img = document.createElement('img');
img.src = ingredient.image_url || 'https://via.placeholder.com/120';
img.alt = ingredient.name;
const name = document.createElement('div');
name.textContent = ingredient.name;
const button = document.createElement('button');
button.textContent = 'Add';
button.onclick = () => {
if (!selectedIngredients.some(item => item.name === ingredient.name)) {
selectedIngredients.push(ingredient);
displaySelectedIngredients();
}
};
item.appendChild(img);
item.appendChild(name);
item.appendChild(button);
ingredientsList.appendChild(item);
});
}
function displayMenuItems(menuItems) {
const chatMessages = document.getElementById('chatMessages');
if (!chatMessages) {
console.error('Chat messages container not found for menu items!');
return;
}
let menuItemsList = document.querySelector('.menu-items-list');
if (!menuItemsList) {
menuItemsList = document.createElement('div');
menuItemsList.className = 'menu-items-list';
chatMessages.appendChild(menuItemsList);
} else {
menuItemsList.innerHTML = '';
}
menuItems.forEach(item => {
const menuItem = document.createElement('div');
menuItem.className = 'menu-item';
const img = document.createElement('img');
img.src = item.image_url || 'https://via.placeholder.com/120';
img.alt = item.name;
const name = document.createElement('div');
name.textContent = item.name;
const button = document.createElement('button');
button.textContent = 'Add to Cart';
button.onclick = () => {
selectedMenuItem = item;
addMessage('bot', `World-class selection! Would you like to customize your dish further?`);
const options = [
{ text: 'Yes', class: 'green' },
{ text: 'No', class: 'red' }
];
currentStep = 'customization';
displayOptions(options);
};
menuItem.appendChild(img);
menuItem.appendChild(name);
menuItem.appendChild(button);
menuItemsList.appendChild(menuItem);
});
}
function displaySelectedIngredients() {
const chatMessages = document.getElementById('chatMessages');
if (!chatMessages) {
console.error('Chat messages container not found for selected ingredients!');
return;
}
let selectedArea = document.querySelector('.selected-ingredients');
if (!selectedArea) {
selectedArea = document.createElement('div');
selectedArea.className = 'selected-ingredients';
chatMessages.appendChild(selectedArea);
} else {
selectedArea.innerHTML = '';
}
selectedIngredients.forEach(ingredient => {
const div = document.createElement('div');
div.textContent = ingredient.name;
const removeButton = document.createElement('button');
removeButton.textContent = 'X';
removeButton.style.marginLeft = '5px';
removeButton.style.padding = '2px 5px';
removeButton.style.backgroundColor = '#dc3545';
removeButton.style.color = '#ffffff';
removeButton.style.border = 'none';
removeButton.style.borderRadius = '4px';
removeButton.style.cursor = 'pointer';
removeButton.onclick = () => {
selectedIngredients = selectedIngredients.filter(item => item.name !== ingredient.name);
displaySelectedIngredients();
};
div.appendChild(removeButton);
selectedArea.appendChild(div);
});
if (selectedIngredients.length > 0) {
let submitButton = document.querySelector('.submit-button');
if (!submitButton) {
submitButton = document.createElement('button');
submitButton.className = 'submit-button';
submitButton.textContent = 'Submit Ingredients';
submitButton.onclick = submitIngredients;
chatMessages.appendChild(submitButton);
}
}
}
function submitIngredients() {
if (selectedIngredients.length === 0) {
addMessage('bot', 'No ingredients selected. Please choose some ingredients.');
return;
}
const ingredientNames = selectedIngredients.map(ingredient => ingredient.name.toLowerCase()).join(' ');
fetchMenuItems({ ingredient_names: ingredientNames });
}
function addToCart(item) {
cart.push(item);
console.log('Cart:', cart);
displayCart();
}
function displayCart() {
const chatMessages = document.getElementById('chatMessages');
if (!chatMessages) {
console.error('Chat messages container not found for cart!');
return;
}
let cartArea = document.querySelector('.cart-items');
if (!cartArea) {
cartArea = document.createElement('div');
cartArea.className = 'cart-items';
chatMessages.appendChild(cartArea);
} else {
cartArea.innerHTML = '';
}
if (cart.length > 0) {
const label = document.createElement('div');
label.textContent = 'Cart:';
cartArea.appendChild(label);
cart.forEach((item, index) => {
const itemDiv = document.createElement('div');
itemDiv.className = 'cart-item';
const img = document.createElement('img');
img.src = item.image_url || 'https://via.placeholder.com/30';
img.alt = item.name;
const name = document.createElement('div');
const text = item.instructions
? `${item.name} (${item.instructions})`
: item.name;
name.textContent = item.ingredients.length > 0
? `${text} with ${item.ingredients.map(i => i.name).join(', ')}`
: text;
const removeButton = document.createElement('button');
removeButton.className = 'remove-button';
removeButton.textContent = 'X';
removeButton.onclick = () => {
cart.splice(index, 1);
displayCart();
};
itemDiv.appendChild(img);
itemDiv.appendChild(name);
itemDiv.appendChild(removeButton);
cartArea.appendChild(itemDiv);
});
let submitCartButton = document.querySelector('.submit-cart-button');
if (!submitCartButton) {
submitCartButton = document.createElement('button');
submitCartButton.className = 'submit-cart-button';
submitCartButton.textContent = 'Submit Cart';
submitCartButton.onclick = submitCart;
cartArea.appendChild(submitCartButton);
}
}
}
function submitCart() {
if (cart.length === 0) {
addMessage('bot', 'Your cart is empty!');
return;
}
fetch('/submit_customization_ingredients', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ items: cart })
})
.then(response => response.json())
.then(data => {
if (data.success) {
addMessage('bot', 'Cart submitted successfully!');
cart = [];
displayCart();
} else {
addMessage('bot', `Error submitting cart: ${data.error}`);
}
})
.catch(error => {
addMessage('bot', `Error submitting cart: ${error.message}`);
});
}
function displayOptions(options) {
const chatMessages = document.getElementById('chatMessages');
if (!chatMessages) {
console.error('Chat messages container not found for options!');
return;
}
console.log('Displaying options:', options);
options.forEach(opt => {
const button = document.createElement('button');
button.textContent = opt.text;
button.className = `option-button ${opt.class}`;
button.onclick = () => {
addMessage('user', opt.text);
conversation.push({ role: 'user', message: opt.text });
console.log(`User selected option: ${opt.text}`);
setTimeout(() => handleResponse(opt.text), 500);
};
chatMessages.appendChild(button);
});
chatMessages.appendChild(document.createElement('br'));
// Back Button Logic
const backButton = document.createElement('button');
backButton.textContent = 'Go Back';
backButton.className = 'option-button';
backButton.onclick = () => {
console.log('Back button clicked');
// Remove last user message and reset currentStep
conversation.pop();
console.log('Removed last user message, conversation length:', conversation.length);
resetToPreviousStep();
};
chatMessages.appendChild(backButton);
chatMessages.scrollTop = chatMessages.scrollHeight;
console.log('Back button added');
}
document.getElementById('userInput').addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
sendMessage();
}
});
console.log('Script loaded successfully');
</script>
<script src="{{ url_for('static', filename='script.js') }}"></script>
</body>
</html> |