const API_BASE = '/api'; let currentTab = 'products'; let editingProductId = null; // Tab switching function switchTab(tab) { currentTab = tab; // Update tab buttons document.querySelectorAll('.admin-tab').forEach(btn => { btn.classList.remove('active'); }); event.target.classList.add('active'); // Update content document.querySelectorAll('.admin-content').forEach(content => { content.classList.remove('active'); }); document.getElementById(tab + 'Tab').classList.add('active'); // Load data for the tab if (tab === 'products') { loadProducts(); } else if (tab === 'orders') { loadOrders(); } else if (tab === 'regions') { loadRegions(); } else if (tab === 'translations') { loadTranslations(); } else if (tab === 'currency') { loadCurrencyRates(); } } // Product Management async function loadProducts() { const container = document.getElementById('productsList'); try { const response = await fetch(`${API_BASE}/products`); const data = await response.json(); if (!data.success) { throw new Error(data.error || 'Failed to load products'); } if (data.products.length === 0) { container.innerHTML = '
| Image | Name | Category | Price | Stock | Actions |
|---|---|---|---|---|---|
|
${product.image && product.image.trim() && (product.image.startsWith('http') || product.image.startsWith('/'))
? ` No Image '}
|
${escapeHtml(product.name)} | ${escapeHtml(product.category || 'general')} |
${product.currency} ${(product.price / 100).toFixed(2)}
${product.taxIncluded ? '(tax included)' : '(tax excluded)'} |
${product.stock === null ? 'Unlimited' : product.stock} |
| Order ID | Customer | Items | Total | Status | Tracking | Date | Actions |
|---|---|---|---|---|---|---|---|
| ${order.orderId} |
${order.customerInfo?.email || 'N/A'} ${order.customerInfo?.firstName || ''} ${order.customerInfo?.lastName || ''} |
${order.items.map(item => `${item.productName} x${item.quantity}`).join(' ')} |
${order.currency} ${(order.total / 100).toFixed(2)} | ${order.status} |
${order.trackingId ? `${escapeHtml(order.trackingId)} ${order.carrier || ''}` : '—'} |
${new Date(order.createdAt).toLocaleString()} |
| Country Code | Country Name | Language | Currency | Status | Actions |
|---|---|---|---|---|---|
| ${escapeHtml(region.countryCode)} | ${escapeHtml(region.countryName)} | ${escapeHtml(region.languageCode)} | ${escapeHtml(region.currencyCode)} | ${region.enabled ? 'Enabled' : 'Disabled'} | ${region.enabled ? `` : `` } |
Select a product to view/manage translations
| Field | Language | Translation | Actions |
|---|---|---|---|
| ${escapeHtml(trans.fieldName)} | ${escapeHtml(trans.languageCode).toUpperCase()} | ${escapeHtml(trans.translatedText)} |
| From | To | Rate | Example | Updated | Expires |
|---|---|---|---|---|---|
| ${escapeHtml(rate.fromCurrency)} | ${escapeHtml(rate.toCurrency)} | ${rate.rate.toFixed(4)} | 1 ${rate.fromCurrency} = ${rate.rate.toFixed(2)} ${rate.toCurrency} | ${new Date(rate.fetchedAt).toLocaleString()} | ${isExpired ? 'Expired' : new Date(rate.expiresAt).toLocaleTimeString()} |