-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.js
273 lines (225 loc) · 8.77 KB
/
menu.js
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
// Select all elements with the class "vegdish"
const vegDishes = document.querySelectorAll(".vegdish");
// Iterate over each vegdish element
vegDishes.forEach((vegDish) => {
const addButton = vegDish.querySelector(".add");
const subButton = vegDish.querySelector(".sub");
const mrp = vegDish.querySelector(".mrp");
const qtyCount = vegDish.querySelector(".qtycnt");
const mrporiginal = parseFloat(mrp.textContent);
// Add event listener to the add button
addButton.addEventListener("click", () => {
qtyCount.textContent++;
mrp.textContent = parseFloat(mrp.textContent) + mrporiginal;
});
// Add event listener to the sub button
subButton.addEventListener("click", () => {
if (qtyCount.textContent > 1) {
qtyCount.textContent--;
mrp.textContent = parseFloat(mrp.textContent) - mrporiginal;
}
});
});
// Select all elements with the class "vegdish"
const nonvegDishes = document.querySelectorAll(".nonvegdish");
// Iterate over each vegdish element
nonvegDishes.forEach((nonvegDish) => {
const addButton = nonvegDish.querySelector(".add");
const subButton = nonvegDish.querySelector(".sub");
const mrp = nonvegDish.querySelector(".mrp");
const qtyCount = nonvegDish.querySelector(".qtycnt");
const mrporiginal = parseFloat(mrp.textContent);
// Add event listener to the add button
addButton.addEventListener("click", () => {
qtyCount.textContent++;
mrp.textContent = parseFloat(mrp.textContent) + mrporiginal;
});
// Add event listener to the sub button
subButton.addEventListener("click", () => {
if (qtyCount.textContent > 1) {
qtyCount.textContent--;
mrp.textContent = parseFloat(mrp.textContent) - mrporiginal;
}
});
});
// Select all elements with the class "vegdish"
const italian = document.querySelectorAll(".italiandish");
// Iterate over each vegdish element
italian.forEach((italianDish) => {
const addButton = italianDish.querySelector(".add");
const subButton = italianDish.querySelector(".sub");
const mrp = italianDish.querySelector(".mrp");
const qtyCount = italianDish.querySelector(".qtycnt");
const mrporiginal = parseFloat(mrp.textContent);
// Add event listener to the add button
addButton.addEventListener("click", () => {
qtyCount.textContent++;
mrp.textContent = parseFloat(mrp.textContent) + mrporiginal;
});
// Add event listener to the sub button
subButton.addEventListener("click", () => {
if (qtyCount.textContent > 1) {
qtyCount.textContent--;
mrp.textContent = parseFloat(mrp.textContent) - mrporiginal;
}
});
});
// Select all elements with the class "vegdish"
const chinese = document.querySelectorAll(".chinesedish");
// Iterate over each vegdish element
chinese.forEach((chineseDish) => {
const addButton = chineseDish.querySelector(".add");
const subButton = chineseDish.querySelector(".sub");
const mrp = chineseDish.querySelector(".mrp");
const qtyCount = chineseDish.querySelector(".qtycnt");
const mrporiginal = parseFloat(mrp.textContent);
// Add event listener to the add button
addButton.addEventListener("click", () => {
qtyCount.textContent++;
mrp.textContent = parseFloat(mrp.textContent) + mrporiginal;
});
// Add event listener to the sub button
subButton.addEventListener("click", () => {
if (qtyCount.textContent > 1) {
qtyCount.textContent--;
mrp.textContent = parseFloat(mrp.textContent) - mrporiginal;
}
});
});
// Select all elements with the class "vegdish"
const beer = document.querySelectorAll(".beerdish");
// Iterate over each vegdish element
beer.forEach((beerDish) => {
const addButton = beerDish.querySelector(".add");
const subButton = beerDish.querySelector(".sub");
const mrp = beerDish.querySelector(".mrp");
const qtyCount = beerDish.querySelector(".qtycnt");
const mrporiginal = parseFloat(mrp.textContent);
// Add event listener to the add button
addButton.addEventListener("click", () => {
qtyCount.textContent++;
mrp.textContent = parseFloat(mrp.textContent) + mrporiginal;
});
// Add event listener to the sub button
subButton.addEventListener("click", () => {
if (qtyCount.textContent > 1) {
qtyCount.textContent--;
mrp.textContent = parseFloat(mrp.textContent) - mrporiginal;
}
});
});
// Get the order button element
const orderButton = document.querySelector(".order");
// Add click event listener to the order button
orderButton.addEventListener("click", () => {
// Get all the checked checkboxes
const checkboxes = document.querySelectorAll('input[name="dish[]"]:checked');
// Create an array to store the selected dishes
const selectedDishes = [];
// Loop through the checked checkboxes
checkboxes.forEach((checkbox) => {
// Get the parent vegdish element
const vegdish = checkbox.closest(".vegdish");
// Get the dish name
const dishName = vegdish.querySelector(".nameofdish").textContent;
// Get the quantity count
const qtyCnt = vegdish.querySelector(".qtycnt").textContent;
// Get the MRP value
const mrp = vegdish.querySelector(".mrp").textContent;
// Create an object to store the dish details
const dish = {
name: dishName,
qty: qtyCnt,
mrp: mrp,
};
// Add the dish to the selected dishes array
selectedDishes.push(dish);
});
// Log the selected dishes array
console.log(selectedDishes);
// Make an AJAX request to send the selected dishes to the PHP script
// Uncomment and customize the code below according to your server setup
/*
const xhr = new XMLHttpRequest();
xhr.open("POST", "process_order.php", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
// Response from the PHP script
const response = xhr.responseText;
console.log(response); // Log the response
}
};
// Convert the selectedDishes array to JSON string
const jsonData = JSON.stringify(selectedDishes);
// Send the JSON data in the request
xhr.send(jsonData);
*/
});
// Get the login modal and overlay elements
const modallogin = document.querySelector("#login");
const overlay = document.querySelector(".overlay");
// Function to open the login modal
const openModallog = () => {
modallogin.classList.remove("hidden");
modallogin.classList.add("modal");
overlay.classList.remove("hidden");
};
// Function to close the modal
const closemodal = () => {
modallogin.classList.add("hidden");
modallogin.classList.remove("modal");
overlay.classList.add("hidden");
};
// Add event listeners for opening and closing the modal
orderButton.addEventListener("click", openModallog);
loginbackbtn.addEventListener("click", closemodal);
overlay.addEventListener("click", closemodal);
// Function to calculate the total quantity and price
function calculateTotal() {
let totalQty = 0;
let totalPrice = 0;
// Iterate over each selected dish
for (let i = 0; i < selectedDishes.length; i++) {
const dish = selectedDishes[i];
totalQty += parseInt(dish.qty);
totalPrice += parseFloat(dish.mrp);
}
console.log("Total Quantity:", totalQty);
console.log("Total Price:", totalPrice);
}
// Add click event listener to the order button to calculate total
orderButton.addEventListener("click", calculateTotal);
// Function to generate the receipt table
function generateReceipt() {
const table = document.getElementById("mytable");
const tbody = document.createElement("tbody");
// Iterate over each selected dish
for (let i = 0; i < selectedDishes.length; i++) {
const dish = selectedDishes[i];
// Create a new row
const newRow = document.createElement("tr");
// Create <td> elements for each column and set their content
const nameCell = document.createElement("td");
nameCell.textContent = dish.name;
const qtyCell = document.createElement("td");
qtyCell.textContent = dish.qty;
const mrpCell = document.createElement("td");
mrpCell.textContent = dish.mrp;
// Append the <td> elements to the new row
newRow.appendChild(nameCell);
newRow.appendChild(qtyCell);
newRow.appendChild(mrpCell);
// Append the new row to the table body
tbody.appendChild(newRow);
}
// Remove any existing tbody element
const existingTbody = table.getElementsByTagName("tbody")[0];
if (existingTbody) {
table.removeChild(existingTbody);
}
// Append the new tbody element to the table
table.appendChild(tbody);
}
// Add click event listener to the order button to generate receipt
orderButton.addEventListener("click", generateReceipt);