import type { BeoOption, LogbuchEintrag, ObjektOption, SelectedObjekt, Wetter } from '@/types/logbuch'; const PHP_DB_URL = process.env.PHP_DB_URL ?? 'http://localhost:8080/DB4js_all.php'; async function call(cmd: string, params: object = {}): Promise { const res = await fetch(PHP_DB_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ cmd, ...params }), cache: 'no-store', }); if (!res.ok) { let detail = ''; try { detail = await res.text(); } catch { /* ignore */ } throw new Error(`DB4js ${cmd} HTTP ${res.status}: ${detail}`); } const json: unknown = await res.json(); if (json && typeof json === 'object' && 'error' in json) { throw new Error(`DB4js ${cmd}: ${(json as { error: string }).error}`); } return json as T; } // ---- Auth / Benutzer ---- export interface Beo { id: number; name: string; vorname: string | null; 'kürzel': string | null; pw: string | null; MustChangePassword: number; role: string | null; } export interface BeoUser { id: number; 'kürzel': string | null; name: string; vorname: string | null; role: string | null; hasPw: boolean; } export async function getBeoByKuerzel(kuerzel: string): Promise { const r = await call<{ beo: Beo | null }>('LB_AUTH_KUERZEL', { kuerzel }); return r.beo; } export async function getBeoByName(name: string): Promise { const r = await call<{ beo: Beo | null }>('LB_AUTH_NAME', { name }); return r.beo; } export async function updateBeoPassword(id: number, pwHash: string): Promise { await call('LB_UPDATE_PW', { id, pw: pwHash }); } export async function resetBeoPassword(id: number): Promise { await call('LB_RESET_PW', { id }); } export async function listUsers(): Promise { return call('LB_LIST_USERS'); } // ---- Logbuch ---- export interface ListLogbuchParams { kuppel?: string; limit?: number; offset?: number; month?: string; search?: string; order?: string; } export async function listLogbuch( params: ListLogbuchParams ): Promise<{ entries: LogbuchEintrag[]; total: number }> { return call('LB_LIST_LOGBUCH', params); } export interface CreateLogbuchData { Kuppel: string; ArtFuehrung: string; SonderName?: string | null; Beginn: string; Ende: string; Besucher?: number; beoIds?: number[]; objekte?: SelectedObjekt[]; Bemerkungen?: string | null; Wetter?: Partial | null; created_by: number; } export async function createLogbuch(data: CreateLogbuchData): Promise<{ id: number }> { return call('LB_CREATE_LOGBUCH', data); } export async function updateLogbuch( id: number, userId: number, userRole: string, data: Omit ): Promise { await call('LB_UPDATE_LOGBUCH', { id, user_id: userId, user_role: userRole, ...data }); } export async function deleteLogbuch( id: number, userId: number, userRole: string ): Promise { await call('LB_DELETE_LOGBUCH', { id, user_id: userId, user_role: userRole }); } // ---- BEOs & Objekte ---- export async function getBeos(): Promise { return call('LB_GET_BEOS'); } export async function getObjekte(): Promise { return call('LB_GET_OBJEKTE'); } export async function createObjekt(name: string): Promise { return call('LB_CREATE_OBJEKT', { name }); } export async function updateObjekt(id: number, name: string): Promise { return call('LB_UPDATE_OBJEKT', { id, name }); } export async function deleteObjekt(id: number): Promise { await call('LB_DELETE_OBJEKT', { id }); } export async function listObjekteAdmin(): Promise<{ ID: number; Name: string; LastUsed: string | null }[]> { return call('LB_LIST_OBJEKTE_ADMIN'); } // ---- Auswertungen ---- export interface FahrkostenRow { ID: number; Kuerzel: string; Name: string; Anzahl: number; } export async function getFahrkosten(ab: string): Promise { return call('LB_FAHRKOSTEN', { ab }); } export interface StatistikResult { monthly: { monat: number; tageFuehrungen: number; tageBeob: number; tageTD: number; tageSonst: number; tageBEOS: number; tagesToT: number; tageGesamt: number; besucherRF: number; besucherSF: number; besucherSonF: number; besucherPrF: number; besucherToT: number; besucherGesamt: number; }[]; cumulative: number; tage: number; year: number; } export async function getStatistik(year: number): Promise { return call('LB_STATISTIK', { year }); } // ---- Backup ---- export interface BackupTable { name: string; createSql: string; rows: Record[]; } export async function getBackupData(): Promise<{ tables: BackupTable[] }> { return call('LB_BACKUP_DATA'); }