schema([ Section::make('') ->schema([ Forms\Components\TextInput::make('code') ->required() ->unique(ignoreRecord: true) ->integer() ->reactive() ->placeholder('Scan the valid code') ->autofocus(true) ->minValue(1000) ->afterStateUpdated(function ($state, callable $set, callable $get) { $codeId = $get('code'); // Ensure `linestop_id` is not cleared if (!$codeId) { $set('pCodeError', 'Scan the valid code.'); return; } else { if (strlen($codeId) < 4) { $set('pCodeError', 'Code must be at least 4 digits.'); return; } // if (!preg_match('/^[1-9][0-9]{3,}$/', $codeId)) { // $set('code',null); // $set('pCodeError', 'Code should not begin with zero and must contain 4 digit number.'); // return; // } $set('pCodeError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('pCodeError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('pCodeError') ? $get('pCodeError') : null) ->hintColor('danger'), Forms\Components\Select::make('company_id') // ->placeholder('Choose the valid company name') ->relationship('company', 'name') ->required() ->reactive() ->default(function () { return optional(Plant::latest()->first())->company_id; }) ->disabled(fn (Get $get) => !empty($get('id'))) ->afterStateUpdated(function ($state, callable $set, callable $get) { $companyId = $get('company_id'); // Ensure `linestop_id` is not cleared if (!$companyId) { $set('pCompanyError', 'Please select a company first.'); return; } else { $set('pCompanyError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('pCompanyError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('pCompanyError') ? $get('pCompanyError') : null) ->hintColor('danger'), Forms\Components\TextInput::make('name') ->required() ->placeholder('Scan the valid name') ->unique(ignoreRecord: true) ->columnSpanFull() ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { $nameId = $get('name'); // Ensure `linestop_id` is not cleared if (!$nameId) { $set('pNameError', 'Scan the valid name.'); return; } else { $set('pNameError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('pNameError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('pNameError') ? $get('pNameError') : null) ->hintColor('danger'), Forms\Components\Textarea::make('address') ->required() ->placeholder('Scan the valid address') ->unique(ignoreRecord: true) ->columnSpanFull() ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { $nameId = $get('address'); // Ensure `linestop_id` is not cleared if (!$nameId) { $set('pAddressError', 'Scan the valid address.'); return; } else { $set('pAddressError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('pAddressError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('pAddressError') ? $get('pAddressError') : null) ->hintColor('danger'), Forms\Components\TextInput::make('id') ->hidden() ->readOnly(), ]) ->columns(2), ]); } public static function table(Table $table): Table { return $table ->columns([ // Tables\Columns\TextColumn::make('id') // ->label('ID') // ->numeric() // ->sortable(), Tables\Columns\TextColumn::make('No.') ->label('No.') ->getStateUsing(function ($record, $livewire, $column, $rowLoop) { $paginator = $livewire->getTableRecords(); $perPage = method_exists($paginator, 'perPage') ? $paginator->perPage() : 10; $currentPage = method_exists($paginator, 'currentPage') ? $paginator->currentPage() : 1; return ($currentPage - 1) * $perPage + $rowLoop->iteration; }), Tables\Columns\TextColumn::make('code') ->label('Code') ->alignCenter() ->sortable() ->searchable(), Tables\Columns\TextColumn::make('name') ->label('Plant') ->alignCenter() ->sortable() ->searchable(), Tables\Columns\TextColumn::make('address') ->label('Address') ->alignCenter() ->sortable() ->searchable(), Tables\Columns\TextColumn::make('company.name') ->label('Company') ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('created_at') ->label('Created At') ->dateTime() ->alignCenter() ->sortable(), Tables\Columns\TextColumn::make('updated_at') ->label('Updated At') ->dateTime() ->alignCenter() ->sortable() ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('deleted_at') ->label('Deleted At') ->dateTime() ->alignCenter() ->sortable() ->toggleable(isToggledHiddenByDefault: true), ]) ->filters([ Tables\Filters\TrashedFilter::make(), ]) ->actions([ Tables\Actions\ViewAction::make(), Tables\Actions\EditAction::make(), ]) ->bulkActions([ Tables\Actions\BulkActionGroup::make([ Tables\Actions\DeleteBulkAction::make(), Tables\Actions\ForceDeleteBulkAction::make(), Tables\Actions\RestoreBulkAction::make(), ]), ]) ->headerActions([ ImportAction::make() ->importer(PlantImporter::class) ->visible(function() { return Filament::auth()->user()->can('view import plant'); }), ExportAction::make() ->exporter(PlantExporter::class) ->visible(function() { return Filament::auth()->user()->can('view export plant'); }), ]); } public static function getRelations(): array { return [ // ]; } public static function getPages(): array { return [ 'index' => Pages\ListPlants::route('/'), 'create' => Pages\CreatePlant::route('/create'), 'view' => Pages\ViewPlant::route('/{record}'), 'edit' => Pages\EditPlant::route('/{record}/edit'), ]; } public static function getEloquentQuery(): Builder { return parent::getEloquentQuery() ->withoutGlobalScopes([ SoftDeletingScope::class, ]); } }