-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorder.php
More file actions
866 lines (775 loc) · 36.9 KB
/
order.php
File metadata and controls
866 lines (775 loc) · 36.9 KB
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
<?php
// order.php
include_once 'config/config.php';
session_start();
// Check if table parameter exists
if (!isset($_GET['table'])) {
header('Location: index.php');
exit();
}
$table_number = $_GET['table'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo SITE_NAME; ?> - Order</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<style>
body {
padding-top: 56px;
background-color: #f5f5f5;
}
.category-section {
margin-bottom: 2rem;
}
.menu-item {
border: none;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
margin-bottom: 20px;
transition: transform 0.2s;
}
.menu-item:hover {
transform: translateY(-5px);
}
.menu-item-unavailable {
opacity: 0.6;
}
.cart-container {
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}
.prep-time {
font-size: 0.8rem;
color: #6c757d;
}
.loading {
display: flex;
justify-content: center;
align-items: center;
min-height: 300px;
}
.mobile-input {
display: none;
}
.mobile-input.show {
display: block;
}
</style>
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
<div class="container">
<a class="navbar-brand" href="index.php"><?php echo SITE_NAME; ?></a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto">
<li class="nav-item">
<a class="nav-link" href="#" id="viewCartBtn">
<i class="fas fa-shopping-cart"></i> Cart <span class="badge bg-primary" id="cartCount">0</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" id="viewOrdersBtn">My Orders</a>
</li>
<!-- Add this to the navbar, right after the "My Orders" list item -->
<li class="nav-item">
<a class="nav-link text-danger" href="#" id="leaveTableBtn">
<i class="fas fa-sign-out-alt"></i> Leave Table
</a>
</li>
</ul>
</div>
</div>
</nav>
<!-- Main Content -->
<div class="container py-4">
<!-- Table Info -->
<div class="alert alert-info mb-4">
<h4 class="mb-0">Table: <?php echo htmlspecialchars($table_number); ?></h4>
</div>
<!-- Mobile Number Input (Initially Hidden) -->
<div class="card mb-4 mobile-input" id="mobileInputCard">
<div class="card-body">
<h5 class="card-title">Enter Your Mobile Number</h5>
<p class="card-text">We need your mobile number to track your order.</p>
<div class="mb-3">
<input type="tel" class="form-control" id="customerPhone" placeholder="Enter your mobile number" required>
</div>
<button class="btn btn-primary" id="saveMobileBtn">Continue</button>
</div>
</div>
<!-- Menu Container -->
<div id="menuContainer">
<div class="loading">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
</div>
</div>
<!-- Cart Modal -->
<div class="modal fade" id="cartModal" tabindex="-1" aria-labelledby="cartModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="cartModalLabel">Your Cart</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div id="cartContent">
<p class="text-center">Your cart is empty</p>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Continue Shopping</button>
<button type="button" class="btn btn-primary" id="checkoutBtn">Proceed to Checkout</button>
</div>
</div>
</div>
</div>
<!-- Order Modal -->
<div class="modal fade" id="orderModal" tabindex="-1" aria-labelledby="orderModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="orderModalLabel">Your Orders</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div id="orderContent">
<div class="loading">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Checkout Modal -->
<div class="modal fade" id="checkoutModal" tabindex="-1" aria-labelledby="checkoutModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="checkoutModalLabel">Checkout</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div id="paymentSummary">
<!-- Will be populated with JavaScript -->
</div>
<hr>
<h6>Payment Method</h6>
<div class="form-check">
<input class="form-check-input" type="radio" name="paymentMethod" id="methodCard" value="Card" checked>
<label class="form-check-label" for="methodCard">
Credit/Debit Card
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="paymentMethod" id="methodUPI" value="UPI">
<label class="form-check-label" for="methodUPI">
UPI
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="paymentMethod" id="methodCash" value="Cash">
<label class="form-check-label" for="methodCash">
Cash
</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" id="confirmPaymentBtn">Confirm Payment</button>
</div>
</div>
</div>
</div>
<!-- Order Success Modal -->
<div class="modal fade" id="orderSuccessModal" tabindex="-1" aria-labelledby="orderSuccessModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="orderSuccessModalLabel">Order Placed Successfully!</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="text-center mb-4">
<i class="fas fa-check-circle text-success" style="font-size: 4rem;"></i>
</div>
<p>Your order has been placed successfully. You can track your order status below.</p>
<div id="orderDetailsContainer"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-bs-dismiss="modal">Continue</button>
</div>
</div>
</div>
</div>
<!-- Leave Table Confirmation Modal -->
<div class="modal fade" id="leaveTableModal" tabindex="-1" aria-labelledby="leaveTableModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="leaveTableModalLabel">Leave Table Confirmation</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Are you sure you want to leave this table?</p>
<p class="text-warning"><i class="fas fa-exclamation-triangle"></i> This will vacate the table and end your session.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-danger" id="confirmLeaveBtn">Yes, Leave Table</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
// Global variables
const tableId = "<?php echo htmlspecialchars($table_number); ?>";
let customerPhone = localStorage.getItem('customerPhone') || '';
let cart = JSON.parse(localStorage.getItem('cart_' + tableId)) || [];
let menu = [];
let orders = [];
const CURRENCY = "<?php echo CURRENCY; ?>"; // Add this line to define the CURRENCY variable for JavaScript
const leaveTableBtn = document.getElementById('leaveTableBtn');
const leaveTableModal = new bootstrap.Modal(document.getElementById('leaveTableModal'));
const confirmLeaveBtn = document.getElementById('confirmLeaveBtn');
// DOM elements
const menuContainer = document.getElementById('menuContainer');
const cartContent = document.getElementById('cartContent');
const cartCount = document.getElementById('cartCount');
const mobileInputCard = document.getElementById('mobileInputCard');
const customerPhoneInput = document.getElementById('customerPhone');
const saveMobileBtn = document.getElementById('saveMobileBtn');
const viewCartBtn = document.getElementById('viewCartBtn');
const checkoutBtn = document.getElementById('checkoutBtn');
const viewOrdersBtn = document.getElementById('viewOrdersBtn');
const confirmPaymentBtn = document.getElementById('confirmPaymentBtn');
// Bootstrap Modals
const cartModal = new bootstrap.Modal(document.getElementById('cartModal'));
const orderModal = new bootstrap.Modal(document.getElementById('orderModal'));
const checkoutModal = new bootstrap.Modal(document.getElementById('checkoutModal'));
const orderSuccessModal = new bootstrap.Modal(document.getElementById('orderSuccessModal'));
// Event Listeners
document.addEventListener('DOMContentLoaded', init);
saveMobileBtn.addEventListener('click', saveMobileNumber);
viewCartBtn.addEventListener('click', showCart);
checkoutBtn.addEventListener('click', showCheckout);
viewOrdersBtn.addEventListener('click', showOrders);
confirmPaymentBtn.addEventListener('click', processPayment);
leaveTableBtn.addEventListener('click', showLeaveTableConfirmation);
confirmLeaveBtn.addEventListener('click', leaveTable);
// Initialize the app
function init() {
// Check if we have the phone number
if (!customerPhone) {
mobileInputCard.classList.add('show');
} else {
fetchMenu();
fetchOrders();
}
updateCartCount();
}
function saveMobileNumber() {
customerPhone = customerPhoneInput.value.trim();
if (!customerPhone) {
alert('Please enter a valid mobile number');
return;
}
localStorage.setItem('customerPhone', customerPhone);
mobileInputCard.classList.remove('show');
fetchMenu();
}
function fetchMenu() {
menuContainer.innerHTML = `
<div class="loading">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
`;
fetch('api/get_menu.php')
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
menu = data.menu;
renderMenu(menu);
} else {
menuContainer.innerHTML = `<div class="alert alert-danger">Error loading menu: ${data.message}</div>`;
}
})
.catch(error => {
menuContainer.innerHTML = `<div class="alert alert-danger">Error loading menu: ${error.message}</div>`;
});
}
function renderMenu(menuData) {
if (!menuData.length) {
menuContainer.innerHTML = '<div class="alert alert-info">No menu items available at the moment.</div>';
return;
}
let html = '';
menuData.forEach(category => {
html += `
<div class="category-section">
<h3 class="mb-3">${category.name}</h3>
<p>${category.description}</p>
<div class="row">
`;
if (category.items.length === 0) {
html += `<div class="col-12"><p>No items available in this category</p></div>`;
} else {
category.items.forEach(item => {
const isAvailable = item.is_available == 1;
// Get image URL or use default image
const imageUrl = item.image_url && item.image_url.trim() ? item.image_url : 'images/default/default-food.jpg';
html += `
<div class="col-md-6 col-lg-4">
<div class="card menu-item ${!isAvailable ? 'menu-item-unavailable' : ''}">
<img src="${imageUrl}" class="card-img-top menu-item-image" alt="${item.name}"
onerror="this.src='images/default/default-food.jpg';">
<div class="card-body">
<h5 class="card-title">${item.name}</h5>
<p class="card-text">${item.description}</p>
<p class="prep-time"><i class="far fa-clock"></i> Prep time: ${item.prep_time_minutes} min</p>
<div class="d-flex justify-content-between align-items-center">
<h6 class="mb-0">${CURRENCY}${item.price}</h6>
${isAvailable ?
`<button class="btn btn-sm btn-primary" onclick="addToCart(${item.id}, '${item.name}', ${item.price})">
Add to Cart
</button>` :
'<span class="badge bg-secondary">Unavailable</span>'
}
</div>
</div>
</div>
</div>
`;
});
}
html += `
</div>
</div>
<hr>
`;
});
menuContainer.innerHTML = html;
}
function addToCart(itemId, itemName, itemPrice) {
if (!customerPhone) {
mobileInputCard.classList.add('show');
return;
}
const existingItem = cart.find(item => item.menu_item_id === itemId);
if (existingItem) {
existingItem.quantity += 1;
} else {
cart.push({
menu_item_id: itemId,
name: itemName,
price: itemPrice,
quantity: 1,
special_instructions: ''
});
}
localStorage.setItem('cart_' + tableId, JSON.stringify(cart));
updateCartCount();
// Show a quick toast notification
const toastContainer = document.createElement('div');
toastContainer.style.position = 'fixed';
toastContainer.style.top = '20px';
toastContainer.style.right = '20px';
toastContainer.style.zIndex = '1070';
toastContainer.innerHTML = `
<div class="toast show" role="alert" aria-live="assertive" aria-atomic="true">
<div class="toast-header">
<strong class="me-auto">Cart Updated</strong>
<button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close" onclick="this.parentElement.parentElement.remove()"></button>
</div>
<div class="toast-body">
Added ${itemName} to your cart
</div>
</div>
`;
document.body.appendChild(toastContainer);
setTimeout(() => {
toastContainer.remove();
}, 3000);
}
function updateCartCount() {
const count = cart.reduce((total, item) => total + item.quantity, 0);
cartCount.textContent = count;
}
function showCart() {
if (cart.length === 0) {
cartContent.innerHTML = `<p class="text-center">Your cart is empty</p>`;
checkoutBtn.disabled = true;
} else {
let html = `
<table class="table">
<thead>
<tr>
<th>Item</th>
<th>Price</th>
<th>Quantity</th>
<th>Subtotal</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
`;
let total = 0;
cart.forEach((item, index) => {
const subtotal = item.price * item.quantity;
total += subtotal;
html += `
<tr>
<td>${item.name}</td>
<td>${CURRENCY}${item.price.toFixed(2)}</td>
<td>
<div class="input-group input-group-sm" style="width: 100px;">
<button class="btn btn-outline-secondary" type="button" onclick="updateCartItemQuantity(${index}, -1)">-</button>
<input type="text" class="form-control text-center" value="${item.quantity}" readonly>
<button class="btn btn-outline-secondary" type="button" onclick="updateCartItemQuantity(${index}, 1)">+</button>
</div>
</td>
<td>${CURRENCY}${subtotal.toFixed(2)}</td>
<td>
<button class="btn btn-sm btn-outline-danger" onclick="removeCartItem(${index})">
<i class="fas fa-trash"></i>
</button>
</td>
</tr>
<tr>
<td colspan="5">
<div class="mb-3">
<label for="special_instructions_${index}" class="form-label">Special Instructions</label>
<textarea class="form-control" id="special_instructions_${index}" rows="2"
onchange="updateSpecialInstructions(${index}, this.value)">${item.special_instructions || ''}</textarea>
</div>
</td>
</tr>
`;
});
html += `
</tbody>
<tfoot>
<tr>
<th colspan="3">Total</th>
<th>${CURRENCY}${total.toFixed(2)}</th>
<th></th>
</tr>
</tfoot>
</table>
`;
cartContent.innerHTML = html;
checkoutBtn.disabled = false;
}
cartModal.show();
}
function updateCartItemQuantity(index, change) {
cart[index].quantity += change;
// Remove item if quantity becomes 0 or negative
if (cart[index].quantity <= 0) {
cart.splice(index, 1);
}
// Update localStorage and display
localStorage.setItem('cart_' + tableId, JSON.stringify(cart));
updateCartCount();
showCart();
}
function updateSpecialInstructions(index, instructions) {
cart[index].special_instructions = instructions;
localStorage.setItem('cart_' + tableId, JSON.stringify(cart));
}
function removeCartItem(index) {
cart.splice(index, 1);
localStorage.setItem('cart_' + tableId, JSON.stringify(cart));
updateCartCount();
showCart();
}
function showCheckout() {
if (cart.length === 0) {
alert('Your cart is empty');
return;
}
// Calculate total
const total = cart.reduce((sum, item) => sum + (item.price * item.quantity), 0);
// Populate payment summary
document.getElementById('paymentSummary').innerHTML = `
<h6>Order Summary</h6>
<p>${cart.length} item(s) in cart</p>
<h5>Total: ${CURRENCY}${total.toFixed(2)}</h5>
`;
// Close cart modal and open checkout modal
cartModal.hide();
checkoutModal.show();
}
function showOrders() {
const orderContent = document.getElementById('orderContent');
// Show loading
orderContent.innerHTML = `
<div class="loading">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
`;
// Show order modal
orderModal.show();
// Fetch orders
fetchOrders();
}
function fetchOrders() {
const orderContent = document.getElementById('orderContent');
// Only fetch if phone number is available
if (!customerPhone) {
orderContent.innerHTML = `
<div class="alert alert-warning">
Please enter your mobile number to view your orders.
</div>
`;
return;
}
fetch(`api/get_orders.php?phone=${encodeURIComponent(customerPhone)}&table=${encodeURIComponent(tableId)}`)
.then(response => response.json())
.then(data => {
if (data.status === 'success' && data.orders && data.orders.length > 0) {
orders = data.orders;
renderOrders(orders);
} else {
orderContent.innerHTML = `
<div class="alert alert-info">
No orders found for this table and phone number.
</div>
`;
}
})
.catch(error => {
orderContent.innerHTML = `
<div class="alert alert-danger">
Error loading orders: ${error.message}
</div>
`;
});
}
// Add this function after the fetchOrders function:
function renderOrders(orders) {
const orderContent = document.getElementById('orderContent');
if (!orders || orders.length === 0) {
orderContent.innerHTML = `
<div class="alert alert-info">
No orders found for this table and phone number.
</div>
`;
return;
}
let html = '';
orders.forEach(order => {
// Format status with appropriate color
let statusClass = '';
switch(order.status) {
case 'Pending': statusClass = 'bg-warning text-dark'; break;
case 'Confirmed': statusClass = 'bg-info text-dark'; break;
case 'Preparing': statusClass = 'bg-primary'; break;
case 'Ready': statusClass = 'bg-success'; break;
case 'Delivered': statusClass = 'bg-success'; break;
case 'Completed': statusClass = 'bg-secondary'; break;
default: statusClass = 'bg-light text-dark';
}
html += `
<div class="card mb-3">
<div class="card-header d-flex justify-content-between">
<h6 class="mb-0">Order #${order.id}</h6>
<span class="badge ${statusClass}">${order.status}</span>
</div>
<div class="card-body">
<p><strong>Date:</strong> ${order.order_time_formatted}</p>
<p><strong>Total:</strong> ${CURRENCY}${order.total_amount.toFixed(2)}</p>
<h6>Items:</h6>
<ul class="list-group mb-3">`;
order.items.forEach(item => {
html += `
<li class="list-group-item d-flex justify-content-between align-items-center">
<div>
<span class="fw-bold">${item.quantity}x</span> ${item.item_name}
${item.special_instructions ? `<br><small class="text-muted">Note: ${item.special_instructions}</small>` : ''}
</div>
<span>${CURRENCY}${(item.item_price * item.quantity).toFixed(2)}</span>
</li>
`;
});
html += `
</ul>
${order.status === 'Preparing' ? `
<div class="alert alert-info">
<i class="fas fa-utensils me-2"></i> Your order is being prepared.
</div>
` : ''}
${order.status === 'Ready' ? `
<div class="alert alert-success">
<i class="fas fa-check-circle me-2"></i> Your order is ready! You can pick it up.
</div>
` : ''}
</div>
</div>
`;
});
orderContent.innerHTML = html;
}
// Add this function to the JavaScript section
function processPayment() {
if (cart.length === 0) {
alert('Your cart is empty');
return;
}
// Calculate total
const total = cart.reduce((sum, item) => sum + (item.price * item.quantity), 0);
// Create a variable outside the promise chain to store orderData
let savedOrderData = null;
// Get table ID from table number
fetch(`api/get_table_id.php?table_number=${encodeURIComponent(tableId)}`)
.then(response => response.json())
.then(tableData => {
if (tableData.status === 'error') {
throw new Error(tableData.message);
}
// Get payment method
const paymentMethod = document.querySelector('input[name="paymentMethod"]:checked').value;
// Create order
return fetch('api/create_order.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
table_id: tableData.table_id,
customer_phone: customerPhone,
items: cart
})
});
})
.then(response => response.json())
.then(orderData => {
if (orderData.status === 'error') {
throw new Error(orderData.message);
}
// Save the orderData for later use
savedOrderData = orderData;
// Get payment method
const paymentMethod = document.querySelector('input[name="paymentMethod"]:checked').value;
// Process payment
return fetch('api/process_payment.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
order_id: orderData.order_id,
payment_method: paymentMethod,
amount: total
})
});
})
.then(response => response.json())
.then(paymentData => {
if (paymentData.status === 'error') {
throw new Error(paymentData.message);
}
// Clear cart
cart = [];
localStorage.setItem('cart_' + tableId, JSON.stringify(cart));
updateCartCount();
// Show success modal
checkoutModal.hide();
// Fetch updated order details
fetchOrders();
// Show order success modal with details
const orderDetailsContainer = document.getElementById('orderDetailsContainer');
orderDetailsContainer.innerHTML = `
<div class="alert alert-success">
<p><strong>Order ID:</strong> #${savedOrderData.order_id}</p>
<p><strong>Payment Method:</strong> ${document.querySelector('input[name="paymentMethod"]:checked').value}</p>
<p><strong>Amount:</strong> ${CURRENCY}${total.toFixed(2)}</p>
<p>Your order has been confirmed and is being prepared.</p>
</div>
`;
orderSuccessModal.show();
})
.catch(error => {
alert('Error: ' + error.message);
});
}
// Show leave table confirmation modal
function showLeaveTableConfirmation() {
leaveTableModal.show();
}
// Process leave table action
function leaveTable() {
// Show loading state
confirmLeaveBtn.innerHTML = '<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Processing...';
confirmLeaveBtn.disabled = true;
fetch('api/vacate_table.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
table_number: tableId,
customer_phone: customerPhone
})
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
// Clear local storage data for this table
localStorage.removeItem('cart_' + tableId);
// Show success message and redirect
leaveTableModal.hide();
// Create a success alert
const alertContainer = document.createElement('div');
alertContainer.className = 'alert alert-success';
alertContainer.innerHTML = `
<h4>Thank you for dining with us!</h4>
<p>${data.message}</p>
<p>Redirecting to homepage...</p>
`;
// Clear page content and show success message
document.querySelector('.container.py-4').innerHTML = '';
document.querySelector('.container.py-4').appendChild(alertContainer);
// Redirect after 3 seconds
setTimeout(() => {
window.location.href = 'index.php';
}, 3000);
} else {
// Show error
alert('Error: ' + data.message);
confirmLeaveBtn.innerHTML = 'Yes, Leave Table';
confirmLeaveBtn.disabled = false;
leaveTableModal.hide();
}
})
.catch(error => {
alert('Error: ' + error.message);
confirmLeaveBtn.innerHTML = 'Yes, Leave Table';
confirmLeaveBtn.disabled = false;
leaveTableModal.hide();
});
}
</script>
</body>
</html>