File size: 5,775 Bytes
3638da2
6e6888e
3638da2
6e6888e
 
3638da2
 
6e6888e
 
 
 
 
3638da2
 
6e6888e
 
 
 
 
 
3638da2
 
6e6888e
 
 
 
3638da2
 
6e6888e
 
 
3638da2
 
 
6e6888e
3638da2
 
6e6888e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3638da2
 
6e6888e
 
 
3638da2
 
6e6888e
 
 
 
 
 
 
 
 
 
 
 
 
3638da2
 
6e6888e
 
 
 
 
 
 
3638da2
 
 
 
6e6888e
 
 
 
3638da2
 
 
 
 
6e6888e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

// Auth functions
window.auth = {
    currentUser: JSON.parse(localStorage.getItem('currentUser')) || null,

    signup: function(name, email, password) {
const user = { name, email };
        localStorage.setItem('currentUser', JSON.stringify(user));
        auth.currentUser = user;
        showToast('Account created successfully!');
        window.location.href = '/';
    },
    login: function(email, password) {
// In a real app, this would verify against a backend
        const user = { name: 'Demo User', email };
        localStorage.setItem('currentUser', JSON.stringify(user));
        auth.currentUser = user;
        showToast('Logged in successfully!');
        window.location.href = '/';
    },
    logout: function() {
localStorage.removeItem('currentUser');
        auth.currentUser = null;
        showToast('Logged out successfully!');
        window.location.href = '/';
    },
    isAuthenticated: function() {
return auth.currentUser !== null;
    }
};
// Cart functions
window.cart = {
    getCart: function() {
return JSON.parse(localStorage.getItem('cart')) || [];
    },
    addToCart: function(product) {
if (!auth.isAuthenticated()) {
            window.location.href = '/login';
            return;
        }

        const cartItems = cart.getCart();
        const existingItem = cartItems.find(item => item.id === product.id);

        if (existingItem) {
            existingItem.quantity += 1;
        } else {
            cartItems.push({
                ...product,
                quantity: 1
            });
        }

        localStorage.setItem('cart', JSON.stringify(cartItems));
        updateCartCount();
        showToast(`${product.name} added to cart!`);
    },
    removeFromCart: function(productId) {
const cartItems = cart.getCart().filter(item => item.id !== productId);
        localStorage.setItem('cart', JSON.stringify(cartItems));
        updateCartCount();
    },
    updateQuantity: function(productId, newQuantity) {
const cartItems = cart.getCart();
        const item = cartItems.find(item => item.id === productId);
        
        if (item) {
            if (newQuantity <= 0) {
                cart.removeFromCart(productId);
            } else {
                item.quantity = newQuantity;
                localStorage.setItem('cart', JSON.stringify(cartItems));
            }
        }
        
        updateCartCount();
    },
    clearCart: function() {
localStorage.removeItem('cart');
        updateCartCount();
    }
};

// Helper functions
function updateCartCount() {
    const cartCount = document.getElementById('cart-count');
    const mobileCartCount = document.getElementById('mobile-cart-count');
    
    const count = window.cart.getCart().reduce((sum, item) => sum + item.quantity, 0);
    
    if (cartCount) {
        cartCount.textContent = count;
        cartCount.style.display = count > 0 ? 'flex' : 'none';
    }
    
    if (mobileCartCount) {
        mobileCartCount.textContent = count;
        mobileCartCount.style.display = count > 0 ? 'flex' : 'none';
    }
}
function showToast(message) {
    const toast = document.createElement('div');
    toast.className = 'toast';
    toast.textContent = message;
    document.body.appendChild(toast);
    
    setTimeout(() => {
        toast.remove();
    }, 3000);
}

// Load featured products
document.addEventListener('DOMContentLoaded', () => {
    const featuredContainer = document.getElementById('featured-products');
    
    if (featuredContainer) {
        const products = [
            {
                id: 1,
                name: 'Wireless Headphones',
                price: 99.99,
                image: 'http://static.photos/technology/320x240/1',
                description: 'Premium wireless headphones with noise cancellation'
            },
            {
                id: 2,
                name: 'Smart Watch',
                price: 199.99,
                image: 'http://static.photos/technology/320x240/2',
                description: 'Track your fitness and stay connected'
            },
            {
                id: 3,
                name: 'Bluetooth Speaker',
                price: 79.99,
                image: 'http://static.photos/technology/320x240/3',
                description: 'Portable speaker with crystal clear sound'
            },
            {
                id: 4,
                name: 'Wireless Charger',
                price: 29.99,
                image: 'http://static.photos/technology/320x240/4',
                description: 'Fast charging for all Qi-enabled devices'
            }
        ];

        products.forEach(product => {
            const productCard = document.createElement('div');
            productCard.className = 'product-card bg-white rounded-lg overflow-hidden shadow-md';
            productCard.innerHTML = `
                <img src="${product.image}" alt="${product.name}" class="w-full h-48 object-cover">
                <div class="p-4">
                    <h3 class="font-semibold text-lg mb-1">${product.name}</h3>
                    <p class="text-gray-600 text-sm mb-2">${product.description}</p>
                    <div class="flex justify-between items-center mt-4">
                        <span class="font-bold text-primary">$${product.price.toFixed(2)}</span>
                        <button onclick="cart.addToCart(${JSON.stringify(product).replace(/"/g, '&quot;')})" 
                                class="bg-primary text-white px-4 py-2 rounded hover:bg-primary-600 transition">
                            Add to Cart
                        </button>
                    </div>
                </div>
            `;
            featuredContainer.appendChild(productCard);
        });
    }

    updateCartCount();
});