const API_BASE = '/api'; const api = { async getConfig() { const res = await fetch(`${API_BASE}/config`); return res.json(); }, async getProducts(params = {}) { const query = new URLSearchParams(params).toString(); const res = await fetch(`${API_BASE}/products${query ? '?' + query : ''}`, { credentials: 'include' }); return res.json(); }, async getCategories() { const res = await fetch(`${API_BASE}/categories`); return res.json(); }, async getMetalPrices() { const res = await fetch(`${API_BASE}/shop-prices`); return res.json(); }, async createOrder(data) { const res = await fetch(`${API_BASE}/orders`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(data) }); return res.json(); }, async getOrders() { const res = await fetch(`${API_BASE}/orders`, { credentials: 'include' }); if (!res.ok) throw new Error('Failed to get orders'); return res.json(); }, async authLiff(accessToken) { const res = await fetch(`${API_BASE}/auth/liff`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify({ access_token: accessToken }) }); if (!res.ok) throw new Error('Auth failed'); return res.json(); }, async getMe() { const res = await fetch(`${API_BASE}/auth/me`, { credentials: 'include' }); if (!res.ok) throw new Error('Unauthorized'); return res.json(); }, async logout() { const res = await fetch(`${API_BASE}/auth/logout`, { method: 'POST', credentials: 'include' }); return res.json(); }, async getProfile() { const res = await fetch(`${API_BASE}/profile`, { credentials: 'include' }); if (!res.ok) throw new Error('Failed to get profile'); return res.json(); }, async updateProfile(data) { const res = await fetch(`${API_BASE}/profile`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, credentials: 'include', body: JSON.stringify(data) }); if (!res.ok) throw new Error('Failed to update profile'); return res.json(); }, async cancelOrder(orderNumber) { const res = await fetch(`${API_BASE}/orders/${orderNumber}/cancel`, { method: 'POST', credentials: 'include' }); if (!res.ok) { const err = await res.json().catch(() => ({})); throw new Error(err.detail || '取消失敗'); } return res.json(); }, async confirmReceived(orderNumber) { const res = await fetch(`${API_BASE}/orders/${orderNumber}/confirm-received`, { method: 'POST', credentials: 'include' }); if (!res.ok) { const err = await res.json().catch(() => ({})); throw new Error(err.detail || '確認收貨失敗'); } return res.json(); }, async getAnnouncement() { const res = await fetch(`${API_BASE}/announcement`); return res.json(); } };