const API_URL = import.meta.env.VITE_API_URL || '' const BASE = `${API_URL}/api/admin` function authHeaders() { return { 'Content-Type': 'application/json', Authorization: `Bearer ${localStorage.getItem('token')}`, } } function apiError(message, status) { const err = new Error(message) err.status = status return err } export async function getStatsApi() { const res = await fetch(`${BASE}/stats`, { headers: authHeaders() }) const data = await res.json() if (!res.ok) throw apiError(data.message || 'Erreur stats.', res.status) return data } export async function getUsersApi() { const res = await fetch(`${BASE}/users`, { headers: authHeaders() }) const data = await res.json() if (!res.ok) throw apiError(data.message || 'Erreur utilisateurs.', res.status) return data } export async function drawWinnerApi() { const res = await fetch(`${BASE}/draw`, { method: 'POST', headers: authHeaders() }) const data = await res.json() if (!res.ok) throw apiError(data.message || 'Erreur tirage.', res.status) return data } export async function getEmailingContactsApi() { const res = await fetch(`${BASE}/emailing`, { headers: authHeaders() }) const data = await res.json() if (!res.ok) throw apiError(data.message || 'Erreur contacts emailing.', res.status) return data } export async function getWinnerApi() { const res = await fetch(`${BASE}/draw`, { headers: authHeaders() }) if (res.status === 404) return null const data = await res.json() if (!res.ok) throw apiError(data.message || 'Erreur gagnant.', res.status) return data } export async function setGameSettingsApi(startDate) { const res = await fetch(`${BASE}/game-settings`, { method: 'PUT', headers: authHeaders(), body: JSON.stringify({ startDate }), }) const data = await res.json() if (!res.ok) throw apiError(data.message || 'Erreur configuration du jeu.', res.status) return data } export async function getEmployeesApi() { const res = await fetch(`${BASE}/employees`, { headers: authHeaders() }) const data = await res.json() if (!res.ok) throw apiError(data.message || 'Erreur lors du chargement des employés.', res.status) return data } export async function getEmployeeDeliveriesApi(employeeId) { const res = await fetch(`${BASE}/employees/${employeeId}/deliveries`, { headers: authHeaders() }) const data = await res.json() if (!res.ok) throw apiError(data.message || 'Erreur lors du chargement des remises.', res.status) return data } export async function getDeliveryByTicketCodeApi(code) { const res = await fetch(`${BASE}/deliveries/${encodeURIComponent(code.trim())}`, { headers: authHeaders() }) const data = await res.json() if (!res.ok) throw apiError(data.message || 'Erreur lors de la recherche du ticket.', res.status) return data } // Pas de mot de passe : le serveur en génère un provisoire et l'envoie par // email à l'employé. export async function createEmployeeApi({ firstname, lastname, email }) { const res = await fetch(`${BASE}/employees`, { method: 'POST', headers: authHeaders(), body: JSON.stringify({ firstname, lastname, email }), }) const data = await res.json() if (!res.ok) throw apiError(data.message || "Erreur lors de la création de l'employé.", res.status) return data }