// Store data will be loaded from JSON let categories = null; let currentCategory = null; let currentSubcategory = null; let selectedPaymentMethod = null; function initializePaymentMethods() { const paymentSelect = document.getElementById('paymentMethod'); paymentSelect.innerHTML = ` ${categories.paymentMethods.map(method => ` `).join('')} `; paymentSelect.addEventListener('change', (e) => { selectedPaymentMethod = e.target.value; updateBuyButtons(); }); } function updateBuyButtons() { const buyButtons = document.querySelectorAll('.buy-btn'); buyButtons.forEach(button => { button.disabled = !selectedPaymentMethod; }); } function handleQuantityChange(productId, change, quantityDisplay) { const currentQty = parseInt(quantityDisplay.textContent); const newQty = Math.max(1, currentQty + change); quantityDisplay.textContent = newQty; } function handlePurchase(product, quantity) { if (!selectedPaymentMethod) { alert('Please select a payment method first'); return; } const total = product.price * quantity; alert(`Purchase Summary: Product: ${product.name} Quantity: ${quantity} Total: $${total.toLocaleString()} Payment Method: ${categories.paymentMethods.find(m => m.id === selectedPaymentMethod).name}`); // Here you would typically integrate with your game's purchasing system } function showMainCategories() { const mainContent = document.querySelector('main'); currentCategory = null; currentSubcategory = null; mainContent.innerHTML = `
`; const categoriesGrid = document.querySelector('.categories-grid'); Object.entries(categories) .filter(([key]) => key !== 'paymentMethods') .forEach(([categoryName, categoryData]) => { const categoryTile = document.createElement('button'); categoryTile.className = 'category-tile'; categoryTile.innerHTML = ` ${categoryData.icon} ${categoryName} `; categoryTile.addEventListener('click', () => showSubcategories(categoryName)); categoriesGrid.appendChild(categoryTile); }); } function showSubcategories(categoryName) { const mainContent = document.querySelector('main'); currentCategory = categoryName; currentSubcategory = null; mainContent.innerHTML = `

${categoryName}

`; const subcategoriesGrid = document.querySelector('.subcategories-grid'); const backButton = document.querySelector('.back-button'); backButton.addEventListener('click', showMainCategories); Object.entries(categories[categoryName].subcategories).forEach(([subCategoryName, subCategoryData]) => { const subCategoryTile = document.createElement('button'); subCategoryTile.className = 'subcategory-tile'; subCategoryTile.innerHTML = ` ${subCategoryData.icon} ${subCategoryName} `; subCategoryTile.addEventListener('click', () => showProducts(categoryName, subCategoryName)); subcategoriesGrid.appendChild(subCategoryTile); }); } function showProducts(categoryName, subCategoryName) { const mainContent = document.querySelector('main'); currentSubcategory = subCategoryName; mainContent.innerHTML = `

${categoryName} > ${subCategoryName}

`; const productsGrid = document.querySelector('.products-grid'); const backButton = document.querySelector('.back-button'); backButton.addEventListener('click', () => showSubcategories(categoryName)); const products = categories[categoryName].subcategories[subCategoryName].products; products.forEach(product => { const productCard = document.createElement('div'); productCard.className = 'product-card'; productCard.innerHTML = `

${product.name}

$${product.price.toLocaleString()}

1
`; const quantityDisplay = productCard.querySelector('.quantity-display'); const minusBtn = productCard.querySelector('.quantity-btn.minus'); const plusBtn = productCard.querySelector('.quantity-btn.plus'); const buyBtn = productCard.querySelector('.buy-btn'); minusBtn.addEventListener('click', () => handleQuantityChange(product.id, -1, quantityDisplay)); plusBtn.addEventListener('click', () => handleQuantityChange(product.id, 1, quantityDisplay)); buyBtn.addEventListener('click', () => handlePurchase(product, parseInt(quantityDisplay.textContent))); productsGrid.appendChild(productCard); }); } // Initialize the store async function initializeStore() { try { const response = await fetch('http://localhost:8000/data/categories.json'); if (!response.ok) { throw new Error('Failed to load store data'); } categories = await response.json(); initializePaymentMethods(); showMainCategories(); } catch (error) { console.error('Error loading store data:', error); document.querySelector('main').innerHTML = `

Error Loading Store Data

Please try again later.

`; } } document.addEventListener('DOMContentLoaded', () => { initializeStore(); });