// ======================================== // API CLIENT — Frontend → Backend Communication // ======================================== const API_BASE = (window.location.hostname === '127.0.0.1' || window.location.port === '5500') ? 'http://localhost:3000/api' : '/api'; const API = { // === PRODUCTS === async getProducts() { try { const res = await fetch(`${API_BASE}/products`); if (!res.ok) throw new Error('Products fetch failed'); return await res.json(); } catch (err) { console.warn('API unavailable, using local data:', err.message); return null; // Fallback to local DEFAULT_PRODUCTS } }, async getProduct(id) { try { const res = await fetch(`${API_BASE}/products/${id}`); if (!res.ok) return null; return await res.json(); } catch { return null; } }, async createProduct(data) { const res = await fetch(`${API_BASE}/products`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); return await res.json(); }, async updateProduct(id, data) { const res = await fetch(`${API_BASE}/products/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); return await res.json(); }, async deleteProduct(id) { const res = await fetch(`${API_BASE}/products/${id}`, { method: 'DELETE' }); return await res.json(); }, // === AUTH === async register(name, phone, password, email) { const res = await fetch(`${API_BASE}/auth/register`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, phone, password, email }) }); return await res.json(); }, async login(phone, password) { const res = await fetch(`${API_BASE}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ phone, password }) }); return await res.json(); }, async getProfile(userId) { const res = await fetch(`${API_BASE}/auth/profile/${userId}`); return await res.json(); }, async updateProfile(userId, data) { const res = await fetch(`${API_BASE}/auth/profile/${userId}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); return await res.json(); }, // === ORDERS === async getOrders() { const res = await fetch(`${API_BASE}/orders`); return await res.json(); }, async getUserOrders(userId) { const res = await fetch(`${API_BASE}/orders/user/${userId}`); return await res.json(); }, async createOrder(data) { const res = await fetch(`${API_BASE}/orders`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); return await res.json(); }, async updateOrderStatus(orderId, status) { const res = await fetch(`${API_BASE}/orders/${orderId}/status`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ status }) }); return await res.json(); }, // === SETTINGS === async getSettings() { try { const res = await fetch(`${API_BASE}/settings`); return await res.json(); } catch { return {}; } }, async updateSettings(data) { const res = await fetch(`${API_BASE}/settings`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); return await res.json(); }, async adminLogin(password) { const res = await fetch(`${API_BASE}/settings/admin-login`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ password }) }); return await res.json(); } };