schema([ Select::make('spice_id') ->label('Rohstoff') ->relationship('spice', 'name') ->required() ->preload() ->searchable() ->live() ->createOptionForm([ TextInput::make('name')->label('Name')->required()->maxLength(255), Forms\Components\Select::make('unit') ->label('Mengeneinheit') ->native(false) ->options([ 'grams' => '(Kilo-)Gramm', 'pieces' => 'Stück', 'liter' => 'Liter', ]) ->required(), ]) ->createOptionUsing(function (array $data) { return \App\Models\Spice::create($data)->id; }), Select::make('supplier_id') ->label('Lieferant') ->relationship('supplier', 'name') ->required() ->preload() ->searchable(), TextInput::make('amount') ->label('Liefermenge') ->numeric() ->required() ->rules(['min:0.001']) ->disabled(fn($record) => !is_null($record)) ->label(function (Get $get) { $unit = \App\Models\Spice::find($get('spice_id'))?->unit; return $unit === 'pieces' ? 'Anzahl Stück' : 'Liefermenge'; }) ->suffix(function (Get $get) { $unit = \App\Models\Spice::find($get('spice_id'))?->unit; return match($unit) { 'grams' => 'kg', 'pieces' => 'Stück', 'liter' => 'Liter', default => null, }; }) ->formatStateUsing(function ($state, $record) { if ($record?->spice?->unit === 'grams') { return round($state / 1000, 3); } return $state; }) ->dehydrateStateUsing(function ($state, Get $get) { $unit = \App\Models\Spice::find($get('spice_id'))?->unit; return $unit === 'grams' ? (float) $state * 1000 : $state; }), TextInput::make('weight_g') ->label('Gesamtgewicht dieser Lieferung (g)') ->numeric() ->suffix('g') ->helperText('Optional — aktualisiert das Ø Stückgewicht automatisch') ->disabled(fn($record) => !is_null($record)) ->visible(function (Get $get) { return \App\Models\Spice::find($get('spice_id'))?->unit === 'pieces'; }), DatePicker::make('supplied_at') ->label('Lieferdatum') ->default(now()) ->required(), ]); } public static function table(Table $table): Table { return $table ->columns([ TextColumn::make('spice.name')->label('Rohstoff')->searchable(), TextColumn::make('supplier.name')->label('Lieferant')->searchable(), TextColumn::make('amount')->label('Liefermenge'), TextColumn::make('supplied_at')->label('Lieferdatum')->date('d.m.Y'), // Lieferdatum in der Tabelle anzeigen ]) ->actions([ Tables\Actions\EditAction::make(), ]) ->bulkActions([ Tables\Actions\DeleteBulkAction::make(), ]); } protected static function getTableActions(): array { return [ DeleteAction::make() ->before(function ($record, DeleteAction $action) { // Custom check logic if (true) { // Notify user about the restriction Notification::make() ->title('Deletion Error') ->body("This record cannot be deleted because it has related records.") ->status('danger') ->send(); // Cancel the deletion $action->cancel(); } }), ]; } public static function getRelations(): array { return []; } public static function getPages(): array { return [ 'index' => Pages\ListSpiceSuppliers::route('/'), 'create' => Pages\CreateSpiceSupplier::route('/create'), 'edit' => Pages\EditSpiceSupplier::route('/{record}/edit'), ]; } public static function getPluralLabel(): string { return 'Rohstofflieferungen'; } }