const ProductManager = { init() { this.loadProducts(); this.setupFilterButtons(); this.setupSearch(); this.setupCartButtons(); }, async loadProducts() { try { Utils.showLoading(); const response = await fetch('data/products.json'); if (!response.ok) throw new Error('ไม่สามารถโหลดข้อมูลสินค้าได้'); const data = await response.json(); State.products = data.products; State.categories = data.categories; State.tags = data.tags; this.renderCategories(); this.renderProducts(); } catch (error) { console.error('Error loading products:', error); NotificationManager.showError('ไม่สามารถโหลดข้อมูลสินค้าได้'); } finally { Utils.hideLoading(); } }, setupSearch() { const searchInput = document.getElementById('searchInput'); if (!searchInput) return; let debounceTimeout; searchInput.addEventListener('input', (e) => { clearTimeout(debounceTimeout); debounceTimeout = setTimeout(() => { const searchTerm = e.target.value.toLowerCase().trim(); this.filterProducts('all', searchTerm); }, 300); }); }, setupFilterButtons() { const filterContainer = document.getElementById('categoryFilters'); if (!filterContainer) return; filterContainer.addEventListener('click', (e) => { const button = e.target.closest('.category-filter'); if (!button) return; const searchTerm = document.getElementById('searchInput')?.value.toLowerCase().trim() || ''; filterContainer.querySelectorAll('.category-filter').forEach(btn => { btn.classList.remove('active'); }); button.classList.add('active'); const category = button.dataset.category; this.filterProducts(category, searchTerm); }); document.getElementById('productGrid')?.addEventListener('click', (e) => { const tag = e.target.closest('.product-tag'); if (!tag) return; const tagId = tag.dataset.tag; if (tagId) { document.getElementById('searchInput').value = ''; filterContainer.querySelectorAll('.category-filter').forEach(btn => { btn.classList.remove('active'); }); filterContainer.querySelector('[data-category="all"]').classList.add('active'); this.filterByTag(tagId); } }); }, renderCategories() { const filterContainer = document.getElementById('categoryFilters'); if (!filterContainer) return; filterContainer.innerHTML = ` `; State.categories .sort((a, b) => a.order - b.order) .forEach(category => { filterContainer.innerHTML += ` `; }); }, filterProducts(category, searchTerm = '') { let filteredProducts = category === 'all' ? [...State.products] : State.products.filter(p => p.category === category); if (searchTerm) { filteredProducts = filteredProducts.filter(p => p.name.toLowerCase().includes(searchTerm) || p.description.toLowerCase().includes(searchTerm) || p.tags.some(tag => State.tags[tag]?.name.toLowerCase().includes(searchTerm)) ); } this.renderProducts(filteredProducts); this.updateProductCount(filteredProducts.length); }, filterByTag(tagId) { const filteredProducts = State.products.filter(p => p.tags.includes(tagId)); this.renderProducts(filteredProducts); this.updateProductCount(filteredProducts.length); }, updateProductCount(count) { const countElement = document.getElementById('productCount'); if (countElement) { countElement.textContent = `${count} รายการ`; } }, renderProducts(products = State.products) { const productGrid = document.getElementById('productGrid'); if (!productGrid) return; productGrid.innerHTML = ''; if (products.length === 0) { productGrid.innerHTML = `
ไม่พบสินค้าที่คุณค้นหา
${product.description}
${product.nutrition ? this.createNutritionInfo(product.nutrition) : ''}