nagasurendra commited on
Commit
8588da2
·
verified ·
1 Parent(s): 56256a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -18
app.py CHANGED
@@ -52,6 +52,7 @@ modal_and_cart_js = """
52
  "Veg Manchurian": 12
53
  };
54
  let finalized = false;
 
55
  function openModal(name, image, description, price) {
56
  if (finalized) {
57
  alert("You cannot add more items after finalizing your order.");
@@ -60,7 +61,6 @@ modal_and_cart_js = """
60
  const modal = document.getElementById('modal');
61
  modal.style.display = 'block';
62
  modal.style.position = 'fixed';
63
-
64
  if (window.innerWidth <= 768) {
65
  // For mobile devices
66
  modal.style.width = '90%';
@@ -68,25 +68,24 @@ modal_and_cart_js = """
68
  // For laptops or larger screens
69
  modal.style.width = '30%';
70
  }
71
-
72
  modal.style.top = '15%';
73
  modal.style.left = '50%';
74
  modal.style.transform = 'translate(-50%, -50%)';
75
-
76
  document.getElementById('modal-image').src = image;
77
  document.getElementById('modal-name').innerText = name;
78
  document.getElementById('modal-description').innerText = description;
79
  document.getElementById('modal-price').innerText = price;
80
-
81
  // Reset extras
82
  const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
83
  extrasInputs.forEach(input => input.checked = false);
84
  document.getElementById('quantity').value = 1;
85
  document.getElementById('special-instructions').value = '';
86
  }
 
87
  function closeModal() {
88
  document.getElementById('modal').style.display = 'none';
89
  }
 
90
  function addToCart() {
91
  if (finalized) {
92
  alert("You cannot add more items after finalizing your order.");
@@ -99,43 +98,104 @@ modal_and_cart_js = """
99
  const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
100
  const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
101
  const itemTotal = (price + extrasCost) * quantity;
102
- const cartItem = { name, price, quantity, instructions, extras, itemTotal };
103
  cart.push(cartItem);
104
  alert(`${name} added to cart!`);
105
  updateCartDisplay();
106
  closeModal();
107
  }
 
108
  function updateCartDisplay() {
109
  let totalBill = 0;
110
- let cartHTML = "<h3>Your Cart:</h3><ul>";
111
  cart.forEach((item, index) => {
112
  totalBill += item.itemTotal;
113
- const extras = item.extras.join(', ');
114
- cartHTML += `<li><input type='checkbox' id='cart-item-${index}' value='${index}' checked onclick='removeItem(${index})'> ${item.name} (x${item.quantity}, Extras: ${extras}, Instructions: ${item.instructions}) - $${item.itemTotal.toFixed(2)}</li>`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  });
116
  cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
 
117
  document.getElementById('floating-cart').innerHTML = cartHTML;
118
  }
119
- function removeItem(index) {
120
- cart.splice(index, 1);
 
 
 
121
  updateCartDisplay();
122
  }
123
- function finalizeOrder() {
124
- if (cart.length === 0) {
125
- alert("Your cart is empty. Please add items before finalizing.");
126
- return;
 
 
 
 
 
 
 
 
127
  }
128
- finalized = true;
 
 
 
129
  let finalOrderHTML = "<h3>Final Order:</h3><ul>";
130
  let totalBill = 0;
131
  cart.forEach(item => {
132
  totalBill += item.itemTotal;
133
- const extras = item.extras.join(', ');
134
- finalOrderHTML += `<li>${item.name} (x${item.quantity}, Extras: ${extras}, Instructions: ${item.instructions}) - $${item.itemTotal.toFixed(2)}</li>`;
 
 
 
 
 
 
 
 
 
 
135
  });
136
  finalOrderHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
137
  document.getElementById('final-order').innerHTML = finalOrderHTML;
138
- alert("Your order has been finalized. You cannot make changes now.");
139
  }
140
  </script>
141
  """
 
52
  "Veg Manchurian": 12
53
  };
54
  let finalized = false;
55
+
56
  function openModal(name, image, description, price) {
57
  if (finalized) {
58
  alert("You cannot add more items after finalizing your order.");
 
61
  const modal = document.getElementById('modal');
62
  modal.style.display = 'block';
63
  modal.style.position = 'fixed';
 
64
  if (window.innerWidth <= 768) {
65
  // For mobile devices
66
  modal.style.width = '90%';
 
68
  // For laptops or larger screens
69
  modal.style.width = '30%';
70
  }
 
71
  modal.style.top = '15%';
72
  modal.style.left = '50%';
73
  modal.style.transform = 'translate(-50%, -50%)';
 
74
  document.getElementById('modal-image').src = image;
75
  document.getElementById('modal-name').innerText = name;
76
  document.getElementById('modal-description').innerText = description;
77
  document.getElementById('modal-price').innerText = price;
 
78
  // Reset extras
79
  const extrasInputs = document.querySelectorAll('input[name="biryani-extra"]');
80
  extrasInputs.forEach(input => input.checked = false);
81
  document.getElementById('quantity').value = 1;
82
  document.getElementById('special-instructions').value = '';
83
  }
84
+
85
  function closeModal() {
86
  document.getElementById('modal').style.display = 'none';
87
  }
88
+
89
  function addToCart() {
90
  if (finalized) {
91
  alert("You cannot add more items after finalizing your order.");
 
98
  const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
99
  const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
100
  const itemTotal = (price + extrasCost) * quantity;
101
+ const cartItem = { name, price, quantity, instructions, extras, itemTotal, extrasQuantities: extras.map(() => 1) };
102
  cart.push(cartItem);
103
  alert(`${name} added to cart!`);
104
  updateCartDisplay();
105
  closeModal();
106
  }
107
+
108
  function updateCartDisplay() {
109
  let totalBill = 0;
110
+ let cartHTML = "<h3>Your Cart:</h3><ul style='list-style: none; padding: 0;'>";
111
  cart.forEach((item, index) => {
112
  totalBill += item.itemTotal;
113
+ const extras = item.extras.map((extra, i) => {
114
+ const extraQuantity = item.extrasQuantities ? item.extrasQuantities[i] || 1 : 1;
115
+ const extraTotal = extrasPrices[extra] * extraQuantity;
116
+ totalBill += extraTotal;
117
+ return `<li>
118
+ <div style='display: flex; justify-content: space-between; align-items: center;'>
119
+ <span>${extra}</span>
120
+ <span>Price: $${extrasPrices[extra].toFixed(2)}</span>
121
+ <div>
122
+ <label for='extra-quantity-${index}-${i}'>Quantity:</label>
123
+ <input type='number' id='extra-quantity-${index}-${i}' value='${extraQuantity}' min='1' style='width: 50px;' onchange='updateExtraQuantity(${index}, ${i}, this.value)'>
124
+ </div>
125
+ <span>Total: $${extraTotal.toFixed(2)}</span>
126
+ <input type='checkbox' id='extra-remove-${index}-${i}' onclick='removeExtra(${index}, ${i})'> Remove
127
+ </div>
128
+ </li>`;
129
+ }).join('');
130
+
131
+ cartHTML += `<li style='border-bottom: 1px solid #ddd; margin-bottom: 10px;'>
132
+ <div style='display: flex; justify-content: space-between; align-items: center;'>
133
+ <span>${item.name}</span>
134
+ <span>Item Price: $${item.price.toFixed(2)}</span>
135
+ <div>
136
+ <label for='item-quantity-${index}'>Quantity:</label>
137
+ <input type='number' id='item-quantity-${index}' value='${item.quantity}' min='1' style='width: 50px;' onchange='updateItemQuantity(${index}, this.value)'>
138
+ </div>
139
+ <span>Total: $${(item.price * item.quantity).toFixed(2)}</span>
140
+ <input type='checkbox' id='item-remove-${index}' onclick='removeItem(${index})'> Remove
141
+ </div>
142
+ <div style='margin-left: 20px;'>
143
+ <h4>Extras:</h4>
144
+ <ul style='list-style: none; padding: 0;'>${extras}</ul>
145
+ </div>
146
+ <div style='margin-left: 20px;'>
147
+ <strong>Instructions:</strong> ${item.instructions || "None"}
148
+ </div>
149
+ </li>`;
150
  });
151
  cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
152
+ cartHTML += `<button style='margin-top: 10px; background-color: #007bff; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer;' onclick='submitCart()'>Submit</button>`;
153
  document.getElementById('floating-cart').innerHTML = cartHTML;
154
  }
155
+
156
+ function updateItemQuantity(index, newQuantity) {
157
+ const quantity = parseInt(newQuantity) || 1;
158
+ cart[index].quantity = quantity;
159
+ cart[index].itemTotal = cart[index].price * quantity;
160
  updateCartDisplay();
161
  }
162
+
163
+ function updateExtraQuantity(cartIndex, extraIndex, newQuantity) {
164
+ const quantity = parseInt(newQuantity) || 1;
165
+ cart[cartIndex].extrasQuantities = cart[cartIndex].extrasQuantities || [];
166
+ cart[cartIndex].extrasQuantities[extraIndex] = quantity;
167
+ updateCartDisplay();
168
+ }
169
+
170
+ function removeExtra(cartIndex, extraIndex) {
171
+ cart[cartIndex].extras.splice(extraIndex, 1);
172
+ if (cart[cartIndex].extrasQuantities) {
173
+ cart[cartIndex].extrasQuantities.splice(extraIndex, 1);
174
  }
175
+ updateCartDisplay();
176
+ }
177
+
178
+ function submitCart() {
179
  let finalOrderHTML = "<h3>Final Order:</h3><ul>";
180
  let totalBill = 0;
181
  cart.forEach(item => {
182
  totalBill += item.itemTotal;
183
+ const extras = item.extras.map((extra, i) => {
184
+ const extraQuantity = item.extrasQuantities ? item.extrasQuantities[i] || 1 : 1;
185
+ const extraTotal = extrasPrices[extra] * extraQuantity;
186
+ totalBill += extraTotal;
187
+ return `${extra} (x${extraQuantity}) - $${extraTotal.toFixed(2)}`;
188
+ }).join(', ');
189
+
190
+ finalOrderHTML += `<li>
191
+ ${item.name} (x${item.quantity}) - $${item.itemTotal.toFixed(2)}
192
+ <br>Extras: ${extras}
193
+ <br>Instructions: ${item.instructions || "None"}
194
+ </li>`;
195
  });
196
  finalOrderHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
197
  document.getElementById('final-order').innerHTML = finalOrderHTML;
198
+ alert("Your final order has been submitted!");
199
  }
200
  </script>
201
  """