/** * Store Main JavaScript * جميع وظائف المتجر */ // Add to Cart Function function addToCart(productId, quantity = 1) { // فحص تسجيل الدخول أولاً fetch('../api/check-login.php') .then(r => r.json()) .then(loginData => { if (!loginData.logged_in) { // المستخدم غير مسجل دخول showToast('⚠️ يجب تسجيل الدخول أولاً!', 'warning'); setTimeout(() => { window.location.href = 'login.php?redirect=' + encodeURIComponent(window.location.href); }, 1500); return; } // المستخدم مسجل دخول - أضف للسلة fetch('../api/cart/add.php', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'X-Requested-With': 'XMLHttpRequest' }, body: `product_id=${productId}&quantity=${quantity}` }) .then(r => r.json()) .then(data => { if (data.success) { showToast('✓ تم إضافة المنتج للسلة'); updateCartCount(data.cart_count || 0); } else { showToast('⚠ ' + (data.message || 'حدث خطأ')); } }) .catch(err => { console.error('Error:', err); showToast('❌ حدث خطأ في الاتصال'); }); }) .catch(err => { console.error('Login check error:', err); showToast('❌ حدث خطأ في التحقق من تسجيل الدخول'); }); } // Add to Wishlist Function function addToWishlist(productId) { fetch('../api/wishlist/add.php', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'X-Requested-With': 'XMLHttpRequest' }, body: `product_id=${productId}` }) .then(r => r.json()) .then(data => { if (data.success) { showToast('💜 تم إضافة المنتج للمفضلة'); updateWishlistCount(data.wishlist_count || 0); } else { showToast('⚠ ' + (data.message || 'حدث خطأ')); } }) .catch(err => { console.error('Error:', err); showToast('❌ حدث خطأ في الاتصال'); }); } // Toggle Wishlist Function function toggleWishlist(productId) { const btn = document.querySelector(`[data-product-id="${productId}"]`); const svg = btn ? btn.querySelector('svg') : null; const path = svg ? svg.querySelector('path') : null; // Add loading animation if (btn) { btn.style.pointerEvents = 'none'; if (svg) svg.style.transform = 'scale(0.8)'; } fetch('../api/wishlist/toggle.php', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'X-Requested-With': 'XMLHttpRequest' }, body: `product_id=${productId}` }) .then(r => r.json()) .then(data => { if (data.success) { // Animate heart if (svg) { svg.style.transform = 'scale(1.3)'; setTimeout(() => { svg.style.transform = 'scale(1)'; }, 200); } // Update state if (btn && path) { if (data.in_wishlist) { path.setAttribute('fill', '#E57393'); path.setAttribute('stroke', '#E57393'); btn.classList.add('active'); } else { path.setAttribute('fill', 'none'); path.setAttribute('stroke', 'currentColor'); btn.classList.remove('active'); } } const message = data.action === 'added' ? '💜 تم إضافة المنتج للمفضلة' : '🗑️ تم إزالة المنتج من المفضلة'; showToast(message); updateWishlistCount(data.wishlist_count || 0); } else { showToast('⚠ ' + (data.message || 'حدث خطأ')); // If not logged in, show login modal if available if (data.message && data.message.includes('تسجيل الدخول') && typeof showLoginModal === 'function') { showLoginModal(); } } }) .catch(err => { console.error('Error:', err); showToast('❌ حدث خطأ في الاتصال'); }) .finally(() => { if (btn) btn.style.pointerEvents = 'auto'; }); } // Update Cart Count function updateCartCount(count) { // Use only specific cart badge selectors const badges = document.querySelectorAll('.cart-badge, #cartBadge'); badges.forEach(badge => { if (count > 0) { badge.textContent = count; badge.style.display = 'flex'; } else { badge.style.display = 'none'; } }); } // Update Wishlist Count function updateWishlistCount(count) { const badges = document.querySelectorAll('.wishlist-badge, #wishlistBadge'); badges.forEach(badge => { if (count > 0) { badge.textContent = count; badge.style.display = 'flex'; } else { badge.style.display = 'none'; } }); } // Show Toast Notification function showToast(message, type = 'success', duration = 3000) { let toast = document.getElementById('toast'); if (!toast) { toast = document.createElement('div'); toast.id = 'toast'; toast.className = 'toast-notification'; document.body.appendChild(toast); } // تحديد اللون حسب النوع let bgColor = 'linear-gradient(135deg, #E57393 0%, #D1537A 100%)'; // success if (type === 'warning') { bgColor = 'linear-gradient(135deg, #f59e0b 0%, #d97706 100%)'; // orange } else if (type === 'error') { bgColor = 'linear-gradient(135deg, #ef4444 0%, #dc2626 100%)'; // red } toast.style.cssText = ` position: fixed; top: 20px; right: 20px; background: ${bgColor}; color: white; padding: 16px 24px; border-radius: 12px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3); z-index: 9999; font-weight: 600; font-size: 16px; animation: slideIn 0.3s ease-out; `; toast.textContent = message; toast.style.display = 'block'; setTimeout(() => { toast.style.display = 'none'; }, duration); } // Load Cart Count from Server function loadCartCount() { fetch('../api/cart/get-count.php') .then(r => r.json()) .then(data => { if (data.success) { updateCartCount(data.count); } }) .catch(err => console.error('Error loading cart count:', err)); } // Load Wishlist Count from Server function loadWishlistCount() { fetch('../api/wishlist/count.php') .then(r => r.json()) .then(data => { if (data.success) { updateWishlistCount(data.count); } }) .catch(err => console.error('Error loading wishlist count:', err)); } // Initialize on page load document.addEventListener('DOMContentLoaded', function() { console.log('✓ Store JS loaded'); // Load counts on page load loadCartCount(); loadWishlistCount(); });