prepare('SELECT id, name FROM staff WHERE id = ?'); $withStmt->execute([$withId]); $withStaff = $withStmt->fetch(); if (!$withStaff) { api_error(404, 'STAFF_NOT_FOUND', 'Der Mitarbeiter wurde nicht gefunden.'); } $pdo->prepare('UPDATE staff_chat_messages SET is_read = 1 WHERE from_staff_id = ? AND to_staff_id = ?') ->execute([$withId, $staff['id']]); $cStmt = $pdo->prepare( 'SELECT m.*, sf.name AS from_name FROM staff_chat_messages m JOIN staff sf ON sf.id = m.from_staff_id WHERE (m.from_staff_id = ? AND m.to_staff_id = ?) OR (m.from_staff_id = ? AND m.to_staff_id = ?) ORDER BY m.created_at ASC LIMIT 300' ); $cStmt->execute([$staff['id'], $withId, $withId, $staff['id']]); $conversation = $cStmt->fetchAll(); api_json([ 'with' => ['id' => (int) $withStaff['id'], 'name' => $withStaff['name']], 'messages' => array_map(fn(array $m) => [ 'id' => (int) $m['id'], 'fromStaffId' => (int) $m['from_staff_id'], 'fromName' => $m['from_name'], 'message' => $m['message'], 'createdAt' => $m['created_at'], ], $conversation), ]); } $allStaff = $pdo->prepare('SELECT id, name FROM staff WHERE is_active = 1 AND id != ? ORDER BY name'); $allStaff->execute([$staff['id']]); $contacts = $allStaff->fetchAll(); $unreadStmt = $pdo->prepare( 'SELECT from_staff_id, COUNT(*) AS cnt FROM staff_chat_messages WHERE to_staff_id = ? AND is_read = 0 GROUP BY from_staff_id' ); $unreadStmt->execute([$staff['id']]); $unreadByStaff = []; foreach ($unreadStmt->fetchAll() as $row) { $unreadByStaff[(int) $row['from_staff_id']] = (int) $row['cnt']; } $lastStmt = $pdo->prepare( "SELECT CASE WHEN from_staff_id = ? THEN to_staff_id ELSE from_staff_id END AS other_id, MAX(created_at) AS last_at FROM staff_chat_messages WHERE from_staff_id = ? OR to_staff_id = ? GROUP BY other_id" ); $lastStmt->execute([$staff['id'], $staff['id'], $staff['id']]); $lastByStaff = []; foreach ($lastStmt->fetchAll() as $row) { $lastByStaff[(int) $row['other_id']] = $row['last_at']; } api_json([ 'contacts' => array_map(fn(array $s) => [ 'id' => (int) $s['id'], 'name' => $s['name'], 'unreadCount' => $unreadByStaff[(int) $s['id']] ?? 0, 'lastMessageAt' => $lastByStaff[(int) $s['id']] ?? null, ], $contacts), ]); } $body = api_request_body(); $action = $body['action'] ?? ''; if ($action === 'send') { $toId = (int) ($body['toId'] ?? 0); $message = trim((string) ($body['message'] ?? '')); if ($toId === 0) api_error(422, 'RECIPIENT_REQUIRED', 'Bitte einen Empfänger angeben.'); if ($message === '') api_error(422, 'MESSAGE_REQUIRED', 'Bitte eine Nachricht eingeben.'); if (strlen($message) > 5000) api_error(422, 'MESSAGE_TOO_LONG', 'Die Nachricht ist zu lang.'); $toCheck = $pdo->prepare('SELECT COUNT(*) FROM staff WHERE id = ? AND is_active = 1'); $toCheck->execute([$toId]); if (!(bool) $toCheck->fetchColumn()) { api_error(422, 'RECIPIENT_NOT_FOUND', 'Der Empfänger wurde nicht gefunden.'); } $pdo->prepare('INSERT INTO staff_chat_messages (from_staff_id, to_staff_id, message, created_at) VALUES (?,?,?,?)') ->execute([$staff['id'], $toId, $message, db_now()]); api_json(['id' => (int) $pdo->lastInsertId()]); } api_error(422, 'INVALID_ACTION', 'Unbekannte Aktion.');