forked from Ameerah-2005/final-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.js
More file actions
120 lines (110 loc) · 4.49 KB
/
Copy pathcart.js
File metadata and controls
120 lines (110 loc) · 4.49 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
// Cart page rendering and interactions
function formatCurrency(n){
return '₦' + n.toLocaleString(undefined, {minimumFractionDigits:0, maximumFractionDigits:2});
}
function renderCart(){
const root = document.getElementById('cart-root');
if(!root) return;
const items = (typeof getCartItems === 'function') ? getCartItems() : JSON.parse(localStorage.getItem('dg_cart_items')||'[]');
if(!items || items.length === 0){
root.innerHTML = '<p>Your cart is empty.</p>';
return;
}
let html = '<table class="cart-table">';
html += '<thead><tr><th>Item</th><th>Size</th><th>Price</th><th>Qty</th><th>Line</th><th></th></tr></thead><tbody>';
items.forEach(it=>{
html += `<tr data-id="${it.id}">` +
`<td class="c-item"><img src="${it.img||''}" alt="" class="c-thumb"> <div>${it.name}</div></td>` +
`<td>${it.size||'-'}</td>` +
`<td>${formatCurrency(it.price)}</td>` +
`<td><input type="number" min="1" class="cart-qty" value="${it.quantity}"></td>` +
`<td class="c-line">${formatCurrency((it.price||0) * (it.quantity||0))}</td>` +
`<td><button class="remove-item">Remove</button></td>` +
`</tr>`;
});
html += '</tbody></table>';
const subtotal = items.reduce((s,i)=> s + (i.price||0) * (i.quantity||0), 0);
root.innerHTML = html;
// update aside summary if present
const summaryEl = document.getElementById('cart-subtotal');
if(summaryEl) summaryEl.textContent = formatCurrency(subtotal);
// wire events
root.querySelectorAll('.cart-qty').forEach(inp=>{
inp.addEventListener('change', e=>{
const tr = e.target.closest('tr');
const id = tr.getAttribute('data-id');
const val = Math.max(1, parseInt(e.target.value)||1);
updateItemQuantity(id, val);
});
});
root.querySelectorAll('.remove-item').forEach(btn=>{
btn.addEventListener('click', e=>{
const tr = e.target.closest('tr');
const id = tr.getAttribute('data-id');
removeItem(id);
});
});
}
function updateItemQuantity(id, qty){
const items = getCartItems();
const it = items.find(x=> x.id === id);
if(!it) return;
it.quantity = qty;
setCartItems(items);
renderCart();
}
function removeItem(id){
let items = getCartItems();
items = items.filter(x=> x.id !== id);
setCartItems(items);
renderCart();
}
document.addEventListener('DOMContentLoaded', ()=>{
renderCart();
const clearBtn = document.getElementById('clear-cart');
if(clearBtn) clearBtn.addEventListener('click', ()=>{ clearCart(); renderCart(); showToast('Cart cleared'); });
const bookBtn = document.getElementById('book-now');
if(bookBtn) bookBtn.addEventListener('click', showBookingModal);
// modal actions
const cancel = document.getElementById('cancel-book');
if(cancel) cancel.addEventListener('click', hideBookingModal);
const confirm = document.getElementById('confirm-book');
if(confirm) confirm.addEventListener('click', ()=>{ confirmBooking(); });
});
function showBookingModal(){
const items = getCartItems();
const details = document.getElementById('booking-details');
if(!details) return;
if(!items || items.length === 0){
details.innerHTML = '<p>Your cart is empty.</p>';
} else {
let html = '<ul class="booking-list">';
items.forEach(it=>{
html += `<li>${it.name} (${it.size||'-'}) x ${it.quantity} — ${formatCurrency((it.price||0)*it.quantity)}</li>`;
});
const subtotal = items.reduce((s,i)=> s + (i.price||0) * (i.quantity||0), 0);
html += `</ul><p><strong>Subtotal: ${formatCurrency(subtotal)}</strong></p>`;
details.innerHTML = html;
}
const modal = document.getElementById('booking-modal');
if(modal){ modal.classList.add('show'); modal.setAttribute('aria-hidden','false'); }
}
function hideBookingModal(){
const modal = document.getElementById('booking-modal');
if(modal){ modal.classList.remove('show'); modal.setAttribute('aria-hidden','true'); }
}
function confirmBooking(){
// placeholder booking flow
const items = getCartItems();
if(!items || items.length===0){
showToast('Cart is empty');
hideBookingModal();
return;
}
// call existing bookNow (keeps simple alert behavior)
bookNow();
clearCart();
renderCart();
hideBookingModal();
showToast('Booking confirmed');
}