columns(2) ->components([ Select::make('property_id') ->label('Wohnung') ->options(fn (): array => Property::query()->orderBy('name')->pluck('name', 'id')->all()) ->default(fn (): ?int => app(CurrentProperty::class)->id()) ->required() ->columnSpanFull() ->hidden(fn (): bool => Property::query()->count() <= 1) ->dehydratedWhenHidden(), DatePicker::make('starts_on')->label('Von (erste gesperrte Nacht)')->required(), DatePicker::make('ends_on')->label('Bis (erster freier Tag)')->after('starts_on')->required(), ...static::purposeFields(), Select::make('is_private')->label('Buchungskanal')->options([0 => 'Nicht angegeben', 1 => 'Privat']) ->default(0)->formatStateUsing(fn (mixed $state): int => (int) $state)->required(), TextInput::make('reason') ->label('Bisheriger Sperrgrund / zusätzliche Angabe') ->placeholder('z. B. Eigennutzung, Renovierung') ->maxLength(255) ->columnSpanFull(), ]); } public static function getEloquentQuery(): Builder { return parent::getEloquentQuery()->explicitBlocks()->occupying(); } /** @return array */ public static function purposeFields(): array { return [ Select::make('block_purpose')->label('Zweck der Sperrzeit')->options(BlockedPeriodPurpose::class)->placeholder('Noch nicht angegeben'), TextInput::make('block_note')->label('Hinweis zum Zweck')->maxLength(255)->placeholder('z. B. Aufenthalt der Familie'), ]; } public static function table(Table $table): Table { return $table ->description('Eigennutzung, Wartung und ausdrücklich als Sperrzeit bestätigte Zeiträume. Portalreservierungen stehen unter Anfragen & Buchungen; unbekannte Zeiten unter Ungeklärte Belegungen.') ->defaultSort('starts_on') ->columns([ TextColumn::make('starts_on')->label('Von')->date('D, d.m.Y')->sortable(), TextColumn::make('ends_on')->label('Bis / erster freier Tag')->date('D, d.m.Y'), TextColumn::make('purpose')->label('Zweck / Grund')->state(fn (BlockedPeriod $record): ?string => $record->blockDescription())->placeholder('–'), TextColumn::make('booking_channel')->label('Buchungskanal')->state(fn (BlockedPeriod $record): string => $record->bookingChannelLabel()), TextColumn::make('property.name')->label('Wohnung'), TextColumn::make('channel.name')->label('Kalenderquelle')->placeholder('Eigene Sperre'), ]) ->filters([ Filter::make('upcoming')->label('Nur kommende')->default() ->query(fn (Builder $query): Builder => $query->whereDate('ends_on', '>=', today())), ]) ->recordActions([ Action::make('viewPortal')->label('Details')->visible(fn (BlockedPeriod $record): bool => $record->source !== 'manual') ->url(fn (BlockedPeriod $record): string => PortalOccupationResource::getUrl('view', ['record' => $record->id])), EditAction::make()->visible(fn (BlockedPeriod $record): bool => $record->source === 'manual'), PortalOccupationResource::classificationAction(), DeleteAction::make()->visible(fn (BlockedPeriod $record): bool => $record->source === 'manual'), ]); } public static function portalActions(): array { return [ Action::make('icalOriginal')->label('iCal-Original & Erkennung')->visible(fn (BlockedPeriod $record): bool => $record->ical_raw !== null) ->modalSubmitAction(false)->modalCancelActionLabel('Schließen')->schema([ TextEntry::make('recognized_rule')->label('Treffende Regel')->state(fn (BlockedPeriod $record): string => $record->ical_interpretation['rule'] ?? 'Keine'), TextEntry::make('recognized_representation')->label('Fachliche Einordnung')->state(fn (BlockedPeriod $record): string => ICalRuleEngine::REPRESENTATIONS[$record->ical_interpretation['representation'] ?? 'unspecified']), TextEntry::make('original')->label('Originalereignis (entfaltet)')->state(fn (BlockedPeriod $record): string => $record->ical_raw)->wrap()->extraAttributes(['style' => 'white-space:pre-wrap;overflow-wrap:anywhere']), ]), Action::make('resolve')->label('Klären: eigenständig oder Doppelung?') ->visible(fn (BlockedPeriod $record): bool => ! $record->trashed() && in_array($record->source, ['channel', 'email'], true)) ->authorize(fn (BlockedPeriod $record): bool => auth()->user()?->can('update', $record) ?? false) ->modalDescription('Nur nach persönlicher Prüfung klären. Eigenständig mit Buchungskanal (oder privat) gilt danach als geklärt und verschwindet aus „Ungeklärt“. Eine Doppelung steuert keine eigene Belegung mehr; die führende Buchung oder Sperre übernimmt Änderungen und Stornierungen. Kalenderereignisse und UIDs bleiben nachvollziehbar erhalten.') ->schema([ Select::make('resolution')->label('Fachliche Klärung')->options(['independent' => 'Eigenständige Belegung – keine Doppelung (Buchungskanal unten angeben)', 'block' => 'Doppelung: dieselbe Belegung steht schon als Portalreservierung oder Sperrzeit im Kalender', 'booking' => 'Doppelung: dieselbe Belegung ist eine unserer eigenen Buchungen'])->default('independent')->required()->live(), Select::make('target_id')->label('Führende Belegung mit demselben Zeitraum') ->visible(fn (Get $get): bool => $get('resolution') !== 'independent') ->required(fn (Get $get): bool => $get('resolution') !== 'independent') ->options(function (BlockedPeriod $record, Get $get): array { if ($get('resolution') === 'booking') { return Booking::query()->where('property_id', $record->property_id)->occupying()->whereDate('arrival', $record->starts_on)->whereDate('departure', $record->ends_on)->get() ->mapWithKeys(fn (Booking $booking): array => [$booking->id => 'Buchung '.$booking->reference])->all(); } return BlockedPeriod::query()->where('property_id', $record->property_id)->occupying()->whereKeyNot($record->id)->whereDate('starts_on', $record->starts_on)->whereDate('ends_on', $record->ends_on)->orderByDesc('leading_priority')->orderBy('id')->get() ->mapWithKeys(fn (BlockedPeriod $block): array => [$block->id => '#'.$block->id.' · '.$block->occupationKind()->getLabel().' · '.($block->reason ?? 'Kalenderbelegung').($block->leading_priority ? ' · bevorzugter Führungskandidat' : '')])->all(); }), Select::make('origin_channel_id')->label('Ursprünglicher Buchungskanal, sofern persönlich bekannt') ->options(fn (BlockedPeriod $record): array => Channel::query()->portals()->where('property_id', $record->property_id)->pluck('name', 'id')->all()) ->default(fn (BlockedPeriod $record): ?int => $record->origin_channel_id)->placeholder('Unbekannt / eigene Vermietung'), ]) ->action(function (BlockedPeriod $record, array $data, ResolveChannelOccupation $resolve): void { $resolve->handle($record, $data['resolution'], filled($data['target_id'] ?? null) ? (int) $data['target_id'] : null, filled($data['origin_channel_id'] ?? null) ? (int) $data['origin_channel_id'] : null); Notification::make()->title('Belegungszuordnung gespeichert')->success()->send(); }), ]; } public static function getPages(): array { return [ 'index' => ManageBlockedPeriods::route('/'), ]; } }