label('Gast zuordnen') ->modalWidth('3xl') ->visible(fn (Booking|BlockedPeriod $record): bool => auth()->user()?->can('update', $record) && ($record instanceof Booking || (! $record->trashed() && ! $record->is_representation && $record->occupationKind() === OccupationKind::Reservation))) ->authorize(fn (Booking|BlockedPeriod $record): bool => auth()->user()?->can('update', $record) ?? false) ->modalDescription('Einen vorhandenen Gast persönlich auswählen oder einen neuen Gast anlegen. Ein Namensfragment ist kein Identitätsnachweis. Die Zuordnung versendet keine Nachricht und ändert keine Belegungszeiträume.') ->fillForm(fn (Booking|BlockedPeriod $record): array => [ 'mode' => 'existing', 'guest_id' => $record->guest_id, 'country' => 'DE', 'first_name' => $record instanceof BlockedPeriod ? ($record->ical_interpretation['fields']['guest_first_name'] ?? '') : '', ]) ->schema([ Select::make('mode')->label('Zuordnung')->options(['existing' => 'Vorhandenen Gast auswählen', 'new' => 'Neuen Gast anlegen'])->required()->live(), Select::make('guest_id')->label('Gast')->searchable() ->getSearchResultsUsing(fn (string $search): array => Guest::query()->whereNull('anonymized_at') ->where(fn ($query) => $query->where('first_name', 'like', '%'.$search.'%')->orWhere('last_name', 'like', '%'.$search.'%')->orWhere('email', 'like', '%'.$search.'%')) ->orderBy('last_name')->orderBy('id')->limit(50)->get()->mapWithKeys(fn (Guest $guest): array => [$guest->id => $guest->full_name.($guest->email ? ' · '.$guest->email : ' · E-Mail unbekannt')])->all()) ->getOptionLabelUsing(fn ($value): ?string => Guest::find($value)?->full_name) ->visible(fn (Get $get): bool => $get('mode') === 'existing')->required(fn (Get $get): bool => $get('mode') === 'existing'), Section::make('Kontakt & Adresse')->columns(2)->visible(fn (Get $get): bool => $get('mode') === 'new') ->schema(GuestForm::contactFields()), Section::make('Notizen & Markierungen')->visible(fn (Get $get): bool => $get('mode') === 'new') ->schema(GuestForm::personalFields()), ]) ->action(function (Booking|BlockedPeriod $record, array $data, AssignOccupationGuest $assign): void { $assign->handle($record, $data['mode'] === 'existing' ? (int) $data['guest_id'] : null, $data); $record->refresh()->load('guest'); Notification::make()->title('Gast zugeordnet')->success()->send(); }); } }