import type { SummaryData, TypeSummary, VehicleListItem, DeptGroup, RegionGroup, CustomerStats, RegionalInventoryStats, } from './types'; const BASE = '/api/vehicles'; async function fetchJson(url: string): Promise { const res = await fetch(url); if (!res.ok) throw new Error(`API error: ${res.status} ${res.statusText}`); return res.json(); } export async function fetchSummary(): Promise { return fetchJson(`${BASE}/summary`); } export async function fetchByType(): Promise { return fetchJson(`${BASE}/by-type`); } export async function fetchVehicleList(params: { batch?: string; model?: string; location?: string; status?: string; category?: string; vehicleType?: string; manager?: string; customer?: string; isColdChain?: string; isTrailer?: string; department?: string; attendance?: string; }): Promise { const query = new URLSearchParams(); if (params.batch) query.set('batch', params.batch); if (params.model) query.set('model', params.model); if (params.location) query.set('location', params.location); if (params.status) query.set('status', params.status); if (params.category) query.set('category', params.category); if (params.vehicleType) query.set('vehicleType', params.vehicleType); if (params.manager) query.set('manager', params.manager); if (params.customer) query.set('customer', params.customer); if (params.isColdChain) query.set('isColdChain', params.isColdChain); if (params.isTrailer) query.set('isTrailer', params.isTrailer); if (params.department) query.set('department', params.department); if (params.attendance) query.set('attendance', params.attendance); return fetchJson(`${BASE}/list?${query.toString()}`); } export interface WeeklyDetailItem { truck_id: number; plate_number: string; handover_date: string | null; contract_type: string | null; customer_name: string | null; } export async function fetchDeptStats(): Promise { return fetchJson(`${BASE}/dept-stats`); } export async function fetchRegionStats(params?: { customer?: string; city?: string; region?: string }): Promise { const query = new URLSearchParams(); if (params?.customer) query.set('customer', params.customer); if (params?.city) query.set('city', params.city); if (params?.region) query.set('region', params.region); const qs = query.toString(); return fetchJson(`${BASE}/region-stats${qs ? `?${qs}` : ''}`); } export async function fetchCustomerStats(): Promise { return fetchJson(`${BASE}/customer-stats`); } export async function fetchInventoryStats(): Promise { return fetchJson(`${BASE}/inventory-stats`); } export async function fetchRegionChart(groupBy: string, top = 8, source = 'realtime'): Promise<{ name: string; value: number }[]> { return fetchJson<{ name: string; value: number }[]>(`${BASE}/region-chart?groupBy=${groupBy}&top=${top}&source=${source}`); } export async function fetchWeeklyDetail(type: string): Promise { return fetchJson(`${BASE}/weekly-detail?type=${type}`); }