-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
1120 lines (968 loc) · 46 KB
/
app.js
File metadata and controls
1120 lines (968 loc) · 46 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
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { initializeApp } from "https://www.gstatic.com/firebasejs/12.3.0/firebase-app.js";
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword, onAuthStateChanged, signInWithCustomToken, signInAnonymously, signOut } from "https://www.gstatic.com/firebasejs/12.3.0/firebase-auth.js";
import {
initializeApp
} from "https://www.gstatic.com/firebasejs/12.3.0/firebase-app.js";
// Import Firebase Auth functions
import {
getAuth,
onAuthStateChanged,
signOut,
} from "https://www.gstatic.com/firebasejs/12.3.0/firebase-auth.js";
import {
getFirestore,
collection,
query,
onSnapshot,
where,
doc,
setDoc,
getDoc,
updateDoc,
increment,
setLogLevel
} from "https://www.gstatic.com/firebasejs/12.3.0/firebase-firestore.js";
// Firebase global variables provided by the environment
const appId = typeof __app_id !== 'undefined' ? __app_id : 'default-app-id';
const firebaseConfig = {
apiKey: "AIzaSyAqDLocdi1CDHDeD5Z_LOlVZOycI2tuJpk",
authDomain: "digisoria-baa83.firebaseapp.com",
projectId: "digisoria-baa83",
storageBucket: "digisoria-baa83.appspot.com",
messagingSenderId: "1042609120554",
appId: "1:1042609120554:web:0500a100e9ae42ead32a53",
measurementId: "G-TM8CBPSRR9"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const auth = getAuth(app);
const METRICS_COLLECTION = 'metrics';
const METRICS_DOC_ID = 'funnel_counts';
let CURRENT_USER_ID = 'not-authenticated-user-placeholder';
let isAuthResolved = false;
const updateUserMetric = async (fieldName) => {
if (CURRENT_USER_ID === 'not-authenticated-user-placeholder') {
console.warn(`User is not authenticated. Cannot track metric: ${fieldName}.`);
alert('Login Required', 'Please log in to track your order activity.', 'info');
return;
}
const metricsRef = doc(db, 'users', CURRENT_USER_ID, METRICS_COLLECTION, METRICS_DOC_ID);
try {
await setDoc(metricsRef, {
[fieldName]: increment(1),
lastUpdated: new Date().toISOString()
}, {
merge: true
});
console.log(`User metric updated: ${fieldName} for user: ${CURRENT_USER_ID}.`);
} catch (e) {
console.error(`Error updating user metric ${fieldName}:`, e);
}
};
// ===================================
// CART PERSISTENCE LOGIC (FIXED PROBLEM)
// ===================================
const CART_COLLECTION = 'carts';
const CART_DOC_ID = 'currentCart';
// ** Global Cart State **
let cartItems = [];
/**
* Saves the current cartItems array to Firestore for the logged-in user.
*/
const saveCartToFirestore = async () => {
// <<< CORE FIX: Check if auth is resolved AND user is logged in
if (!isAuthResolved || CURRENT_USER_ID === 'not-authenticated-user-placeholder') {
console.warn("Authentication not resolved or user not logged in. Cannot save cart.");
return;
}
// Path: users/{UID}/carts/currentCart
const cartRef = doc(db, 'users', CURRENT_USER_ID, CART_COLLECTION, CART_DOC_ID);
try {
// Save the entire cartItems array
await setDoc(cartRef, {
items: cartItems,
lastUpdated: new Date().toISOString()
}, {
merge: true
}); // Use merge: true just in case
console.log("Shopping cart saved to Firestore successfully.");
} catch (e) {
console.error("Error saving cart to Firestore:", e);
}
};
/**
* Loads the cartItems array from Firestore for the logged-in user.
*/
const loadCartFromFirestore = async (userId) => {
if (userId === 'not-authenticated-user-placeholder') {
return [];
}
const cartRef = doc(db, 'users', userId, CART_COLLECTION, CART_DOC_ID);
try {
const docSnap = await getDoc(cartRef);
if (docSnap.exists()) {
const data = docSnap.data();
console.log("Shopping cart loaded from Firestore successfully. Items:", data.items ? data.items.length : 0);
return data.items || [];
} else {
console.log("No existing cart found in Firestore. Starting with an empty cart.");
return [];
}
} catch (e) {
console.error("Error loading cart from Firestore:", e);
return []; // Fallback to empty cart on error
}
};
// ===================================
// AUTHENTICATION STATE MANAGEMENT - INTEGRATED WITH CART LOAD (FIXED)
// ===================================
const userIndicator = document.getElementById('user-indicator');
const authButtonText = document.getElementById('auth-button-text');
// ** Refactored onAuthStateChanged for synchronous cart state update **
onAuthStateChanged(auth, async (user) => {
if (user) {
// User is signed in.
CURRENT_USER_ID = user.uid;
const displayName = user.displayName || user.email;
userIndicator.textContent = `Welcome, ${displayName.split('@')[0]}`;
userIndicator.classList.remove('hidden', 'bg-white/20');
userIndicator.classList.add('bg-accent-orange/80');
authButtonText.textContent = "Sign Out";
// Load cart and update global state after authentication
cartItems = await loadCartFromFirestore(CURRENT_USER_ID);
renderCart();
} else {
// User is signed out.
CURRENT_USER_ID = 'not-authenticated-user-placeholder';
userIndicator.textContent = 'Logged Out';
userIndicator.classList.remove('hidden', 'bg-accent-orange/80');
userIndicator.classList.add('bg-white/20');
authButtonText.textContent = "Log In";
// Clear cart immediately on sign out
cartItems = [];
renderCart();
}
// <<< CORE FIX: Set the flag AFTER we have determined the user status
isAuthResolved = true;
});
// ===================================
// CART STATE & LOGIC
// ===================================
const cartCountElement = document.getElementById('cart-count');
const cartDrawer = document.getElementById('cart-drawer');
const backdrop = document.getElementById('backdrop'); // RENAMED backdrop
const cartItemsList = document.getElementById('cart-items-list');
const cartTotalElement = document.getElementById('cart-total');
const selectedItemCountElement = document.getElementById('selected-item-count');
const checkoutButton = document.getElementById('checkout-button');
const selectAllCheckbox = document.getElementById('select-all-checkbox');
const orderReviewScreen = document.getElementById('order-review-screen');
const trackOrderScreen = document.getElementById('track-order-screen');
const SHIPPING_SUBTOTAL = 4.99;
const updateCartCountDisplay = () => {
const totalCount = cartItems.reduce((sum, item) => sum + item.quantity, 0);
if (cartCountElement) {
cartCountElement.textContent = totalCount;
}
};
const updateCartTotal = () => {
let subtotal = 0;
let selectedCount = 0;
cartItems.forEach(item => {
if (item.selected) {
subtotal += item.price * item.quantity;
selectedCount++;
}
});
if (cartTotalElement) {
cartTotalElement.textContent = subtotal.toFixed(2);
}
if (selectedItemCountElement) {
selectedItemCountElement.textContent = selectedCount;
}
if (checkoutButton) {
checkoutButton.disabled = selectedCount === 0;
}
if (selectAllCheckbox) {
// Only check 'select all' if the cart isn't empty and all items are selected
selectAllCheckbox.checked = cartItems.length > 0 && selectedCount === cartItems.length;
}
};
const renderCart = () => {
cartItemsList.innerHTML = '';
if (cartItems.length === 0) {
cartItemsList.innerHTML = '<p class="text-center text-text-muted py-8">Your cart is empty.</p>';
} else {
cartItems.forEach((item, index) => {
const itemElement = document.createElement('div');
itemElement.className = 'bg-white rounded-lg shadow-sm flex flex-col space-y-3';
// Shop Name Header Row
const shopHeader = document.createElement('div');
shopHeader.className =
'flex items-center p-3 border-b border-gray-100 bg-secondary-gray rounded-t-lg';
shopHeader.innerHTML = `
<label class="flex items-center w-full">
<input type="checkbox" data-index="${index}" class="item-select-checkbox h-4 w-4 text-primary-blue rounded focus:ring-primary-blue border-gray-300 mr-3" ${item.selected ? 'checked' : ''}>
<span class="font-semibold text-text-dark text-sm">Shop Name Placeholder</span>
</label>
`;
itemElement.appendChild(shopHeader);
// Item Detail Row
const itemDetail = document.createElement('div');
itemDetail.className = 'flex items-start justify-between space-x-2 p-3 pt-0';
itemDetail.innerHTML = `
<div class="flex items-start flex-shrink-0 w-1/2">
<img src="${item.image}" alt="${item.name}" class="w-16 h-16 object-cover rounded-md flex-shrink-0 mr-3 mt-1">
<div class="flex flex-col flex-grow min-w-0">
<span class="font-medium text-text-dark truncate">${item.name}</span>
<select class="mt-1 text-xs text-text-muted border-gray-200 rounded-md p-1 min-w-[5rem] max-w-full">
<option>Variations: Default</option>
<option>Variations: Option A</option>
</select>
</div>
</div>
<div class="flex flex-grow justify-end items-center text-sm space-x-2">
<div class="flex flex-col items-center pt-2 hidden lg:flex min-w-[4rem]">
<span class="text-text-muted text-xs">Unit Price</span>
<span class="font-semibold text-primary-blue text-sm">$${item.price.toFixed(2)}</span>
</div>
<div class="flex flex-col items-center pt-2 min-w-[5.5rem] flex-shrink-0">
<span class="text-text-muted text-xs">Qty</span>
<div class="flex items-center border border-gray-300 rounded-md mt-1">
<button class="qty-btn text-text-dark hover:bg-gray-100 px-2 py-1 rounded-l-md" data-index="${index}" data-action="decrease"><i class="fas fa-minus text-xs"></i></button>
<span class="px-2 font-medium text-sm">${item.quantity}</span>
<button class="qty-btn text-text-dark hover:bg-gray-100 px-2 py-1 rounded-r-md" data-index="${index}" data-action="increase"><i class="fas fa-plus text-xs"></i></button>
</div>
</div>
<div class="flex flex-col items-center pt-2 min-w-[4.5rem] flex-shrink-0">
<span class="text-text-muted text-xs">Total</span>
<span class="font-bold text-red-danger text-lg">$${(item.price * item.quantity).toFixed(2)}</span>
</div>
<button
class="delete-item-btn text-text-muted hover:text-red-danger transition-colors ml-2 pt-2 flex-shrink-0"
data-index="${index}">
<i class="fas fa-trash-alt text-lg"></i>
</button>
</div>
`;
itemElement.appendChild(itemDetail);
cartItemsList.appendChild(itemElement);
});
attachCartEventListeners();
}
updateCartTotal();
updateCartCountDisplay();
};
const attachCartEventListeners = () => {
document.querySelectorAll('.qty-btn').forEach(button => {
button.addEventListener('click', (event) => {
const index = parseInt(event.currentTarget.dataset.index);
const action = event.currentTarget.dataset.action;
updateQuantity(index, action);
});
});
document.querySelectorAll('.delete-item-btn').forEach(button => {
button.addEventListener('click', (event) => {
const index = parseInt(event.currentTarget.dataset.index);
deleteItem(index);
});
});
document.querySelectorAll('.item-select-checkbox').forEach(checkbox => {
checkbox.addEventListener('change', (event) => {
const index = parseInt(event.target.dataset.index);
cartItems[index].selected = event.target.checked;
updateCartTotal();
saveCartToFirestore();
});
});
};
const toggleCartDrawer = () => {
cartDrawer.classList.toggle('open');
backdrop.classList.toggle('open');
if (cartDrawer.classList.contains('open')) {
renderCart();
}
// Close filter menu if cart is opened
if (cartDrawer.classList.contains('open') && filterMenu.classList.contains('open')) {
filterMenu.classList.remove('open');
}
// Close product detail modal if cart is opened
if (cartDrawer.classList.contains('open') && productDetailModal.classList.contains('open')) {
closeProductDetailModal();
}
};
const updateQuantity = (index, action) => {
// <<< CORE FIX: Gated function call
if (!isAuthResolved) {
alert('Loading...', 'Please wait for the page to finish loading user data.', 'info');
return;
}
const item = cartItems[index];
if (action === 'increase') {
item.quantity++;
} else if (action === 'decrease' && item.quantity > 1) {
item.quantity--;
} else if (action === 'decrease' && item.quantity === 1) {
deleteItem(index);
return;
}
renderCart();
saveCartToFirestore();
};
const deleteItem = (index) => {
// <<< CORE FIX: Gated function call
if (!isAuthResolved) {
alert('Loading...', 'Please wait for the page to finish loading user data.', 'info');
return;
}
cartItems.splice(index, 1);
renderCart();
saveCartToFirestore();
};
const addToCart = (product) => {
updateUserMetric('shoppingCartAdds');
if (!isAuthResolved) {
alert('Loading...', 'Please wait for the page to finish loading user data.', 'info');
return;
}
if (CURRENT_USER_ID === 'not-authenticated-user-placeholder') {
alert('Login Required', 'Please log in to add items to your persistent cart.', 'error');
return;
}
const existingItem = cartItems.find(item => item.name === product.name);
if (existingItem) {
existingItem.quantity++;
} else {
cartItems.push({
name: product.name,
price: product.price,
quantity: product.quantity || 1, // Use product quantity from modal if available
image: product.image,
selected: true,
});
}
updateCartCountDisplay();
// If the cart drawer is already open, re-render the contents
if (cartDrawer.classList.contains('open')) {
renderCart();
}
// Show a success notification instead of forcing the drawer open
window.alert('Item Added', `${product.name} has been added to your cart.`, 'success');
// Add the bounce animation to the cart icon for visual feedback
if (cartCountElement) {
cartCountElement.classList.add('bouncing');
setTimeout(() => {
cartCountElement.classList.remove('bouncing');
}, 600);
}
saveCartToFirestore();
};
// ===================================
// NEW PRODUCT DETAIL MODAL LOGIC (FIX)
// ===================================
const productDetailModal = document.getElementById('product-detail-modal');
const closeDetailModalBtn = document.getElementById('close-detail-modal-btn');
const detailProductName = document.getElementById('detail-product-name');
const detailPrice = document.getElementById('detail-price');
const detailRating = document.getElementById('detail-rating');
const detailDescription = document.getElementById('detail-description');
const detailMainImage = document.getElementById('detail-main-image');
const detailColorSelect = document.getElementById('detail-color-select');
const detailQtyInput = document.getElementById('detail-qty-input');
const detailQtyDecrease = document.getElementById('detail-qty-decrease');
const detailQtyIncrease = document.getElementById('detail-qty-increase');
const detailAddToCartBtn = document.getElementById('detail-add-to-cart-btn');
const detailBuyNowBtn = document.getElementById('detail-buy-now-btn');
const detailInStock = document.getElementById('detail-in-stock');
let currentProduct = null;
const closeProductDetailModal = () => {
productDetailModal.classList.remove('open');
currentProduct = null;
};
const renderProductDetail = (product) => {
currentProduct = product;
// FIX 1: Set a fallback name/title
const name = product.name || 'Unknown Product';
detailProductName.textContent = name;
document.getElementById('modal-product-name-mobile').textContent = name;
// FIX 2: Populate all other fields from the product object
detailPrice.textContent = `$${parseFloat(product.price || 0).toFixed(2)}`;
detailDescription.textContent = product.description || 'No detailed description available.';
detailMainImage.src = product.image || 'https://placehold.co/600x600/e2e8f0/64748b?text=No+Image';
// Rating
const ratingHtml = getRatingStars(product.rating || 0) +
`<span class="ml-2 text-text-muted text-sm">(${parseFloat(product.rating || 0).toFixed(1)})</span>` +
`<a href="#" class="ml-2 text-primary-blue hover:underline text-sm font-medium">View 345 Feedbacks</a>`;
detailRating.innerHTML = ratingHtml;
// Quantity and Stock
const stock = product.stock || 500;
detailInStock.textContent = `In stock: ${stock}`;
detailQtyInput.value = 1;
detailQtyInput.max = stock;
// Variations/Colors (Placeholder logic)
detailColorSelect.innerHTML = product.options && product.options.colors ?
product.options.colors.map(c => `<option>${c}</option>`).join('') :
'<option>Default</option>';
// Seller Info Placeholder
document.getElementById('detail-seller-property').textContent = product.property || 'No Property Info';
document.getElementById('detail-description-property').textContent = product.property || 'No Property Info';
// Open the modal
productDetailModal.classList.add('open');
};
const handleDetailAddToCart = () => {
if (!currentProduct) return;
const quantity = parseInt(detailQtyInput.value);
const selectedColor = detailColorSelect.value;
// Create a cart item object with the selected quantity and variation
const cartProduct = {
id: currentProduct.id,
name: `${currentProduct.name} - ${selectedColor}`, // Include variation in name for cart distinction
price: currentProduct.price,
image: currentProduct.image,
quantity: quantity
};
addToCart(cartProduct);
closeProductDetailModal();
};
// Attach listeners for modal quantity controls
const attachDetailModalListeners = () => {
closeDetailModalBtn.addEventListener('click', closeProductDetailModal);
productDetailModal.addEventListener('click', (event) => {
// Close modal if user clicks on the semi-transparent background (not the content)
if (event.target === productDetailModal) {
closeProductDetailModal();
}
});
detailQtyIncrease.addEventListener('click', () => {
let qty = parseInt(detailQtyInput.value);
const max = parseInt(detailQtyInput.max);
if (qty < max) {
detailQtyInput.value = qty + 1;
}
});
detailQtyDecrease.addEventListener('click', () => {
let qty = parseInt(detailQtyInput.value);
if (qty > 1) {
detailQtyInput.value = qty - 1;
}
});
detailAddToCartBtn.addEventListener('click', handleDetailAddToCart);
detailBuyNowBtn.addEventListener('click', () => {
// For simplicity, this acts like add to cart and then goes to checkout
handleDetailAddToCart();
openOrderReview();
});
};
// ===================================
// NEW FILTER MENU LOGIC
// ===================================
const menuButton = document.getElementById('menu-button');
const filterMenu = document.getElementById('filter-menu');
const closeMenuBtn = document.getElementById('close-menu-btn');
const cancelFiltersBtn = document.getElementById('cancel-filters-btn');
const applyFiltersBtn = document.getElementById('apply-filters-btn');
const toggleFilterMenu = () => {
filterMenu.classList.toggle('open');
backdrop.classList.toggle('open');
// Close cart if filter menu is opened
if (filterMenu.classList.contains('open') && cartDrawer.classList.contains('open')) {
cartDrawer.classList.remove('open');
}
// Close product detail modal if filter menu is opened
if (filterMenu.classList.contains('open') && productDetailModal.classList.contains('open')) {
closeProductDetailModal();
}
};
// ===================================
// ORDER REVIEW & TRACK ORDER LOGIC
// ===================================
const closeOrderReview = () => {
orderReviewScreen.classList.remove('open');
};
const closeTrackOrder = () => {
trackOrderScreen.classList.remove('open');
trackOrderScreen.innerHTML = '';
}
const renderTrackOrderScreen = () => {
const orderDetails = {
name: "Product Name Placeholder",
variation: "Large, Platinum Pink",
unitPrice: 150.00,
quantity: 1,
totalPrice: 249.00,
currentStep: 1,
steps: [{
icon: 'fas fa-shopping-cart',
label: 'Order Placed'
}, {
icon: 'fas fa-box-open',
label: 'Packing Order'
}, {
icon: 'fas fa-hands-helping',
label: 'Order Shipped'
}, {
icon: 'fas fa-truck',
label: 'Out for Delivery'
}, {
icon: 'fas fa-box',
label: 'Delivered'
}]
};
const currencySymbol = '₱';
const progressPercentage = ((orderDetails.currentStep - 1) / (orderDetails.steps.length - 1)) * 100;
let stepsHtml = orderDetails.steps.map((step, index) => {
const stepNumber = index + 1;
const isActive = stepNumber <= orderDetails.currentStep;
const iconClass = step.icon;
return `
<div class="progress-step w-1/5">
<div class="step-icon ${isActive ? 'active' : ''}">
<i class="${iconClass} text-lg"></i>
</div>
<span class="text-xs font-semibold ${isActive ? 'text-text-dark' : 'text-text-muted'}">${step.label}</span>
</div>
`;
}).join('');
const content = `
<div class="max-w-4xl mx-auto p-4 md:p-8">
<div class="flex justify-between items-center mb-6 border-b pb-4 bg-page-bg sticky top-0 z-10">
<h2 class="text-3xl font-extrabold text-primary-blue">Track Your Order</h2>
<button id="close-track-btn" class="text-text-muted hover:text-red-danger transition-colors text-2xl">
<i class="fas fa-times"></i>
</button>
</div>
<div class="bg-white rounded-xl shadow-lg p-6 mb-8 border border-gray-200">
<div class="relative flex justify-between items-start mb-12">
<div class="progress-bar-line">
<div class="progress-fill" style="width: ${progressPercentage}%;"></div>
</div>
${stepsHtml}
</div>
<div class="flex flex-col sm:flex-row border-t border-gray-100 pt-6">
<div class="flex items-start w-full sm:w-2/3 mb-6 sm:mb-0">
<div class="w-24 h-24 bg-gray-300 rounded-md flex-shrink-0 mr-4">
</div>
<div class="flex flex-col">
<span class="text-xl font-bold text-text-dark">${orderDetails.name}</span>
<span class="text-sm text-accent-orange mt-1">Variation: ${orderDetails.variation}</span>
</div>
</div>
<div class="w-full sm:w-1/3 flex flex-col space-y-2 text-sm text-right">
<div class="flex justify-between">
<span class="text-text-muted">Unit Price:</span>
<span class="font-medium">${currencySymbol}${orderDetails.unitPrice.toFixed(2)}</span>
</div>
<div class="flex justify-between">
<span class="text-text-muted">Quantity:</span>
<span class="font-medium">${orderDetails.quantity}</span>
</div>
<div class="flex justify-between items-center pt-2 border-t border-gray-100">
<span class="text-lg font-semibold">Total Price:</span>
<span class="text-2xl font-extrabold text-red-danger">${currencySymbol}${orderDetails.totalPrice.toFixed(2)}</span>
</div>
<span class="text-sm font-bold text-green-600">Cash On Delivery</span>
</div>
</div>
</div>
<div class="flex justify-between items-center p-4">
<button onclick="window.location.reload()" class="px-6 py-3 bg-primary-blue text-white font-semibold rounded-xl shadow-md hover:bg-blue-800 transition-colors">
Back to Shopping
</button>
<a href="message.html"
class="px-6 py-3 bg-white text-text-dark border border-gray-300 font-semibold rounded-xl shadow-sm hover:bg-gray-100 transition-colors">
Message Seller
</a>
</div>
</div>
`;
trackOrderScreen.innerHTML = content;
trackOrderScreen.classList.add('open');
document.getElementById('close-track-btn').addEventListener('click', closeTrackOrder);
};
const placeOrder = () => {
updateUserMetric('placeOrders');
console.log("Order Placed!");
// 1. Filter out purchased items
cartItems = cartItems.filter(item => !item.selected);
// 2. Save the new, smaller cart array to Firestore
saveCartToFirestore();
// 3. Update the displays
updateCartCountDisplay();
updateCartTotal();
renderCart();
// 4. Close the order review screen
closeOrderReview();
// 5. Show the NEW Track Order screen
renderTrackOrderScreen();
};
const renderOrderItem = (item) => {
const row = document.createElement('div');
const itemTotal = (item.price * item.quantity);
const currencySymbol = '₱';
row.className = 'bg-white rounded-lg shadow-sm flex flex-col p-4 mb-4 border border-gray-200';
row.innerHTML = `
<div class="grid grid-cols-4 md:grid-cols-6 lg:grid-cols-10 gap-2 items-center text-text-dark">
<div class="col-span-4 md:col-span-3 lg:col-span-5 flex items-start">
<img src="${item.image}" alt="${item.name}" class="w-16 h-16 object-cover rounded-md flex-shrink-0 mr-4">
<div class="flex flex-col">
<span class="font-bold text-lg leading-tight">${item.name}</span>
<span class="text-accent-orange text-sm mt-1">Variation: Default</span>
</div>
</div>
<div class="col-span-2 md:col-span-1 lg:col-span-2 text-center text-sm hidden sm:block">
<span class="text-text-muted text-xs block lg:hidden">Unit Price</span>
<span class="font-medium text-primary-blue mt-1">${currencySymbol}${item.price.toFixed(2)}</span>
</div>
<div class="col-span-2 md:col-span-1 lg:col-span-1 text-center text-sm hidden sm:block">
<span class="text-text-muted text-xs block lg:hidden">Qty</span>
<span class="font-medium mt-1">${item.quantity}</span>
</div>
<div class="col-span-4 md:col-span-1 lg:col-span-2 text-right">
<span class="font-extrabold text-red-danger text-xl">${currencySymbol}${itemTotal.toFixed(2)}</span>
</div>
<div class="col-span-4 sm:hidden flex flex-col text-sm text-text-muted mt-2">
<span>Unit: ${currencySymbol}${item.price.toFixed(2)} | Qty: ${item.quantity}</span>
</div>
</div>
`;
return row;
};
const openOrderReview = () => {
const selectedItems = cartItems.filter(item => item.selected);
if (selectedItems.length === 0) {
alert('No Items Selected', 'Please select at least one item to proceed to checkout.', 'error');
return;
}
if (CURRENT_USER_ID === 'not-authenticated-user-placeholder') {
// Essential check for placing an order
alert('Login Required', 'You must be logged in to proceed with checkout and order placement.', 'error');
return;
}
updateUserMetric('checkouts');
cartDrawer.classList.remove('open');
backdrop.classList.remove('open'); // Use generic backdrop
let merchandiseSubtotal = selectedItems.reduce((sum, item) => sum + (item.price * item.quantity), 0);
const totalPayment = merchandiseSubtotal + SHIPPING_SUBTOTAL;
const currencySymbol = '₱';
const content = `
<div class="max-w-4xl mx-auto p-4 md:p-8">
<div class="flex justify-between items-center mb-6 border-b pb-4 bg-page-bg sticky top-0 z-10">
<h2 class="text-3xl font-extrabold text-primary-blue">Review & Place Order</h2>
<button id="close-order-btn" class="text-text-muted hover:text-red-danger transition-colors text-2xl">
<i class="fas fa-times"></i>
</button>
</div>
<div class="bg-white rounded-xl shadow-lg p-6 mb-6">
<div class="flex justify-between items-center border-b pb-3 mb-3">
<div class="text-lg font-bold text-accent-orange flex items-center">
<i class="fas fa-map-marker-alt mr-2"></i>Delivery Address
</div>
<button class="text-primary-blue hover:text-accent-orange font-medium text-sm transition-colors">
Change Address
</button>
</div>
<p class="text-text-dark font-medium">John Doe</p>
<p class="text-text-muted text-sm">123 Main St, Apt 4B, Springfield, MA 01104, USA</p>
<p class="text-text-muted text-sm">(+1) 555-1234</p>
</div>
<h3 class="text-xl font-bold text-text-dark mb-4">Your Order(s):</h3>
<div class="hidden lg:grid grid-cols-10 gap-2 font-semibold text-text-muted text-sm border-b pb-2 px-4 mb-2">
<span class="col-span-5">Product Details</span>
<span class="col-span-2 text-center">Unit Price</span>
<span class="col-span-1 text-center">Quantity</span>
<span class="col-span-2 text-right">Total Price</span>
</div>
<div id="order-items-list" class="mb-8">
</div>
<div class="bg-white rounded-xl shadow-lg p-6 mb-8 flex flex-col lg:flex-row justify-between">
<div class="w-full lg:w-1/3 mb-6 lg:mb-0">
<h3 class="text-2xl font-bold text-text-dark mb-4">Payment Method</h3>
<div class="flex space-x-2">
<button class="px-4 py-2 bg-accent-orange text-white font-semibold rounded-lg shadow-md flex items-center">
<i class="fas fa-truck mr-2"></i> Cash On Delivery
</button>
<button class="px-4 py-2 bg-green-500/80 text-white font-semibold rounded-lg shadow-md hover:bg-green-600 transition-colors">
Discount %
</button>
</div>
</div>
<div class="w-full lg:w-1/2 flex flex-col items-end">
<div class="w-full max-w-sm space-y-2 text-text-dark mb-6">
<div class="flex justify-between">
<span class="text-text-muted">Merchandise Subtotal:</span>
<span class="font-medium">${currencySymbol}<span id="merchandise-subtotal">${merchandiseSubtotal.toFixed(2)}</span></span>
</div>
<div class="flex justify-between">
<span class="text-text-muted">Shipping Subtotal:</span>
<span class="font-medium">${currencySymbol}${SHIPPING_SUBTOTAL.toFixed(2)}</span>
</div>
<div class="flex justify-between items-center pt-2 border-t border-gray-200">
<span class="text-lg font-semibold">Total Payment:</span>
<span class="text-3xl font-extrabold text-red-danger">${currencySymbol}<span id="total-payment">${totalPayment.toFixed(2)}</span></span>
</div>
</div>
<button id="place-order-btn"
class="w-full max-w-sm py-3 bg-accent-orange text-white font-extrabold text-lg rounded-xl shadow-xl hover:bg-orange-600 transition-colors transform hover:scale-[1.01] active:scale-[0.99]">
PLACE ORDER
</button>
</div>
</div>
</div>
`;
orderReviewScreen.innerHTML = content;
const orderItemsList = document.getElementById('order-items-list');
selectedItems.forEach(item => {
orderItemsList.appendChild(renderOrderItem(item));
});
document.getElementById('close-order-btn').addEventListener('click', closeOrderReview);
document.getElementById('place-order-btn').addEventListener('click', placeOrder);
orderReviewScreen.classList.add('open');
};
const getRatingStars = (rating) => {
const fullStars = Math.floor(rating);
const halfStar = rating % 1 !== 0;
const emptyStars = 5 - fullStars - (halfStar ? 1 : 0);
let starsHtml = '';
for (let i = 0; i < fullStars; i++) {
starsHtml += `<i class="fas fa-star"></i>`;
}
if (halfStar) {
starsHtml += `<i class="fas fa-star-half-alt"></i>`;
}
for (let i = 0; i < emptyStars; i++) {
starsHtml += `<i class="far fa-star"></i>`;
}
return starsHtml;
};
const featuredProductsGrid = document.getElementById('featured-products-grid');
function renderProductCard(product) {
const card = document.createElement('div');
card.className =
'bg-light-bg rounded-xl shadow-lg overflow-hidden product-card hover:shadow-xl transition-shadow duration-300 cursor-pointer flex flex-col justify-between';
const price = parseFloat(product.price || 0).toFixed(2);
const rating = parseFloat(product.rating || 0).toFixed(1);
const description = product.description || 'No description provided.';
const image = product.image || 'https://placehold.co/400x300/e2e8f0/64748b?text=No+Image';
card.innerHTML = `
<div>
<img src="${image}" alt="${product.name}" class="w-full h-48 object-cover">
<div class="p-4">
<h3 class="text-lg font-bold text-text-dark truncate">${product.name}</h3>
<div class="flex items-center text-amber-500 text-sm mb-1">
${getRatingStars(product.rating || 0)}
<span class="ml-2 text-text-muted text-xs">(${rating})</span>
</div>
<p class="text-text-muted text-sm truncate">${description}</p>
<p class="text-xl font-bold text-primary-blue mt-2">$${price}</p>
</div>
</div>
<div class="p-4 pt-0">
<button class="add-to-cart-btn w-full px-4 py-2 bg-accent-orange text-white font-semibold rounded-lg shadow-md hover:bg-orange-600 transition-colors flex items-center justify-center space-x-2">
<i class="fas fa-cart-plus"></i>
<span>Add to cart</span>
</button>
</div>
`;
// Add listener to open the product detail modal on card click
card.addEventListener('click', (event) => {
// Only open the modal if the click target is NOT the Add to Cart button
if (!event.target.closest('.add-to-cart-btn')) {
renderProductDetail(product);
}
});
const addToCartButton = card.querySelector('.add-to-cart-btn');
if (addToCartButton) {
addToCartButton.addEventListener('click', (event) => {
event.stopPropagation();
addToCart({
id: product.id || product.name,
name: product.name,
price: parseFloat(price),
image: image
});
});
}
featuredProductsGrid.appendChild(card);
}
// ===================================
// PRODUCT FETCHING & CATEGORY LOGIC (UPDATED)
// ===================================
const productsRef = collection(db, 'artifacts', appId, 'public', 'data', 'products');
let unsubscribeProducts; // Variable to hold the unsubscribe function
const fetchProductsByCategory = (category) => {
// 1. Unsubscribe from the previous listener if it exists
if (unsubscribeProducts) {
unsubscribeProducts();
}
// 2. Build the new query
let productQuery;
if (category === 'All') {
productQuery = query(productsRef); // Query with no filter
} else {
// Query with a 'where' filter for the selected category
productQuery = query(productsRef, where('category', '==', category));
}
// 3. Set up the new listener
unsubscribeProducts = onSnapshot(productQuery, (querySnapshot) => {
featuredProductsGrid.innerHTML = '';
if (querySnapshot.empty) {
featuredProductsGrid.innerHTML =
'<p class="text-center text-text-muted col-span-full py-12">No products found in this category.</p>';
return;
}
querySnapshot.forEach((doc) => {
const product = doc.data();
product.id = doc.id;
renderProductCard(product);
});
}, (error) => {
console.error("Error fetching products:", error);
featuredProductsGrid.innerHTML =
'<p class="text-center text-red-danger col-span-full py-12">Failed to load products. Check console for details.</p>';
});
};
/**
* Handles the click event for category buttons.
* @param {Event} event The click event.
*/
const handleCategoryClick = (event) => {
const button = event.currentTarget;
const category = button.dataset.category;
// 1. Update active button styling
document.querySelectorAll('.category-button').forEach(btn => {
btn.classList.remove('active');
});
button.classList.add('active');
// 2. Fetch products for the new category
fetchProductsByCategory(category);
};
// --- Main Event Listener for DOM Ready ---
document.addEventListener('DOMContentLoaded', () => {
// ... (Carousel, Profile dropdown, Cart drawer listeners remain the same)
const slides = document.getElementById('carousel-slides');
const indicators = document.querySelectorAll('.carousel-indicator');
let currentIndex = 0;
const totalSlides = slides ? slides.children.length : 0;
if (slides && indicators.length > 0) {
const updateCarousel = () => {
const offset = -currentIndex * 100;
slides.style.transform = `translateX(${offset}%)`;
indicators.forEach((indicator, index) => {
if (index === currentIndex) {
indicator.classList.add('active');
indicator.style.backgroundColor = '#FF6B1F';
} else {
indicator.classList.remove('active');
indicator.style.backgroundColor = 'rgba(255, 255, 255, 0.7)';
}
});
};
const nextSlide = () => {
currentIndex = (currentIndex + 1) % totalSlides;
updateCarousel();
};
const prevSlide = () => {