forked from sagarkagi123/EliteCommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
301 lines (257 loc) · 9.43 KB
/
script.js
File metadata and controls
301 lines (257 loc) · 9.43 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
// STATE
let cart = [];
let isLoggedIn = false;
let currentUser = null;
let allProducts = [];
let currentFilter = 'all';
const validUsers = { 'admin': 'admin123' };
const products = [
{ id: 1, name: "Smart Watch Pro", price: 1999, originalPrice: 2999, emoji: "⌚", category: "electronics", badge: "Sale" },
{ id: 2, name: "Wireless Headphones", price: 899, originalPrice: 1299, emoji: "🎧", category: "electronics", badge: "New" },
{ id: 3, name: "Running Shoes", price: 1599, originalPrice: 2199, emoji: "👟", category: "fashion", badge: "Sale" },
{ id: 4, name: "Travel Backpack", price: 499, originalPrice: 799, emoji: "🎒", category: "accessories", badge: "" },
{ id: 5, name: "Sunglasses", price: 799, originalPrice: 1199, emoji: "🕶️", category: "accessories", badge: "New" },
{ id: 6, name: "Phone Case", price: 299, originalPrice: 499, emoji: "📱", category: "accessories", badge: "" },
{ id: 7, name: "Laptop Stand", price: 699, originalPrice: 999, emoji: "💻", category: "electronics", badge: "Sale" },
{ id: 8, name: "Water Bottle", price: 399, originalPrice: 599, emoji: "💧", category: "accessories", badge: "" },
{ id: 9, name: "Gaming Mouse", price: 1299, originalPrice: 1799, emoji: "🖱️", category: "electronics", badge: "New" }
];
allProducts = [...products];
// HELPERS
function showToast(message) {
const toast = document.getElementById('toast');
toast.textContent = message;
toast.classList.add('show');
setTimeout(() => toast.classList.remove('show'), 2500);
}
function showPage(pageName) {
document.querySelectorAll('.page').forEach(p => p.classList.remove('active'));
document.getElementById(pageName).classList.add('active');
document.querySelectorAll('.nav-link').forEach(link => {
const target = link.dataset.page;
link.classList.toggle('active', target === pageName);
});
document.getElementById("navMenu").classList.remove("show");
if (pageName === 'cart') renderCart();
}
function updateCartBadge() {
const total = cart.reduce((sum, item) => sum + item.quantity, 0);
document.getElementById('cartBadge').textContent = total;
}
function openLoginModal() {
document.getElementById('loginModal').style.display = 'block';
}
function closeLoginModal() {
document.getElementById('loginModal').style.display = 'none';
document.getElementById('username').value = '';
document.getElementById('password').value = '';
}
function updateProductButtons() {
document.querySelectorAll('.product-card button').forEach(button => {
if (!isLoggedIn) {
button.disabled = true;
button.textContent = 'Login to Shop';
} else {
button.disabled = false;
button.textContent = 'Add to Cart';
}
});
}
function login() {
const username = document.getElementById('username').value.trim();
const password = document.getElementById('password').value;
if (!username || !password) {
showToast('Please fill in all fields');
return;
}
if (validUsers[username] && validUsers[username] === password) {
isLoggedIn = true;
currentUser = username;
document.getElementById('userName').textContent = username;
document.getElementById('userNav').style.display = 'inline';
document.getElementById('loginLink').style.display = 'none';
closeLoginModal();
updateProductButtons();
showToast(`Welcome back, ${username}!`);
} else {
showToast('Invalid credentials!');
}
}
function logout() {
cart = [];
isLoggedIn = false;
currentUser = null;
document.getElementById('userNav').style.display = 'none';
document.getElementById('loginLink').style.display = 'inline';
updateCartBadge();
updateProductButtons();
if (document.getElementById('cart').classList.contains('active')) renderCart();
showToast('Logged out successfully');
}
function addToCart(productId) {
if (!isLoggedIn) {
openLoginModal();
return;
}
const product = products.find(p => p.id === productId);
const existing = cart.find(item => item.id === productId);
if (existing) {
existing.quantity++;
} else {
cart.push({ ...product, quantity: 1 });
}
updateCartBadge();
showToast(`${product.name} added!`);
}
function removeFromCart(productId) {
cart = cart.filter(item => item.id !== productId);
updateCartBadge();
renderCart();
}
function changeQty(productId, delta) {
const item = cart.find(i => i.id === productId);
if (!item) return;
item.quantity += delta;
if (item.quantity <= 0) {
cart = cart.filter(i => i.id !== productId);
}
updateCartBadge();
renderCart();
}
function renderCart() {
const cartItems = document.getElementById('cartItems');
const cartTotal = document.getElementById('cartTotal');
if (cart.length === 0) {
cartItems.innerHTML = '<div class="cart-empty"><h3>Your cart is empty</h3><p>Start shopping!</p></div>';
cartTotal.innerHTML = '';
return;
}
cartItems.innerHTML = cart.map(item => `
<div class="cart-item">
<div class="cart-item-info">
<span style="font-size: 50px;">${item.emoji}</span>
<div>
<h3>${item.name}</h3>
<p style="font-size: 14px; color:var(--text-secondary);">₹${item.price} each</p>
</div>
</div>
<div class="cart-controls">
<button onclick="changeQty(${item.id}, -1)">−</button>
<span>${item.quantity}</span>
<button onclick="changeQty(${item.id}, 1)">+</button>
</div>
<div style="text-align: right;">
<p style="color: var(--primary); font-weight: bold; font-size: 1.3em;">₹${item.price * item.quantity}</p>
<button class="remove-btn" onclick="removeFromCart(${item.id})">Remove</button>
</div>
</div>
`).join('');
const subtotal = cart.reduce((sum, item) => sum + (item.price * item.quantity), 0);
const savings = cart.reduce((sum, item) => sum + ((item.originalPrice - item.price) * item.quantity), 0);
cartTotal.innerHTML = `
<div class="cart-total">
<div class="total-row">
<span>Subtotal:</span>
<span>₹${subtotal}</span>
</div>
<div class="total-row" style="color: var(--success);">
<span>You Save:</span>
<span>₹${savings}</span>
</div>
<div class="total-final">
<span>Total:</span>
<span>₹${subtotal}</span>
</div>
<button class="btn" onclick="checkout()" style="margin-top: 20px; width: 100%;">Proceed to Checkout</button>
</div>
`;
}
function checkout() {
if (!isLoggedIn) {
showToast('Please login!');
openLoginModal();
return;
}
showToast(`Thank you ${currentUser}! Order placed successfully!`);
cart = [];
updateCartBadge();
showPage('home');
}
function renderProducts(productsToRender) {
const productList = document.getElementById('productList');
productList.innerHTML = productsToRender.map(p => `
<div class="product-card">
${p.badge ? `<div class="product-badge">${p.badge}</div>` : ''}
<span class="product-emoji">${p.emoji}</span>
<h3>${p.name}</h3>
<div class="product-price">
₹${p.price}
<span class="original-price">₹${p.originalPrice}</span>
</div>
<button onclick="addToCart(${p.id})">Add to Cart</button>
</div>
`).join('');
updateProductButtons();
}
function filterProducts(category) {
currentFilter = category;
document.querySelectorAll('.filter-btn').forEach(btn => {
btn.classList.toggle('active', btn.dataset.filter === category);
});
const filtered = category === 'all'
? allProducts
: allProducts.filter(p => p.category === category);
renderProducts(filtered);
}
function searchProducts() {
const query = document.getElementById('searchInput').value.toLowerCase().trim();
if (!query) {
renderProducts(allProducts);
return;
}
const results = allProducts.filter(p =>
p.name.toLowerCase().includes(query) ||
p.category.toLowerCase().includes(query)
);
renderProducts(results);
showToast(`Found ${results.length} products`);
}
// EVENT LISTENERS
document.addEventListener('DOMContentLoaded', () => {
// Navigation
document.getElementById('logoBtn').addEventListener('click', () => showPage('home'));
document.getElementById('menuBtn').addEventListener('click', () => {
document.getElementById('navMenu').classList.toggle('show');
});
document.querySelectorAll('.nav-link').forEach(link => {
link.addEventListener('click', () => {
const page = link.dataset.page;
if (page) showPage(page);
});
});
// Login/Logout
document.getElementById('loginLink').addEventListener('click', openLoginModal);
document.getElementById('closeModalBtn').addEventListener('click', closeLoginModal);
document.getElementById('loginBtn').addEventListener('click', login);
document.getElementById('logoutLink').addEventListener('click', logout);
// Shop button
document.getElementById('shopNowBtn').addEventListener('click', () => showPage('products'));
// Search
document.getElementById('searchBtn').addEventListener('click', searchProducts);
document.getElementById('searchInput').addEventListener('keypress', (e) => {
if (e.key === 'Enter') searchProducts();
});
// Filters
document.querySelectorAll('.filter-btn').forEach(btn => {
btn.addEventListener('click', () => {
filterProducts(btn.dataset.filter);
});
});
// Modal close on outside click
window.addEventListener('click', (e) => {
if (e.target === document.getElementById('loginModal')) closeLoginModal();
});
// Initialize
renderProducts(allProducts);
updateCartBadge();
});