File size: 1,993 Bytes
ac3b6d4
e15bd5e
 
ac3b6d4
e174490
e15bd5e
 
e174490
e15bd5e
 
 
 
 
 
 
 
 
 
 
 
3388264
 
e15bd5e
 
 
 
 
 
 
 
f290e66
e15bd5e
 
a5c6e85
 
 
 
 
e15bd5e
 
 
a5c6e85
 
e15bd5e
ac3b6d4
e15bd5e
 
 
 
 
 
 
 
 
 
ac3b6d4
 
e15bd5e
 
 
 
 
 
 
 
 
ac3b6d4
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
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 = `
    <i class="fas fa-check-circle mr-2"></i>
    <span>${message}</span>
  `;
  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;
  }
}