document.addEventListener('DOMContentLoaded', () => { // Initialize cart count updateCartCount(); }); // Cart management functions let cart = JSON.parse(localStorage.getItem('bizrah-cart')) || []; function addToCart(productId) { const existingItem = cart.find(item => item.id === productId); if (existingItem) { existingItem.quantity += 1; } else { cart.push({ id: productId, quantity: 1 }); } localStorage.setItem('bizrah-cart', JSON.stringify(cart)); updateCartCount(); showToast('Product added to cart!'); } function updateCartCount() { const countElements = document.querySelectorAll('.cart-count'); const totalItems = cart.reduce((sum, item) => sum + item.quantity, 0); countElements.forEach(el => { el.textContent = totalItems; el.style.display = totalItems > 0 ? 'flex' : 'none'; }); } function showToast(message) { const toast = document.createElement('div'); toast.className = 'fixed bottom-4 right-4 bg-green-600 text-white px-4 py-2 rounded-lg shadow-lg flex items-center'; toast.innerHTML = ` ${message} `; document.body.appendChild(toast); setTimeout(() => { toast.classList.add('opacity-0', 'transition-opacity', 'duration-300'); setTimeout(() => toast.remove(), 300); }, 3000); } // API functions async function fetchProducts(category = '') { try { const response = await fetch(`/api/products?category=${category}`); if (!response.ok) throw new Error('Failed to fetch products'); return await response.json(); } catch (error) { console.error('Error fetching products:', error); return []; } } async function fetchProductDetails(id) { try { const response = await fetch(`/api/products/${id}`); if (!response.ok) throw new Error('Failed to fetch product details'); return await response.json(); } catch (error) { console.error('Error fetching product details:', error); return null; } }