Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
461 lines (420 loc) · 23.3 KB
/
Copy pathscript.js
File metadata and controls
461 lines (420 loc) · 23.3 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
// ===== FLIPKART APP - JAVASCRIPT =====
// --- Product Data ---
const products = [
{ id: 1, name: "iPhone 15 Pro Max 256GB", brand: "Apple", category: "phones", price: 134999, originalPrice: 159900, discount: 16, rating: 4.6, reviews: 12453, image: "product_phone.png", features: ["A17 Pro Chip", "48MP Camera", "Titanium Design", "USB-C"] },
{ id: 2, name: "MacBook Air M2 13-inch", brand: "Apple", category: "laptops", price: 89990, originalPrice: 119900, discount: 25, rating: 4.7, reviews: 8921, image: "product_laptop.png", features: ["M2 Chip", "8GB RAM", "256GB SSD", "18hr Battery"] },
{ id: 3, name: "Samsung Galaxy S24 Ultra", brand: "Samsung", category: "phones", price: 109999, originalPrice: 134999, discount: 19, rating: 4.5, reviews: 9876, image: "product_phone.png", features: ["Snapdragon 8 Gen 3", "200MP Camera", "S Pen", "AI Features"] },
{ id: 4, name: "Sony WH-1000XM5 Headphones", brand: "Sony", category: "audio", price: 22990, originalPrice: 34990, discount: 34, rating: 4.8, reviews: 15632, image: "product_laptop.png", features: ["30hr Battery", "ANC", "LDAC", "Multipoint"] },
{ id: 5, name: "HP Pavilion 15 Ryzen 7", brand: "HP", category: "laptops", price: 54990, originalPrice: 72999, discount: 25, rating: 4.3, reviews: 5431, image: "product_laptop.png", features: ["Ryzen 7", "16GB RAM", "512GB SSD", "FHD Display"] },
{ id: 6, name: "OnePlus 12 5G 256GB", brand: "OnePlus", category: "phones", price: 59999, originalPrice: 69999, discount: 14, rating: 4.4, reviews: 7654, image: "product_phone.png", features: ["Snapdragon 8 Gen 3", "50MP Camera", "100W Charging", "AMOLED"] },
{ id: 7, name: "boAt Airdopes 441 TWS", brand: "boAt", category: "audio", price: 1299, originalPrice: 4490, discount: 71, rating: 4.1, reviews: 234567, image: "product_laptop.png", features: ["Bluetooth 5.0", "6hr Playback", "IPX7", "Touch Controls"] },
{ id: 8, name: "Apple Watch Series 9 GPS", brand: "Apple", category: "wearables", price: 37900, originalPrice: 41900, discount: 10, rating: 4.6, reviews: 4521, image: "product_phone.png", features: ["S9 SiP", "Always-On", "Blood O₂", "ECG App"] },
{ id: 9, name: "Dell Inspiron 14 i5 13th", brand: "Dell", category: "laptops", price: 49990, originalPrice: 65990, discount: 24, rating: 4.2, reviews: 3245, image: "product_laptop.png", features: ["i5-1335U", "8GB RAM", "512GB SSD", "14\" FHD"] },
{ id: 10, name: "Noise ColorFit Pro 5", brand: "Noise", category: "wearables", price: 2999, originalPrice: 5999, discount: 50, rating: 4.0, reviews: 45123, image: "product_phone.png", features: ["1.85\" AMOLED", "BT Calling", "SpO2", "7-Day Battery"] },
{ id: 11, name: "JBL Tune 760NC Wireless", brand: "JBL", category: "audio", price: 4499, originalPrice: 7999, discount: 44, rating: 4.3, reviews: 8765, image: "product_laptop.png", features: ["ANC", "50hr Battery", "Foldable", "Multi-Connect"] },
{ id: 12, name: "Google Pixel 8 Pro 128GB", brand: "Google", category: "phones", price: 79999, originalPrice: 106999, discount: 25, rating: 4.5, reviews: 6543, image: "product_phone.png", features: ["Tensor G3", "50MP Camera", "7 Years Updates", "AI Magic"] },
{ id: 13, name: "Maybelline Fit Me Foundation", brand: "Maybelline", category: "beauty", price: 399, originalPrice: 599, discount: 33, rating: 4.3, reviews: 87654, image: "product_phone.png", features: ["Matte + Poreless", "SPF 22", "16HR Wear", "Oil-Free"] },
{ id: 14, name: "L'Oréal Paris Revitalift Serum", brand: "L'Oréal", category: "beauty", price: 899, originalPrice: 1499, discount: 40, rating: 4.5, reviews: 34521, image: "product_laptop.png", features: ["1.5% Hyaluronic Acid", "Anti-Aging", "Hydrating", "Derma Tested"] },
{ id: 15, name: "Lakmé 9 to 5 Lipstick Set", brand: "Lakmé", category: "beauty", price: 649, originalPrice: 1200, discount: 46, rating: 4.4, reviews: 45678, image: "product_phone.png", features: ["Matte Finish", "12HR Stay", "Set of 3", "Moisturizing"] },
{ id: 16, name: "Neutrogena Sunscreen SPF 50+", brand: "Neutrogena", category: "beauty", price: 549, originalPrice: 849, discount: 35, rating: 4.6, reviews: 23456, image: "product_laptop.png", features: ["Ultra Sheer", "SPF 50+ PA+++", "Non-Greasy", "Water Resistant"] },
{ id: 17, name: "MAC Ruby Woo Lipstick", brand: "MAC", category: "beauty", price: 1450, originalPrice: 1950, discount: 26, rating: 4.7, reviews: 56789, image: "product_phone.png", features: ["Retro Matte", "Iconic Red", "Long-Lasting", "Vivid Color"] },
{ id: 18, name: "Nykaa Skincare Combo Kit", brand: "Nykaa", category: "beauty", price: 799, originalPrice: 1599, discount: 50, rating: 4.2, reviews: 12345, image: "product_laptop.png", features: ["Cleanser + Toner", "Vitamin C", "For All Skin", "Paraben Free"] },
{ id: 19, name: "Forest Essentials Night Cream", brand: "Forest Essentials", category: "beauty", price: 1875, originalPrice: 2500, discount: 25, rating: 4.8, reviews: 9876, image: "product_phone.png", features: ["Ayurvedic", "Rejuvenating", "With Saffron", "Luxury Care"] },
{ id: 20, name: "Biotique Bio Morning Nectar Pack", brand: "Biotique", category: "beauty", price: 345, originalPrice: 699, discount: 51, rating: 4.1, reviews: 67890, image: "product_laptop.png", features: ["Face Wash + Cream", "100% Botanical", "Glowing Skin", "No Chemicals"] },
];
const searchSuggestions = [
{ text: "iPhone 15", category: "Mobiles" }, { text: "Samsung Galaxy S24", category: "Mobiles" },
{ text: "Laptop deals", category: "Laptops" }, { text: "Wireless earbuds", category: "Audio" },
{ text: "Running shoes Nike", category: "Footwear" }, { text: "T-shirts for men", category: "Fashion" },
{ text: "Smartwatch under 5000", category: "Wearables" }, { text: "Air conditioner", category: "Appliances" },
{ text: "Lipstick MAC", category: "Beauty" }, { text: "Sunscreen SPF 50", category: "Beauty" },
{ text: "Face serum vitamin C", category: "Beauty" }, { text: "Foundation matte", category: "Beauty" },
];
// --- State ---
let cart = JSON.parse(localStorage.getItem('flipkart_cart') || '[]');
let wishlist = JSON.parse(localStorage.getItem('flipkart_wishlist') || '[]');
let recentlyViewed = JSON.parse(localStorage.getItem('flipkart_recent') || '[]');
// --- DOM Elements ---
const $ = (sel) => document.querySelector(sel);
const $$ = (sel) => document.querySelectorAll(sel);
// --- Init ---
document.addEventListener('DOMContentLoaded', () => {
renderProducts('all');
updateCartUI();
initCarousel();
initCountdown();
initSearch();
initModals();
initScrollEffects();
initFilters();
initMobileNav();
updateAnnouncementTimer();
});
// --- Render Products ---
function renderProducts(filter) {
const grid = $('#products-grid');
const filtered = filter === 'all' ? products : products.filter(p => p.category === filter);
grid.innerHTML = filtered.map(p => `
<div class="product-card" data-id="${p.id}">
<div class="product-card-image">
<span class="product-discount-badge">${p.discount}% off</span>
<button class="product-wishlist-btn ${wishlist.includes(p.id) ? 'active' : ''}" data-id="${p.id}" onclick="event.stopPropagation(); toggleWishlist(${p.id})">
<svg viewBox="0 0 24 24" fill="${wishlist.includes(p.id) ? 'currentColor' : 'none'}" stroke="currentColor" stroke-width="2"><path d="M20.84 4.61a5.5 5.5 0 00-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 00-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 000-7.78z"/></svg>
</button>
<img src="${p.image}" alt="${p.name}" loading="lazy">
</div>
<div class="product-card-info">
<div class="product-brand">${p.brand}</div>
<div class="product-name">${p.name}</div>
<div class="product-rating">
<span class="rating-badge">${p.rating} ★</span>
<span class="rating-count">(${formatNum(p.reviews)})</span>
</div>
<div class="product-pricing">
<span class="product-price">₹${formatPrice(p.price)}</span>
<span class="product-original-price">₹${formatPrice(p.originalPrice)}</span>
<span class="product-discount-text">${p.discount}% off</span>
</div>
</div>
<div class="product-card-actions">
<button class="add-to-cart-btn" onclick="event.stopPropagation(); addToCart(${p.id})">🛒 Add to Cart</button>
<button class="quick-view-btn" onclick="event.stopPropagation(); openQuickView(${p.id})">👁</button>
</div>
</div>
`).join('');
// Card click → quick view
grid.querySelectorAll('.product-card').forEach(card => {
card.addEventListener('click', () => {
const id = parseInt(card.dataset.id);
addToRecentlyViewed(id);
openQuickView(id);
});
});
}
// --- Carousel ---
function initCarousel() {
const slides = $$('.carousel-slide');
const dots = $$('.dot');
let current = 0;
let interval;
const show = (idx) => {
slides.forEach(s => s.classList.remove('active'));
dots.forEach(d => d.classList.remove('active'));
current = (idx + slides.length) % slides.length;
slides[current].classList.add('active');
dots[current].classList.add('active');
};
const startAuto = () => { interval = setInterval(() => show(current + 1), 5000); };
const stopAuto = () => clearInterval(interval);
$('#carousel-prev').onclick = () => { stopAuto(); show(current - 1); startAuto(); };
$('#carousel-next').onclick = () => { stopAuto(); show(current + 1); startAuto(); };
dots.forEach(d => d.onclick = () => { stopAuto(); show(+d.dataset.index); startAuto(); });
startAuto();
// Touch swipe
let startX;
const track = $('#hero-carousel');
track.addEventListener('touchstart', e => { startX = e.touches[0].clientX; stopAuto(); }, { passive: true });
track.addEventListener('touchend', e => {
const diff = startX - e.changedTouches[0].clientX;
if (Math.abs(diff) > 50) show(current + (diff > 0 ? 1 : -1));
startAuto();
});
}
// --- Countdown ---
function initCountdown() {
const update = () => {
const now = new Date();
const end = new Date(now);
end.setHours(23, 59, 59, 999);
const diff = end - now;
const h = Math.floor(diff / 3600000);
const m = Math.floor((diff % 3600000) / 60000);
const s = Math.floor((diff % 60000) / 1000);
$('#hours').textContent = String(h).padStart(2, '0');
$('#minutes').textContent = String(m).padStart(2, '0');
$('#seconds').textContent = String(s).padStart(2, '0');
};
update();
setInterval(update, 1000);
}
function updateAnnouncementTimer() {
const timer = $('#announcement-timer');
const update = () => {
const now = new Date();
const end = new Date(now);
end.setHours(23, 59, 59, 999);
const diff = end - now;
const h = Math.floor(diff / 3600000);
const m = Math.floor((diff % 3600000) / 60000);
timer.textContent = `Ends in ${h}h ${m}m`;
};
update();
setInterval(update, 60000);
}
// --- Search ---
function initSearch() {
const input = $('#search-input');
const box = $('#search-suggestions');
input.addEventListener('input', () => {
const q = input.value.trim().toLowerCase();
if (q.length < 2) { box.classList.remove('active'); return; }
const matched = searchSuggestions.filter(s => s.text.toLowerCase().includes(q));
const productMatch = products.filter(p => p.name.toLowerCase().includes(q) || p.brand.toLowerCase().includes(q)).slice(0, 4);
let html = matched.map(s => `<div class="suggestion-item" onclick="performSearch('${s.text}')"><span class="suggestion-icon">🔍</span>${s.text}<span class="suggestion-category">${s.category}</span></div>`).join('');
html += productMatch.map(p => `<div class="suggestion-item" onclick="openQuickView(${p.id})"><span class="suggestion-icon">📦</span>${p.name}<span class="suggestion-category">₹${formatPrice(p.price)}</span></div>`).join('');
if (html) { box.innerHTML = html; box.classList.add('active'); }
else { box.classList.remove('active'); }
});
input.addEventListener('keydown', e => { if (e.key === 'Enter') { performSearch(input.value); box.classList.remove('active'); } });
document.addEventListener('click', e => { if (!e.target.closest('.search-container')) box.classList.remove('active'); });
}
function performSearch(query) {
$('#search-input').value = query;
$('#search-suggestions').classList.remove('active');
const q = query.toLowerCase();
const results = products.filter(p => p.name.toLowerCase().includes(q) || p.brand.toLowerCase().includes(q) || p.category.includes(q));
if (results.length) {
const grid = $('#products-grid');
$$('.filter-chip').forEach(c => c.classList.remove('active'));
$$('.filter-chip')[0].classList.add('active');
grid.innerHTML = '';
renderFilteredProducts(results);
document.querySelector('.products-section').scrollIntoView({ behavior: 'smooth' });
showToast(`Found ${results.length} result${results.length > 1 ? 's' : ''} for "${query}"`);
} else {
showToast('No products found', 'error');
}
}
function renderFilteredProducts(list) {
const grid = $('#products-grid');
grid.innerHTML = list.map(p => `
<div class="product-card" data-id="${p.id}">
<div class="product-card-image">
<span class="product-discount-badge">${p.discount}% off</span>
<button class="product-wishlist-btn ${wishlist.includes(p.id) ? 'active' : ''}" data-id="${p.id}" onclick="event.stopPropagation(); toggleWishlist(${p.id})">
<svg viewBox="0 0 24 24" fill="${wishlist.includes(p.id) ? 'currentColor' : 'none'}" stroke="currentColor" stroke-width="2"><path d="M20.84 4.61a5.5 5.5 0 00-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 00-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 000-7.78z"/></svg>
</button>
<img src="${p.image}" alt="${p.name}" loading="lazy">
</div>
<div class="product-card-info">
<div class="product-brand">${p.brand}</div>
<div class="product-name">${p.name}</div>
<div class="product-rating"><span class="rating-badge">${p.rating} ★</span><span class="rating-count">(${formatNum(p.reviews)})</span></div>
<div class="product-pricing">
<span class="product-price">₹${formatPrice(p.price)}</span>
<span class="product-original-price">₹${formatPrice(p.originalPrice)}</span>
<span class="product-discount-text">${p.discount}% off</span>
</div>
</div>
<div class="product-card-actions">
<button class="add-to-cart-btn" onclick="event.stopPropagation(); addToCart(${p.id})">🛒 Add to Cart</button>
<button class="quick-view-btn" onclick="event.stopPropagation(); openQuickView(${p.id})">👁</button>
</div>
</div>
`).join('');
}
// --- Filters ---
function initFilters() {
$$('.filter-chip').forEach(chip => {
chip.addEventListener('click', () => {
$$('.filter-chip').forEach(c => c.classList.remove('active'));
chip.classList.add('active');
renderProducts(chip.dataset.filter);
});
});
}
// --- Cart ---
function addToCart(id) {
const existing = cart.find(c => c.id === id);
if (existing) { existing.qty++; }
else { cart.push({ id, qty: 1 }); }
saveCart();
updateCartUI();
showToast('Added to cart! 🛒', 'success');
}
function removeFromCart(id) {
cart = cart.filter(c => c.id !== id);
saveCart();
updateCartUI();
renderCartItems();
}
function updateQty(id, delta) {
const item = cart.find(c => c.id === id);
if (!item) return;
item.qty += delta;
if (item.qty <= 0) { removeFromCart(id); return; }
saveCart();
updateCartUI();
renderCartItems();
}
function saveCart() {
localStorage.setItem('flipkart_cart', JSON.stringify(cart));
}
function updateCartUI() {
const count = cart.reduce((s, c) => s + c.qty, 0);
const badge = $('#cart-badge');
const mobBadge = $('#mob-cart-badge');
badge.textContent = count;
if (count > 0) { badge.classList.add('visible'); badge.style.display = 'flex'; }
else { badge.classList.remove('visible'); badge.style.display = 'none'; }
if (mobBadge) { mobBadge.textContent = count; mobBadge.style.display = count > 0 ? 'flex' : 'none'; }
}
function renderCartItems() {
const container = $('#cart-items');
const footer = $('#cart-footer');
if (cart.length === 0) {
container.innerHTML = `<div class="empty-cart"><div class="empty-cart-icon">🛒</div><h3>Your cart is empty!</h3><p>Add items to continue shopping</p><button class="cta-primary" onclick="closeCart()">Start Shopping</button></div>`;
footer.style.display = 'none';
return;
}
let total = 0;
container.innerHTML = cart.map(c => {
const p = products.find(pr => pr.id === c.id);
if (!p) return '';
total += p.price * c.qty;
return `
<div class="cart-item">
<div class="cart-item-image"><img src="${p.image}" alt="${p.name}"></div>
<div class="cart-item-details">
<div class="cart-item-name">${p.name}</div>
<div class="cart-item-brand">${p.brand}</div>
<div class="cart-item-price">₹${formatPrice(p.price * c.qty)}</div>
<div class="cart-item-controls">
<button class="qty-btn" onclick="updateQty(${p.id},-1)">−</button>
<span class="qty-value">${c.qty}</span>
<button class="qty-btn" onclick="updateQty(${p.id},1)">+</button>
<button class="cart-item-remove" onclick="removeFromCart(${p.id})">REMOVE</button>
</div>
</div>
</div>`;
}).join('');
footer.style.display = 'block';
$('#cart-total-price').textContent = `₹${formatPrice(total)}`;
}
function openCart() {
renderCartItems();
$('#cart-overlay').classList.add('active');
document.body.style.overflow = 'hidden';
}
function closeCart() {
$('#cart-overlay').classList.remove('active');
document.body.style.overflow = '';
}
// --- Wishlist ---
function toggleWishlist(id) {
const idx = wishlist.indexOf(id);
if (idx > -1) { wishlist.splice(idx, 1); showToast('Removed from wishlist'); }
else { wishlist.push(id); showToast('Added to wishlist ❤️', 'success'); }
localStorage.setItem('flipkart_wishlist', JSON.stringify(wishlist));
// Update UI
$$('.product-wishlist-btn').forEach(btn => {
const bid = +btn.dataset.id;
btn.classList.toggle('active', wishlist.includes(bid));
const svg = btn.querySelector('svg');
svg.setAttribute('fill', wishlist.includes(bid) ? 'currentColor' : 'none');
});
}
// --- Quick View ---
function openQuickView(id) {
const p = products.find(pr => pr.id === id);
if (!p) return;
addToRecentlyViewed(id);
const body = $('#quickview-body');
body.innerHTML = `
<div class="quickview-image"><img src="${p.image}" alt="${p.name}"></div>
<div class="quickview-details">
<div class="product-brand">${p.brand}</div>
<div class="product-name">${p.name}</div>
<div class="product-rating"><span class="rating-badge">${p.rating} ★</span><span class="rating-count">(${formatNum(p.reviews)} reviews)</span></div>
<div class="product-pricing">
<span class="product-price">₹${formatPrice(p.price)}</span>
<span class="product-original-price">₹${formatPrice(p.originalPrice)}</span>
<span class="product-discount-text">${p.discount}% off</span>
</div>
<ul class="quickview-features">${p.features.map(f => `<li>${f}</li>`).join('')}</ul>
<div class="quickview-actions">
<button class="add-to-cart-btn" onclick="addToCart(${p.id}); closeModal('quickview-modal')">🛒 Add to Cart</button>
<button class="add-to-cart-btn" style="background:var(--flipkart-orange)" onclick="addToCart(${p.id}); closeModal('quickview-modal'); openCart()">⚡ Buy Now</button>
</div>
</div>`;
openModal('quickview-modal');
}
// --- Recently Viewed ---
function addToRecentlyViewed(id) {
recentlyViewed = recentlyViewed.filter(r => r !== id);
recentlyViewed.unshift(id);
if (recentlyViewed.length > 8) recentlyViewed.pop();
localStorage.setItem('flipkart_recent', JSON.stringify(recentlyViewed));
renderRecentlyViewed();
}
function renderRecentlyViewed() {
if (recentlyViewed.length === 0) return;
const section = $('#recently-viewed-section');
const grid = $('#recently-viewed-grid');
section.style.display = 'block';
grid.innerHTML = recentlyViewed.map(id => {
const p = products.find(pr => pr.id === id);
if (!p) return '';
return `<div class="product-card" style="min-width:200px;flex-shrink:0" onclick="openQuickView(${p.id})">
<div class="product-card-image" style="height:160px"><img src="${p.image}" alt="${p.name}"></div>
<div class="product-card-info"><div class="product-brand">${p.brand}</div><div class="product-name">${p.name}</div><div class="product-price">₹${formatPrice(p.price)}</div></div>
</div>`;
}).join('');
}
// --- Modals ---
function initModals() {
// Login
$('#login-btn').onclick = () => openModal('login-modal');
$('#login-modal-close').onclick = () => closeModal('login-modal');
$('#login-modal').onclick = e => { if (e.target === $('#login-modal')) closeModal('login-modal'); };
$('#login-form').onsubmit = e => { e.preventDefault(); showToast('Login successful! 🎉', 'success'); closeModal('login-modal'); };
// Quick View
$('#quickview-close').onclick = () => closeModal('quickview-modal');
$('#quickview-modal').onclick = e => { if (e.target === $('#quickview-modal')) closeModal('quickview-modal'); };
// Cart
$('#cart-btn').onclick = openCart;
$('#mob-nav-cart').addEventListener('click', openCart);
$('#cart-close').onclick = closeCart;
$('#cart-overlay').onclick = e => { if (e.target === $('#cart-overlay')) closeCart(); };
$('#checkout-btn').onclick = () => { showToast('Order placed successfully! 🎉', 'success'); cart = []; saveCart(); updateCartUI(); renderCartItems(); };
// Mobile menu
$('#more-btn').onclick = () => $('#mobile-menu-overlay').classList.add('active');
$('#mob-nav-account').addEventListener('click', () => $('#mobile-menu-overlay').classList.add('active'));
$('#mobile-menu-close').onclick = () => $('#mobile-menu-overlay').classList.remove('active');
$('#mobile-menu-overlay').onclick = e => { if (e.target === $('#mobile-menu-overlay')) $('#mobile-menu-overlay').classList.remove('active'); };
}
function openModal(id) { $(`#${id}`).classList.add('active'); document.body.style.overflow = 'hidden'; }
function closeModal(id) { $(`#${id}`).classList.remove('active'); document.body.style.overflow = ''; }
// --- Scroll Effects ---
function initScrollEffects() {
const backToTop = $('#back-to-top');
window.addEventListener('scroll', () => {
backToTop.classList.toggle('visible', window.scrollY > 400);
});
backToTop.onclick = () => window.scrollTo({ top: 0, behavior: 'smooth' });
}
// --- Mobile Nav ---
function initMobileNav() {
$$('.mob-nav-item').forEach(item => {
item.addEventListener('click', () => {
$$('.mob-nav-item').forEach(i => i.classList.remove('active'));
item.classList.add('active');
const page = item.dataset.page;
if (page === 'home') window.scrollTo({ top: 0, behavior: 'smooth' });
else if (page === 'categories') $('.categories-nav').scrollIntoView({ behavior: 'smooth' });
else if (page === 'cart') openCart();
});
});
}
// --- Toast ---
function showToast(msg, type = '') {
const container = $('#toast-container');
const toast = document.createElement('div');
toast.className = `toast ${type}`;
toast.textContent = msg;
container.appendChild(toast);
setTimeout(() => toast.remove(), 3000);
}
// --- Helpers ---
function formatPrice(n) { return n.toLocaleString('en-IN'); }
function formatNum(n) { return n >= 1000 ? (n / 1000).toFixed(1) + 'K' : n; }