query( 'SELECT p.*, (SELECT COUNT(*) FROM departments d WHERE d.sla_plan_id = p.id) AS dept_count, (SELECT COUNT(*) FROM help_topics h WHERE h.sla_plan_id = p.id) AS topic_count FROM sla_plans p ORDER BY p.name' )->fetchAll(); api_json(['items' => array_map(fn(array $p) => [ 'id' => (int) $p['id'], 'name' => $p['name'], 'responseHours' => (int) $p['response_hours'], 'resolutionHours' => (int) $p['resolution_hours'], 'isActive' => (bool) $p['is_active'], 'notes' => $p['notes'], 'departmentCount' => (int) $p['dept_count'], 'helpTopicCount' => (int) $p['topic_count'], 'updatedAt' => $p['updated_at'], ], $plans)]); } $body = api_request_body(); $action = $body['action'] ?? ''; $id = (int) ($body['id'] ?? 0); try { if ($action === 'save') { $in = ['name' => $body['name'] ?? '', 'response_hours' => $body['responseHours'] ?? 0, 'resolution_hours' => $body['resolutionHours'] ?? 0, 'notes' => $body['notes'] ?? '']; if (array_key_exists('isActive', $body)) $in['is_active'] = $body['isActive']; $newId = sla_plan_save($pdo, $id, $in); audit_log($staff['id'], ($id === 0 ? 'SLA-Plan erstellt: ' : 'SLA-Plan aktualisiert: ') . trim((string) ($body['name'] ?? '')) . ' (Mobile App)'); api_json(['id' => $newId]); } if ($action === 'delete') { $deleted = sla_plan_delete($pdo, $id); audit_log($staff['id'], "SLA-Plan gelöscht: {$deleted} (Mobile App)"); api_json(['ok' => true]); } } catch (AdminModelException $e) { api_error($e->httpStatus(), $e->errorCode(), $e->getMessage()); } api_error(422, 'INVALID_ACTION', 'Unbekannte Aktion.');