query( 'SELECT s.id, s.name, s.is_on_vacation, 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'], ]; $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.'); } $drv = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME); $sql = $drv === 'sqlite' ? 'INSERT INTO staff_availability (staff_id, status, note, updated_at) VALUES (?,?,?,?) ON CONFLICT(staff_id) DO UPDATE SET status=excluded.status, note=excluded.note, updated_at=excluded.updated_at' : 'INSERT INTO staff_availability (staff_id, status, note, updated_at) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE status=VALUES(status), note=VALUES(note), updated_at=VALUES(updated_at)'; $pdo->prepare($sql)->execute([$staff['id'], $newStatus, $note ?: null, db_now()]); api_json(['ok' => true, 'status' => $newStatus]);