schema([ Section::make('') ->schema([ Forms\Components\Select::make('plant_id') ->relationship('plant', 'name') ->required() // ->nullable() ->reactive() // ->afterStateUpdated(fn ($set) => $set('block_name', null)) ->afterStateUpdated(function ($state, callable $set, callable $get) { $plantId = $get('plant_id'); $set('block_name', null); if (!$plantId) { $set('ppPlantError', 'Please select a plant first.'); return; } else { $set('ppPlantError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('ppPlantError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('ppPlantError') ? $get('ppPlantError') : null) ->hintColor('danger'), Forms\Components\Select::make('block_name') ->required() // ->nullable() ->label('Block') ->options(function (callable $get) { if (!$get('plant_id')) { return []; } return \App\Models\Block::where('plant_id', $get('plant_id')) ->pluck('name', 'id') ->toArray(); }) ->reactive() //->afterStateUpdated(fn ($set) => $set('shift_id', null)) ->afterStateUpdated(function ($state, callable $set, callable $get) { $blockId = $get('block_name'); $set('shift_id', null); if (!$blockId) { $set('ppBlockError', 'Please select a block first.'); return; } else { $set('ppBlockError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('ppBlockError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('ppBlockError') ? $get('ppBlockError') : null) ->hintColor('danger'), Forms\Components\Select::make('shift_id') ->relationship('shift', 'name') ->required() // ->nullable() ->options(function (callable $get) { if (!$get('plant_id') || !$get('block_name')) { return []; } return \App\Models\Shift::where('plant_id', $get('plant_id')) ->where('block_id', $get('block_name')) ->pluck('name', 'id') ->toArray(); }) ->reactive() // ->afterStateUpdated(fn ($set) => $set('line_id', null)) ->afterStateUpdated(function ($state, callable $set, callable $get) { $shiftId = $get('shift_id'); $set('line_id', null); if (!$shiftId) { $set('ppShiftError', 'Please select a shift first.'); return; } else { $set('ppShiftError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('ppShiftError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('ppShiftError') ? $get('ppShiftError') : null) ->hintColor('danger'), Forms\Components\Select::make('line_id') ->relationship('line', 'name') ->required() // ->nullable() // ->options(fn (callable $get) => // \App\Models\Line::where('plant_id', $get('plant_id')) // ->pluck('name', 'id') // ->toArray() // Convert collection to array // ) ->options(function (callable $get) { if (!$get('plant_id') || !$get('block_name') || !$get('shift_id')) { return []; } return \App\Models\Line::where('plant_id', $get('plant_id')) ->pluck('name', 'id') ->toArray(); }) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { $lineId = $get('line_id'); if (!$lineId) { $set('ppLineError', 'Please select a line first.'); return; } else { $set('ppLineError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('ppLineError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('ppLineError') ? $get('ppLineError') : null) ->hintColor('danger'), Forms\Components\TextInput::make('plan_quantity') ->required() ->integer() ->label('Plan Quantity') ->placeholder('Scan the valid quantity') ->minValue(1) ->reactive() ->afterStateUpdated(function ($state, callable $set, callable $get) { $planQuan = $get('plan_quantity'); if (!$planQuan) { $set('ppPlanQuanError', 'Scan the valid plan quantity.'); return; } else { $set('ppPlanQuanError', null); } }) ->extraAttributes(fn ($get) => [ 'class' => $get('ppPlanQuanError') ? 'border-red-500' : '', ]) ->hint(fn ($get) => $get('ppPlanQuanError') ? $get('ppPlanQuanError') : null) ->hintColor('danger'), Forms\Components\TextInput::make('production_quantity') ->required() ->integer() ->label('Production Quantity') ->readOnly() ->default(0), ]) ->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('plan_quantity') ->numeric() ->sortable(), Tables\Columns\TextColumn::make('production_quantity') ->numeric() ->sortable(), Tables\Columns\TextColumn::make('line.name') ->sortable(), Tables\Columns\TextColumn::make('shift.name') ->sortable(), Tables\Columns\TextColumn::make('plant.name') ->sortable(), Tables\Columns\TextColumn::make('created_at') ->dateTime() ->sortable() ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('updated_at') ->dateTime() ->sortable() ->toggleable(isToggledHiddenByDefault: true), Tables\Columns\TextColumn::make('deleted_at') ->dateTime() ->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(ProductionPlanImporter::class), ]); } public static function getRelations(): array { return [ // ]; } public static function getPages(): array { return [ 'index' => Pages\ListProductionPlans::route('/'), 'create' => Pages\CreateProductionPlan::route('/create'), 'view' => Pages\ViewProductionPlan::route('/{record}'), 'edit' => Pages\EditProductionPlan::route('/{record}/edit'), ]; } public static function getEloquentQuery(): Builder { return parent::getEloquentQuery() ->withoutGlobalScopes([ SoftDeletingScope::class, ]); } }