Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
480 lines (448 loc) · 16.5 KB
/
Copy pathscript.js
File metadata and controls
480 lines (448 loc) · 16.5 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
const productsData = {
"1": { name: "مجلسی فول کاردست", price: "۱۲,۵۰۰,۰۰۰ تومان", fabric: "فول کریستال عربی", size: "50" },
"2": { name: "مجلسی فول کاردست لوکس", price: "۱۴,۰۰۰,۰۰۰ تومان", fabric: "فول کریستال عربی", size: "50" },
"3": { name: "مجلسی فول کاردست کلاسیک", price: "۱۱,۲۰۰,۰۰۰ تومان", fabric: "فول کریستال عربی", size: "52" },
"5": { name: "مجلسی پنل کاردست", price: "۹,۸۰۰,۰۰۰ تومان", fabric: "کرپ مشکی پنل کاردست", size: "46" },
"6": { name: "شنل مشکی کاردست", price: "۷,۵۰۰,۰۰۰ تومان", fabric: "تور سنگ دوزی", size: "فری سایز" },
"7": { name: "مجلسی تمام کاردست پرنسسی دودامن", price: "۱۸,۹۰۰,۰۰۰ تومان", fabric: "دو دامن فول کار دست ", size: "48" },
"8": { name: "مجلسی فول کاردست", price: "۱۳,۴۰۰,۰۰۰ تومان", fabric: " فول کریستال گلبهی ", size: "52" },
"9": { name: "مجلسی کاردست پنل", price: "۱۰,۵۰۰,۰۰۰ تومان", fabric: "کرپ مشکی پنل کار دست", size: "48" },
"10": { name: "مجلسی فول کاردست", price: "۱۵,۰۰۰,۰۰۰ تومان", fabric: "تور کاردست سنگین عربی", size: "46" }
};
let cart = [];
let wishlist = [];
let currentProductId = null;
document.querySelectorAll('.details-btn').forEach(button => {
button.addEventListener('click', function() {
const id = this.getAttribute('data-id');
const product = productsData[id];
if(product) {
currentProductId = id;
document.getElementById('modalTitle').innerText = product.name;
document.getElementById('modalPrice').innerText = product.price;
document.getElementById('modalFabric').innerText = product.fabric;
document.getElementById('modalOccasion').innerText = product.size;
const productModal = new bootstrap.Modal(document.getElementById('productModal'));
productModal.show();
}
});
});
function updateUI() {
document.getElementById('cartCount').innerText = cart.length;
document.getElementById('wishlistCount').innerText = wishlist.length;
const cartBody = document.getElementById('cartModalBody');
if(cart.length === 0) {
cartBody.innerHTML = '<p class="text-center text-muted py-4">سبد خرید شما خالی است.</p>';
} else {
let html = '<ul class="list-group list-group-flush">';
cart.forEach((item, index) => {
html += `
<li class="list-group-item d-flex justify-content-between align-items-center bg-transparent border-bottom py-3">
<div style="text-align: right;">
<h6 class="mb-0 text-dark">${item.name}</h6>
<small class="text-muted">${item.price}</small>
</div>
<button class="btn btn-sm btn-outline-danger border-0" onclick="removeFromCart(${index})">
<i class="fa fa-trash"></i>
</button>
</li>
`;
});
html += '</ul>';
cartBody.innerHTML = html;
}
const wishlistBody = document.getElementById('wishlistModalBody');
if(wishlist.length === 0) {
wishlistBody.innerHTML = '<p class="text-center text-muted py-4">لیست علاقهمندیهای شما خالی است.</p>';
} else {
let html = '<ul class="list-group list-group-flush">';
wishlist.forEach((item, index) => {
html += `
<li class="list-group-item d-flex justify-content-between align-items-center bg-transparent border-bottom py-3">
<div style="text-align: right;">
<h6 class="mb-0 text-dark">${item.name}</h6>
<small class="text-muted">${item.price}</small>
</div>
<button class="btn btn-sm btn-outline-danger border-0" onclick="removeFromWishlist(${index})">
<i class="fa fa-trash"></i>
</button>
</li>
`;
});
html += '</ul>';
wishlistBody.innerHTML = html;
}
}
window.removeFromCart = function(index) {
cart.splice(index, 1);
updateUI();
}
window.removeFromWishlist = function(index) {
wishlist.splice(index, 1);
updateUI();
}
// کدهای اسلایدر قبلی شما
let currentSlide = 0;
const slides = document.querySelectorAll('.slide');
window.showSlide = function(index) {
slides.forEach(slide => slide.classList.remove('active'));
if (index >= slides.length) currentSlide = 0;
if (index < 0) currentSlide = slides.length - 1;
slides[currentSlide].classList.add('active');
}
window.nextSlide = function() {
currentSlide++;
showSlide(currentSlide);
}
window.prevSlide = function() {
currentSlide--;
showSlide(currentSlide);
}
updateUI();
function updateUI(){
document.getElementById('cartCount').innerText = cart.length;
document.getElementById('wishlistCount').innerText = wishlist.length;
// ===== سبد خرید =====
const cartBody = document.getElementById('cartModalBody');
if(cart.length === 0){
cartBody.innerHTML = '<p class="text-center text-muted py-4">سبد خرید شما خالی است.</p>';
}else{
let html = '<ul class="list-group list-group-flush">';
cart.forEach((item,index)=>{
const img = `image/${item.id}.jpg`;
html += `
<li class="list-group-item d-flex align-items-center justify-content-between">
<img src="${img}" style="width:60px;height:60px;object-fit:cover;border-radius:8px">
<div style="flex:1;text-align:right;margin-right:10px">
<h6 class="mb-0">${item.name}</h6>
<small class="text-muted">${item.price}</small>
</div>
<button class="btn btn-sm btn-outline-danger border-0" onclick="removeFromCart(${index})">
<i class="fa fa-trash"></i>
</button>
</li>
`;
});
html += '</ul>';
cartBody.innerHTML = html;
}
// ===== علاقه مندی =====
const wishlistBody = document.getElementById('wishlistModalBody');
if(wishlist.length === 0){
wishlistBody.innerHTML = '<p class="text-center text-muted py-4">لیست علاقهمندیهای شما خالی است.</p>';
}else{
let html = '<ul class="list-group list-group-flush">';
wishlist.forEach((item,index)=>{
const img = `image/${item.id}.jpg`;
html += `
<li class="list-group-item d-flex align-items-center justify-content-between">
<img src="${img}" style="width:60px;height:60px;object-fit:cover;border-radius:8px">
<div style="flex:1;text-align:right;margin-right:10px">
<h6 class="mb-0">${item.name}</h6>
<small class="text-muted">${item.price}</small>
</div>
<button class="btn btn-sm btn-dark" onclick="moveToCart(${index})">
خرید
</button>
<button class="btn btn-sm btn-outline-danger border-0" onclick="removeFromWishlist(${index})">
<i class="fa fa-trash"></i>
</button>
</li>
`;
});
html += '</ul>';
wishlistBody.innerHTML = html;
}
}
function moveToCart(index){
const item = wishlist[index];
if(!cart.some(product => product.id === item.id)){
cart.push(item);
}
wishlist.splice(index,1);
updateUI();
}
// سرچ هوشمند
const searchInput = document.getElementById("searchInput");
if (searchInput) {
searchInput.addEventListener("input", function() {
const filter = this.value.trim().toLowerCase();
// انتخاب تمام کالاهای گالری
const galleryItems = document.querySelectorAll(".gallery-item");
galleryItems.forEach(item => {
// متن عنوان لباس رو میگیره
const title = item.querySelector(".dress-title").innerText.toLowerCase();
// اگه عنوان لباس شامل کلمه سرچ شده باشه، نمایش میده، وگرنه مخفی میکنه
if (title.includes(filter)) {
item.parentElement.style.display = "block"; // نمایش کارت
} else {
item.parentElement.style.display = "none"; // مخفی کردن کارت
}
});
});
}
// ===== گرفتن کاربران =====
function getUsers(){
return JSON.parse(localStorage.getItem("users")) || [];
}
// ===== ذخیره کاربران =====
function saveUsers(users){
localStorage.setItem("users", JSON.stringify(users));
}
// ===== گرفتن کاربر فعلی =====
function getCurrentUser(){
return JSON.parse(localStorage.getItem("currentUser"));
}
// ===== آپدیت اطلاعات کاربر =====
function updateUserData(updatedUser){
let users = getUsers();
users = users.map(user => {
if(user.email === updatedUser.email){
return updatedUser;
}
return user;
});
saveUsers(users);
localStorage.setItem("currentUser", JSON.stringify(updatedUser));
}
// ===== ثبت نام =====
document.getElementById("registerForm").addEventListener("submit", function(e){
e.preventDefault();
const name = this.querySelector('input[type="text"]').value;
const email = this.querySelector('input[type="email"]').value;
const password = this.querySelector('input[type="password"]').value;
let users = getUsers();
let exist = users.find(u => u.email === email);
if(exist){
alert("این ایمیل قبلا ثبت شده");
return;
}
let newUser = {
name:name,
email:email,
password:password,
cart:[],
wishlist:[]
};
users.push(newUser);
saveUsers(users);
alert("ثبت نام موفق ✅");
this.reset();
});
// ===== ورود =====
document.getElementById("loginForm").addEventListener("submit", function(e){
e.preventDefault();
const email = this.querySelector('input[type="text"]').value;
const password = this.querySelector('input[type="password"]').value;
let users = getUsers();
let user = users.find(u => u.email === email && u.password === password);
if(user){
localStorage.setItem("currentUser", JSON.stringify(user));
updateCartCount();
updateWishlistCount();
updateUserUI();
alert("ورود موفق ✅");
this.reset();
}else{
alert("اطلاعات اشتباه است");
}
});
// ===== افزودن به سبد خرید =====
function addToCart(productId){
let user = getCurrentUser();
if(!user){
alert("ابتدا وارد حساب شوید");
return;
}
user.cart.push(productId);
updateUserData(user);
updateCartCount();
alert("به سبد خرید اضافه شد");
}
// ===== افزودن به علاقه مندی =====
function addToWishlist(productId){
let user = getCurrentUser();
if(!user){
alert("ابتدا وارد حساب شوید");
return;
}
user.wishlist.push(productId);
updateUserData(user);
updateWishlistCount();
alert("به علاقه مندی اضافه شد");
}
// ===== تعداد سبد خرید =====
function updateCartCount(){
let user = getCurrentUser();
if(!user){
document.getElementById("cartCount").innerText = 0;
return;
}
document.getElementById("cartCount").innerText = user.cart.length;
}
// ===== تعداد علاقه مندی =====
function updateWishlistCount(){
let user = getCurrentUser();
if(!user){
document.getElementById("wishlistCount").innerText = 0;
return;
}
document.getElementById("wishlistCount").innerText = user.wishlist.length;
}
// ===== نمایش سبد خرید =====
function renderCart(){
let user = getCurrentUser();
const cartBody = document.getElementById("cartModalBody");
if(!user || user.cart.length === 0){
cartBody.innerHTML = "سبد خرید خالی است";
return;
}
let html = "";
user.cart.forEach(id => {
html += `<p>محصول شماره ${id}</p>`;
});
cartBody.innerHTML = html;
}
// ===== نمایش علاقه مندی =====
function renderWishlist(){
let user = getCurrentUser();
const body = document.getElementById("wishlistModalBody");
if(!user || user.wishlist.length === 0){
body.innerHTML = "لیست خالی است";
return;
}
let html = "";
user.wishlist.forEach(id => {
html += `<p>محصول شماره ${id}</p>`;
});
body.innerHTML = html;
}
// ===== اتصال دکمه ها =====
document.addEventListener("DOMContentLoaded", function(){
updateCartCount();
updateWishlistCount();
let cartModal = document.getElementById("cartModal");
cartModal.addEventListener("show.bs.modal", renderCart);
let wishlistModal = document.getElementById("wishlistModal");
wishlistModal.addEventListener("show.bs.modal", renderWishlist);
});
// ===== اتصال دکمه های محصولات =====
document.querySelectorAll(".details-btn").forEach(btn=>{
btn.addEventListener("click",function(){
let id = this.dataset.id;
document.getElementById("addToCartBtn").onclick=function(){
addToCart(id);
}
document.getElementById("addToWishlistBtn").onclick=function(){
addToWishlist(id);
}
});
});
// تبدیل قیمت فارسی به عدد
function priceToNumber(priceText){
let english = priceText
.replace(/,/g,"")
.replace(" تومان","")
.replace(/[۰-۹]/g, d => "۰۱۲۳۴۵۶۷۸۹".indexOf(d));
return parseInt(english);
}
const priceRange = document.getElementById("priceRange");
const priceValue = document.getElementById("priceValue");
if(priceRange){
priceRange.addEventListener("input", function(){
let maxPrice = parseInt(this.value);
priceValue.innerText = maxPrice.toLocaleString("fa-IR");
document.querySelectorAll(".gallery-item").forEach(item=>{
let id = item.querySelector(".details-btn").dataset.id;
let product = productsData[id];
let priceNumber = priceToNumber(product.price);
if(priceNumber <= maxPrice){
item.parentElement.style.display = "block";
}else{
item.parentElement.style.display = "none";
}
});
});
}
// نمایش وضعیت کاربر
function updateUserUI(){
let user = getCurrentUser();
const userBox = document.querySelector(".user-box");
const loginBtn = document.querySelector(".btn-login");
const signBtn = document.querySelector(".btn-sign");
const userName = document.getElementById("userName");
if(user){
userBox.style.display = "flex";
loginBtn.style.display = "none";
signBtn.style.display = "none";
userName.innerText = user.name;
}else{
userBox.style.display = "none";
loginBtn.style.display = "inline-block";
signBtn.style.display = "inline-block";
}
}
function logout(){
localStorage.removeItem("currentUser");
cart = [];
wishlist = [];
updateCartCount();
updateWishlistCount();
updateUserUI();
alert("از حساب خارج شدید");
}
// ===== اصلاح نمایش سبد خرید =====
function renderCart(){
let user = getCurrentUser();
const cartBody = document.getElementById("cartModalBody");
if(!user || user.cart.length === 0){
cartBody.innerHTML = '<p class="text-center text-muted">سبد خرید خالی است</p>';
return;
}
let html = '<ul class="list-group list-group-flush">';
user.cart.forEach(id => {
let product = productsData[id];
if(!product) return;
let img = `image/${id}.jpg`;
html += `
<li class="list-group-item d-flex align-items-center justify-content-between">
<img src="${img}" style="width:60px;height:60px;object-fit:cover;border-radius:8px">
<div style="flex:1;text-align:right;margin-right:10px">
<h6 class="mb-0">${product.name}</h6>
<small class="text-muted">${product.price}</small>
</div>
</li>
`;
});
html += "</ul>";
cartBody.innerHTML = html;
}
// ===== اصلاح نمایش علاقه مندی =====
function renderWishlist(){
let user = getCurrentUser();
const body = document.getElementById("wishlistModalBody");
if(!user || user.wishlist.length === 0){
body.innerHTML = '<p class="text-center text-muted">لیست خالی است</p>';
return;
}
let html = '<ul class="list-group list-group-flush">';
user.wishlist.forEach(id => {
let product = productsData[id];
if(!product) return;
let img = `image/${id}.jpg`;
html += `
<li class="list-group-item d-flex align-items-center justify-content-between">
<img src="${img}" style="width:60px;height:60px;object-fit:cover;border-radius:8px">
<div style="flex:1;text-align:right;margin-right:10px">
<h6 class="mb-0">${product.name}</h6>
<small class="text-muted">${product.price}</small>
</div>
<button onclick="addToCart('${id}')" class="btn btn-dark btn-sm">
خرید
</button>
</li>
`;
});
html += "</ul>";
body.innerHTML = html;
}