prepare('SELECT COUNT(*) FROM ticket_collaborators WHERE ticket_id = ? AND LOWER(email) = LOWER(?)'); $dup->execute([$ticket['id'], $email]); if ((int) $dup->fetchColumn() === 0) { $pdo->prepare('INSERT INTO ticket_collaborators (ticket_id, email, name, added_by, created_at) VALUES (?,?,?,?,?)') ->execute([$ticket['id'], $email, $name !== '' ? $name : null, $staff['id'], db_now()]); log_activity($pdo, (int) $ticket['id'], (int) $staff['id'], "Kollaborator hinzugefügt: {$email}"); } } elseif ($action === 'remove') { $s = $pdo->prepare('SELECT email FROM ticket_collaborators WHERE id = ? AND ticket_id = ?'); $s->execute([(int) ($body['id'] ?? 0), $ticket['id']]); $email = $s->fetchColumn(); if ($email !== false) { $pdo->prepare('DELETE FROM ticket_collaborators WHERE id = ? AND ticket_id = ?')->execute([(int) $body['id'], $ticket['id']]); log_activity($pdo, (int) $ticket['id'], (int) $staff['id'], "Kollaborator entfernt: {$email}"); } } else { api_error(422, 'UNKNOWN_ACTION', 'Unbekannte Aktion.'); } } api_json(['collaborators' => api_ticket_collaborators($pdo, (int) $ticket['id'])]);