File size: 11,040 Bytes
b2a399d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// TechSphere Marketplace JavaScript
                    document.addEventListener('DOMContentLoaded', function() {
                        // Initialize all components
                        initializeSearch();
                        initializeFilters();
                        initializeCart();
                        loadProducts();
                    });
                    
                    // Search functionality
                    function initializeSearch() {
                        const searchInput = document.querySelector('input[type="search"]');
                        if (searchInput) {
                            let searchTimeout;
                            searchInput.addEventListener('input', function() {
                                clearTimeout(searchTimeout);
                                searchTimeout = setTimeout(() => {
                                    const searchTerm = this.value.toLowerCase();
                                    filterProducts(searchTerm);
                                }, 300);
                            }
                        }
                    
                        // Filter products based on search term
                        function filterProducts(searchTerm) {
                            const products = document.querySelectorAll('.product-card');
                            let visibleCount = 0;
                    
                            products.forEach(product => {
                                const title = product.querySelector('h3').textContent.toLowerCase();
                                const description = product.querySelector('p').textContent.toLowerCase();
                    
                                if (title.includes(searchTerm) || description.includes(searchTerm)) {
                                    product.style.display = 'block';
                                    visibleCount++;
                                } else {
                                    product.style.display = 'none';
                                }
                            });
                    
                            // Update results count
                            const resultsElement = document.querySelector('#resultsCount');
                            if (resultsElement) {
                                resultsElement.textContent = `Showing ${visibleCount} products`;
                            }
                        }
                    
                        // Initialize filter functionality
                        function initializeFilters() {
                            const filterButtons = document.querySelectorAll('.filter-btn');
                            filterButtons.forEach(button => {
                                button.addEventListener('click', function() {
                                    // Remove active class from all buttons
                                    filterButtons.forEach(btn => btn.classList.remove('active'));
                                    // Add active class to clicked button
                                    this.classList.add('active');
                                    applyFilters();
                                });
                            }
                    
                            // Price filter
                            const priceFilter = document.querySelector('#priceFilter');
                            if (priceFilter) {
                                priceFilter.addEventListener('change', applyFilters);
                            }
                        }
                    
                        function applyFilters() {
                            const activeFilter = document.querySelector('.filter-btn.active');
                            const priceRange = document.querySelector('#priceFilter')?.value;
                    
                            // Apply both category and price filters
                            const products = document.querySelectorAll('.product-card');
                            let visibleCount = 0;
                    
                            products.forEach(product => {
                                const price = parseFloat(product.querySelector('.price').textContent.replace('$', ''));
                            let shouldShow = true;
                    
                            if (priceRange) {
                                const [min, max] = priceRange.split('-').map(Number);
                                const currentPrice = parseFloat(product.querySelector('.price').textContent.replace('$', ''));
                    
                                if (priceRange === 'under50' && currentPrice > 50) {
                                    shouldShow = false;
                                } else if (priceRange === '50-100' && (currentPrice < 50 || currentPrice > 100)) {
                                    shouldShow = false;
                                } else if (priceRange === '100-200' && (currentPrice < 100 || currentPrice > 200)) {
                                    shouldShow = false;
                                } else if (priceRange === 'over200' && currentPrice < 200) {
                                    shouldShow = false;
                                }
                            }
                    
                            if (shouldShow) {
                                product.style.display = 'block';
                                visibleCount++;
                            } else {
                                product.style.display = 'none';
                            }
                        });
                    
                        // Update results count
                        const resultsElement = document.querySelector('#resultsCount');
                        if (resultsElement) {
                            resultsElement.textContent = `Showing ${visibleCount} products`;
                            }
                        }
                    
                        // Cart functionality
                        function initializeCart() {
                            let cart = JSON.parse(localStorage.getItem('techsphere_cart')) || [];
                    
                            // Add to cart buttons
                            const addToCartButtons = document.querySelectorAll('.add-to-cart');
                            addToCartButtons.forEach(button => {
                            button.addEventListener('click', function() {
                                const productCard = this.closest('.product-card');
                                const productName = productCard.querySelector('h3').textContent;
                                const productPrice = parseFloat(productCard.querySelector('.price').textContent.replace('$', ''));
                    
                                const product = {
                                    name: productName,
                                    price: productPrice,
                                    image: productCard.querySelector('img').src
                                };
                    
                                cart.push(product);
                                localStorage.setItem('techsphere_cart', JSON.stringify(cart));
                                showCartNotification();
                            });
                        }
                    
                        function showCartNotification() {
                            const notification = document.createElement('div');
                            notification.className = 'fixed top-4 right-4 bg-green-500 text-white px-4 py-2 rounded-lg shadow-lg';
                                notification.textContent = 'Product added to cart!';
                                document.body.appendChild(notification);
                    
                                setTimeout(() => {
                                    notification.remove();
                                }, 3000);
                            }
                    
                            // Update cart count
                            function updateCartCount() {
                                const cartCount = document.querySelector('#cartCount');
                                if (cartCount) {
                                    cartCount.textContent = cart.length;
                                }
                            }
                    
                            // Load products from API
                            async function loadProducts() {
                                try {
                                    // Simulating API call
                                    const response = await fetch('https://fakestoreapi.com/products?limit=8');
                                    const products = await response.json();
                                    displayProducts(products);
                                } catch (error) {
                                    console.error('Failed to load products:', error);
                                }
                            }
                    
                            function displayProducts(products) {
                                const container = document.querySelector('#products .grid');
                                if (!container) return;
                    
                                container.innerHTML = products.map(product => `
                                    <div class="product-card bg-white rounded-xl shadow-sm hover:shadow-lg transition duration-300 overflow-hidden">
                                        <div class="relative">
                                            <img src="${product.image}" alt="${product.title}" class="w-full h-48 object-cover">
                                        </div>
                                        <div class="p-4">
                                            <h3 class="font-semibold text-lg mb-2">${product.title}</h3>
                                            <p class="text-gray-600 text-sm mb-3">${product.description.substring(0, 100)}...</p>
                                            <div class="flex justify-between items-center">
                                                <span class="text-blue-600 font-bold text-xl">$${product.price}</span>
                                        </div>
                                        <a href="#" class="block w-full bg-blue-600 hover:bg-blue-700 text-white text-center py-2 rounded-lg font-semibold transition duration-300">
                                                Add to Cart
                                            </a>
                                        </div>
                                    </div>
                                `).join('');
                            }
                    
                            // Initialize product loading
                            loadProducts();