schema([ Forms\Components\Select::make('plant_id') ->relationship('plant', 'name') ->required() ->nullable() ->reactive(), Forms\Components\Select::make('block_name') ->required() ->nullable() ->reactive() ->afterStateUpdated(fn ($set) => $set('shift_id', null)), Forms\Components\Select::make('shift_id') ->relationship('shift', 'name') ->required() ->nullable() // ->options(fn (callable $get) => // \App\Models\Shift::where('plant_id', $get('plant_id')) // ->pluck('name', 'id') // ->toArray() // Convert collection to array // ) ->options(function (callable $get) { if (!$get('plant_id')) { return []; } return \App\Models\Shift::where('plant_id', $get('plant_id')) ->pluck('name', 'id') ->toArray(); }) ->reactive() ->afterStateUpdated(fn ($set) => $set('line_id', null)), 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')) { return []; } return \App\Models\Line::where('plant_id', $get('plant_id')) ->pluck('name', 'id') ->toArray(); }) ->reactive(), // Forms\Components\Select::make('linestop_id') // ->label('Line Stop Code') // ->relationship('linestop', 'code') // ->searchable() // ->required() // ->nullable() // ->reactive(), Forms\Components\Select::make('linestop_id') ->label('Line Stop Code') // ->options(fn (callable $get) => // \App\Models\LineStop::where('id', $get('linestop_id')) // ->pluck('code', 'id') // ) ->options(fn () => \App\Models\LineStop::pluck('code', 'id')) ->required() ->nullable() // ->reactive() ->searchable() ->live(debounce: 500) // Enable live updates ->afterStateUpdated(function ($state, callable $set, callable $get) { $lineStopId = $get('linestop_id'); // Get entered linestop_id // Ensure `linestop_id` is not cleared if (!$lineStopId) { $set('lineStop_reason', null); return; } // Check if item exists for the selected plant $lineStop = \App\Models\LineStop::where('id', $lineStopId) ->where('id', $lineStopId) ->first(); $set('lineStop_reason', $lineStop?->reason ?? null); }), Forms\Components\TextInput::make('lineStop_reason') ->label('Line Stop Reason') ->required() ->reactive() ->readOnly(true), Forms\Components\DateTimePicker::make('from_datetime') ->label('From DateTime') ->required() ->reactive() ->afterStateUpdated(fn ($state, callable $set, callable $get) => self::updateStopDuration($get, $set) ), Forms\Components\DateTimePicker::make('to_datetime') ->label('To DateTime') ->required() ->reactive() ->afterStateUpdated(fn ($state, callable $set, callable $get) => self::updateStopDuration($get, $set) //self means it calling the function within the class ), Forms\Components\TextInput::make('stop_hour') ->required() // ->dehydrated(false) // Don't send to backend ->readOnly(true) ->numeric(), Forms\Components\TextInput::make('stop_min') ->required() // ->dehydrated(false) ->readOnly(true) ->numeric(), ]); } public static function updateStopDuration(callable $get, callable $set) { $from = $get('from_datetime'); $to = $get('to_datetime'); // Carbon is a PHP date and time library. if ($from && $to) { $fromTime = Carbon::parse($from); //Carbon::parse($from) converts the from datetime string into a Carbon objec $toTime = Carbon::parse($to); if ($fromTime->lt($toTime)) { $diffInMinutes = $fromTime->diffInMinutes($toTime); $set('stop_hour', floor($diffInMinutes / 60)); $set('stop_min', $diffInMinutes % 60); } else { $set('stop_hour', 0); $set('stop_min', 0); } } } public static function table(Table $table): Table { return $table ->columns([ Tables\Columns\TextColumn::make('id') ->label('ID') ->numeric() ->sortable(), Tables\Columns\TextColumn::make('linestop.code') ->sortable(), Tables\Columns\TextColumn::make('linestop.reason') ->sortable(), Tables\Columns\TextColumn::make('from_datetime') ->dateTime() ->sortable(), Tables\Columns\TextColumn::make('to_datetime') ->dateTime() ->sortable(), Tables\Columns\TextColumn::make('stop_hour') ->numeric() ->sortable(), Tables\Columns\TextColumn::make('stop_min') ->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(ProductionLineStopImporter::class), ]); } public static function getRelations(): array { return [ // ]; } public static function getPages(): array { return [ 'index' => Pages\ListProductionLineStops::route('/'), 'create' => Pages\CreateProductionLineStop::route('/create'), 'view' => Pages\ViewProductionLineStop::route('/{record}'), 'edit' => Pages\EditProductionLineStop::route('/{record}/edit'), ]; } public static function getEloquentQuery(): Builder { return parent::getEloquentQuery() ->withoutGlobalScopes([ SoftDeletingScope::class, ]); } }