TableRepeater::make('promos') ->relationship('block_promos') ->label('Promos') ->reorderable(true) ->reorderableWithButtons(true) ->reorderableWithDragAndDrop(true) ->orderColumn('order_column') ->maxItems(4) ->defaultItems(0) ->headers([ Header::make('Lista Promos'), Header::make('Tipo'), Header::make('Valido') ]) ->addActionLabel('Aggiungi un Promo') ->addAction(function (\Filament\Forms\Components\Actions\Action $action) { return $action ->modalHeading('Seleziona Promos') ->modalSubmitActionLabel('Aggiungi Promos') ->modalCancelActionLabel('Annulla') ->action(function (LivewireComponent $livewire, array $data, TableRepeater $component) { $promosIds = $data['promo_id'] ?? []; if (empty($promosIds)) { Notification::make() ->title('Errore') ->body('Seleziona almeno un promo') ->danger() ->send(); return; } $currentState = $component->getState() ?? []; $maxOrder = 0; if (!empty($currentState) && Arr::exists($currentState, 'order_column')) { $maxOrder = max(array_column($currentState, 'order_column')) ?? 0; } foreach ($promosIds as $promoId) { $maxOrder++; // Check if this promo is already in the repeater $exists = collect($currentState)->contains(function ($item) use ($promoId) { return isset($item['promo_id']) && $item['promo_id'] == $promoId; }); if (!$exists) { $currentState[] = [ 'promo_id' => $promoId, 'order_column' => $maxOrder, ]; } } // Update the repeater state $component->state($currentState); // Refresh the form to show new entries $livewire->dispatch('refresh'); Notification::make() ->title('Successo') ->body(count($promosIds) . ' promo(s) aggiunti con successo') ->success() ->send(); }) ->slideOver() ->form([ RecordFinder::make('promo_id') ->label('Promo') ->inline() ->required() ->multiple() ->query(Promo::query()->scopes(['active', 'visibleInBlog'])) ->openModalActionModalWidth(MaxWidth::Full) ->slideOver() ->badge() ->tableHeaderActions([ CreateAction::make() ->form(fn () => PromoResource::formSchema()) ->icon('fluentui-square-add-20') ->slideOver() ->modalWidth(MaxWidth::Full) ]), ]); })