query( 'SELECT s.id, s.name, s.is_on_vacation, s.last_active_at, sa.auto_offline, sa.status, sa.note, sa.updated_at FROM staff s LEFT JOIN staff_availability sa ON sa.staff_id = s.id WHERE s.is_active = 1 ORDER BY s.name' )->fetchAll(); $me = null; $team = []; foreach ($agents as $a) { $entry = [ 'id' => (int) $a['id'], 'name' => $a['name'], 'status' => $a['is_on_vacation'] ? 'offline' : ($a['status'] ?: 'offline'), 'note' => $a['is_on_vacation'] ? 'Urlaub' : ($a['note'] ?? ''), 'isOnVacation' => (bool) $a['is_on_vacation'], 'updatedAt' => $a['updated_at'], 'lastActiveAt' => $a['last_active_at'] ?? null, 'autoOffline' => !empty($a['auto_offline']), ]; $team[] = $entry; if ((int) $a['id'] === (int) $staff['id']) { // "Mein Status" fällt (wie im Web) auf "available" zurück, solange nie gesetzt – // reine Anzeige-Vorauswahl, ändert nichts an der "Offline"-Team-Konvention oben. $me = ['status' => $a['status'] ?: 'available', 'note' => $a['note'] ?? '']; } } api_json(['myStatus' => $me['status'] ?? 'available', 'myNote' => $me['note'] ?? '', 'team' => $team]); } $body = api_request_body(); $newStatus = (string) ($body['status'] ?? ''); $note = trim((string) ($body['note'] ?? '')); if (!in_array($newStatus, AVAILABILITY_STATUSES, true)) { api_error(422, 'INVALID_STATUS', 'Ungültiger Status.'); } // Ein Admin darf den Status eines anderen Mitarbeiters setzen (staffId), alle anderen nur den eigenen $targetId = (int) $staff['id']; if (isset($body['staffId']) && (int) $body['staffId'] !== $targetId) { if (!has_permission($staff, 'manage_availability')) { api_error(403, 'FORBIDDEN', 'Für den Status anderer Mitarbeiter fehlt die erforderliche Berechtigung.'); } $targetId = (int) $body['staffId']; $exists = $pdo->prepare('SELECT COUNT(*) FROM staff WHERE id = ? AND is_active = 1'); $exists->execute([$targetId]); if ((int) $exists->fetchColumn() === 0) { api_error(404, 'STAFF_NOT_FOUND', 'Mitarbeiter nicht gefunden.'); } audit_log((int) $staff['id'], "Status von Mitarbeiter #$targetId auf $newStatus gesetzt"); } staff_set_status($pdo, $targetId, $newStatus, $note); staff_touch_activity($pdo, (int) $staff['id']); api_json(['ok' => true, 'status' => $newStatus, 'staffId' => $targetId]);