Subbu1304 commited on
Commit
d22d613
·
verified ·
1 Parent(s): 40014c1

Update templates/menu.html

Browse files
Files changed (1) hide show
  1. templates/menu.html +254 -3
templates/menu.html CHANGED
@@ -619,8 +619,8 @@ form-check-input addon-option{
619
  </a>
620
  </div>
621
 
622
- <!-- Modal for Item Details -->
623
- <div class="modal fade" id="itemModal" tabindex="-1" aria-labelledby="itemModalLabel" aria-hidden="true">
624
  <div class="modal-dialog modal-dialog-centered">
625
  <div class="modal-content">
626
  <div class="modal-header">
@@ -660,4 +660,255 @@ form-check-input addon-option{
660
  </div>
661
  </div>
662
  </div>
663
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
619
  </a>
620
  </div>
621
 
622
+ <!-- Modal for Item Details -->
623
+ <div class="modal fade" id="itemModal" tabindex="-1" aria-labelledby="itemModalLabel" aria-hidden="true">
624
  <div class="modal-dialog modal-dialog-centered">
625
  <div class="modal-content">
626
  <div class="modal-header">
 
660
  </div>
661
  </div>
662
  </div>
663
+ </div>
664
+
665
+ <!-- JavaScript -->
666
+ <script>
667
+ // Show item details and fetch customization options
668
+ function showItemDetails(name, price, image, description, section, selectedCategory) {
669
+ document.getElementById('modal-name').innerText = name;
670
+ document.getElementById('modal-price').innerText = `$${price}`;
671
+ document.getElementById('modal-img').src = image || '/static/placeholder.jpg';
672
+ document.getElementById('modal-description').innerText = description || 'No description available.';
673
+ document.getElementById('addons-list').innerHTML = 'Loading customization options...';
674
+ document.getElementById('modal-instructions').value = '';
675
+
676
+ const modalSectionEl = document.getElementById('modal-section');
677
+ modalSectionEl.setAttribute('data-section', section);
678
+ modalSectionEl.setAttribute('data-category', selectedCategory);
679
+
680
+ // Set the default quantity to 1
681
+ document.getElementById('quantityInput').value = 1;
682
+
683
+ // Fetch customization options based on the section
684
+ fetch(`/api/addons?item_name=${encodeURIComponent(name)}&item_section=${encodeURIComponent(section)}`)
685
+ .then(response => response.json())
686
+ .then(data => {
687
+ const addonsList = document.getElementById('addons-list');
688
+ addonsList.innerHTML = ''; // Clear previous content
689
+
690
+ if (!data.success || !data.addons || data.addons.length === 0) {
691
+ addonsList.innerHTML = '<p>No customization options available.</p>';
692
+ return;
693
+ }
694
+
695
+ // Display customization options inside styled divs
696
+ data.addons.forEach(addon => {
697
+ const sectionDiv = document.createElement('div');
698
+ sectionDiv.classList.add('addon-section'); // Add styling class
699
+
700
+ // Add section title
701
+ const title = document.createElement('h6');
702
+ title.innerText = addon.name;
703
+ sectionDiv.appendChild(title);
704
+
705
+ // Create options list
706
+ const optionsContainer = document.createElement('div');
707
+ addon.options.forEach((option, index) => {
708
+ const optionId = `addon-${addon.name.replace(/\s+/g, '')}-${index}`;
709
+ const listItem = document.createElement('div');
710
+ listItem.classList.add('form-check');
711
+ listItem.innerHTML = `
712
+ <input type="checkbox" class="form-check-input addon-option" id="${optionId}" value="${option}"
713
+ data-name="${option}" data-group="${addon.name}" data-price="${addon.extra_charge ? addon.extra_charge_amount : 0}">
714
+ <label class="form-check-label" for="${optionId}">
715
+ ${option} ${addon.extra_charge ? `($${addon.extra_charge_amount})` : ''}
716
+ </label>
717
+ `;
718
+ optionsContainer.appendChild(listItem);
719
+ });
720
+ sectionDiv.appendChild(optionsContainer);
721
+ addonsList.appendChild(sectionDiv);
722
+ });
723
+ })
724
+ .catch(err => {
725
+ console.error('Error fetching add-ons:', err);
726
+ document.getElementById('addons-list').innerHTML = '<p>Error loading customization options.</p>';
727
+ });
728
+ }
729
+
730
+ // Handle single-select/deselect logic for checkbox groups in all modals
731
+ document.addEventListener('click', function(event) {
732
+ if (event.target.classList.contains('addon-option')) {
733
+ handleAddonClick(event.target);
734
+ }
735
+ });
736
+
737
+ // Handle checkbox selection logic
738
+ function handleAddonClick(checkbox) {
739
+ const groupName = checkbox.getAttribute('data-group');
740
+ const isMultiSelectGroup = ["Extra Toppings", "Choose Raita/Sides","Select Dip/Sauce","Extra Add-ons","Make it a Combo"].includes(groupName);
741
+
742
+ // If it's not multi-select, uncheck all other checkboxes in the same group
743
+ if (!isMultiSelectGroup) {
744
+ const checkboxes = document.querySelectorAll(`.addon-option[data-group="${groupName}"]`);
745
+ checkboxes.forEach(otherCheckbox => {
746
+ if (otherCheckbox !== checkbox) {
747
+ otherCheckbox.checked = false;
748
+ }
749
+ });
750
+ }
751
+ }
752
+ function filterMenu() {
753
+ let input = document.getElementById('searchBar').value.toLowerCase(); // Get the value from search bar
754
+ let sections = document.querySelectorAll('h3'); // Select section headers
755
+ let items = document.querySelectorAll('.menu-card'); // Select all items
756
+ let matchedSections = new Set(); // Store matched sections
757
+
758
+ // Hide all items initially
759
+ items.forEach(item => {
760
+ let itemName = item.querySelector('.card-title').innerText.toLowerCase(); // Get item name
761
+ let itemSection = item.closest('.row').previousElementSibling.innerText.toLowerCase(); // Get section name
762
+
763
+ // If the search matches item name or section, show the item
764
+ if (itemName.includes(input) || (itemSection && itemSection.includes(input))) {
765
+ item.style.display = 'block'; // Show item if it matches search
766
+ matchedSections.add(item.closest('.row')); // Add section to matched list
767
+ } else {
768
+ item.style.display = 'none'; // Hide item if not matched
769
+ }
770
+ });
771
+
772
+ // Show or hide sections based on matched items
773
+ sections.forEach(section => {
774
+ let sectionRow = section.nextElementSibling; // The row containing items
775
+ if (matchedSections.has(sectionRow)) {
776
+ section.style.display = 'block'; // Show section header
777
+ sectionRow.style.display = 'flex'; // Show section items
778
+ } else {
779
+ section.style.display = 'none'; // Hide section header
780
+ sectionRow.style.display = 'none'; // Hide section items
781
+ }
782
+ });
783
+ }
784
+
785
+ function addToCartFromModal() {
786
+ const itemName = document.getElementById('modal-name').innerText;
787
+ let itemPrice = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
788
+
789
+ // Validate item price
790
+ if (isNaN(itemPrice)) {
791
+ alert('Invalid price for the item. Please check the item details.');
792
+ return;
793
+ }
794
+
795
+ const itemImage = document.getElementById('modal-img').src;
796
+ console.log(itemName, itemPrice, itemImage); // Log values for debugging
797
+ const modalSectionEl = document.getElementById('modal-section');
798
+ const section = modalSectionEl.getAttribute('data-section');
799
+ const selectedCategory = modalSectionEl.getAttribute('data-category');
800
+ if (!itemName || !itemPrice || !section || !itemImage) {
801
+ console.error('Missing data for cart item:', { itemName, itemPrice, section, itemImage});
802
+ return;
803
+ }
804
+
805
+ // Collect selected add-ons
806
+ let selectedAddOns = Array.from(
807
+ document.querySelectorAll('#addons-list input[type="checkbox"]:checked')
808
+ ).map(addon => ({
809
+ name: addon.getAttribute('data-name') || 'Default Name', // Fallback Name
810
+ price: parseFloat(addon.getAttribute('data-price') || 0)
811
+ }));
812
+
813
+ // Get the selected quantity
814
+ const quantity = parseInt(document.getElementById('quantityInput').value) || 1; // Default to 1 if invalid
815
+
816
+ const instructions = document.getElementById('modal-instructions').value;
817
+
818
+ // Prepare data for the cart
819
+ const cartPayload = {
820
+ itemName: itemName,
821
+ itemPrice: itemPrice,
822
+ itemImage: itemImage,
823
+ section: section,
824
+ category: selectedCategory,
825
+ addons: selectedAddOns,
826
+ instructions: instructions,
827
+ quantity: quantity // Include the quantity
828
+ };
829
+
830
+ // Send the cart data to the server
831
+ fetch('/cart/add', {
832
+ method: 'POST',
833
+ headers: {
834
+ 'Content-Type': 'application/json',
835
+ },
836
+ body: JSON.stringify(cartPayload)
837
+ })
838
+ .then(response => response.json())
839
+ .then(data => {
840
+ if (data.success) {
841
+ alert('Item added to cart successfully!');
842
+ updateCartUI(data.cart); // Update cart UI after adding an item
843
+ const modal = document.getElementById('itemModal');
844
+ const modalInstance = bootstrap.Modal.getInstance(modal);
845
+ modalInstance.hide();
846
+ } else {
847
+ alert(data.error || 'Failed to add item to cart.');
848
+ }
849
+ })
850
+ .catch(err => {
851
+ console.error('Error adding item to cart:', err);
852
+ alert('An error occurred while adding the item to the cart.');
853
+ });
854
+ }
855
+
856
+ function updateCartUI(cart) {
857
+ if (!Array.isArray(cart)) {
858
+ console.error('Invalid cart data:', cart);
859
+ return;
860
+ }
861
+ const cartIcon = document.getElementById('cart-icon');
862
+ cartIcon.innerText = cart.length; // Assuming cart is an array of items
863
+ }
864
+
865
+ function updateCartDisplay(cart) {
866
+ if (!Array.isArray(cart)) {
867
+ console.error('Invalid cart data:', cart);
868
+ return;
869
+ }
870
+ // Optional: Update quantity on the cart page
871
+ const cartCountElement = document.getElementById('cart-count');
872
+ cartCountElement.innerText = cart.reduce((total, item)=> total+item.quantity,0); // Update cart item count //Sum of all quantities
873
+
874
+ // Optionally, show a small success notification that the item was added
875
+ const successNotification = document.createElement('div');
876
+ successNotification.classList.add('success-notification');
877
+ successNotification.innerText = 'Item added to cart!';
878
+ document.body.appendChild(successNotification);
879
+ setTimeout(() => {
880
+ successNotification.remove(); // Remove success notification after a few seconds
881
+ }, 2000);
882
+ }
883
+
884
+ document.addEventListener('DOMContentLoaded', function () {
885
+ // Get references to the quantity buttons and the input field
886
+ const decreaseBtn = document.getElementById('decreaseQuantity');
887
+ const increaseBtn = document.getElementById('increaseQuantity');
888
+ const quantityInput = document.getElementById('quantityInput');
889
+
890
+ // Add event listener to decrease button
891
+ decreaseBtn.addEventListener('click', function () {
892
+ let currentQuantity = parseInt(quantityInput.value);
893
+ if (currentQuantity > 1) {
894
+ currentQuantity--;
895
+ quantityInput.value = currentQuantity;
896
+ }
897
+ });
898
+
899
+ // Add event listener to increase button
900
+ increaseBtn.addEventListener('click', function () {
901
+ let currentQuantity = parseInt(quantityInput.value);
902
+ currentQuantity++;
903
+ quantityInput.value = currentQuantity;
904
+ });
905
+ });
906
+
907
+
908
+
909
+ </script>
910
+
911
+ <!-- Bootstrap JS -->
912
+ <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
913
+ </body>
914
+ </html>