222 lines
9.6 KiB
PHP
222 lines
9.6 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources;
|
|
|
|
use App\Filament\Imports\LineImporter;
|
|
use App\Filament\Resources\LineResource\Pages;
|
|
use App\Filament\Resources\LineResource\RelationManagers;
|
|
use App\Models\Line;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Form;
|
|
use Filament\Forms\Get;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Actions\ImportAction;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
|
use Filament\Forms\Components\Section;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Rules\Unique;
|
|
|
|
class LineResource extends Resource
|
|
{
|
|
protected static ?string $model = Line::class;
|
|
|
|
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
|
|
|
|
protected static ?string $navigationGroup = 'Master Entries';
|
|
protected static ?int $navigationSort = 5;
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
Section::make('')
|
|
->schema([
|
|
Forms\Components\Select::make('plant_id')
|
|
->relationship('plant', 'name')
|
|
->required()
|
|
// ->nullable(),
|
|
->reactive()
|
|
->default(function () {
|
|
return optional(Line::latest()->first())->plant_id;
|
|
})
|
|
->disabled(fn (Get $get) => !empty($get('id')))
|
|
// ->afterStateUpdated(fn ($set) => $set('block_id', null) & $set('name', null) & $set('start_time', null) & $set('duration', null) & $set('end_time', null))
|
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
|
$plantId = $get('plant_id');
|
|
// Ensure `linestop_id` is not cleared
|
|
if (!$plantId) {
|
|
$set('lPlantError', 'Please select a plant first.');
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
$set('lPlantError', null);
|
|
}
|
|
})
|
|
->extraAttributes(fn ($get) => [
|
|
'class' => $get('lPlantError') ? 'border-red-500' : '',
|
|
])
|
|
->hint(fn ($get) => $get('lPlantError') ? $get('lPlantError') : null)
|
|
->hintColor('danger'),
|
|
Forms\Components\TextInput::make('name')
|
|
->required()
|
|
->placeholder('Scan the valid name')
|
|
->autofocus(true)
|
|
// ->unique(
|
|
// ignoreRecord: true,
|
|
// modifyRuleUsing: function (Unique $rule) {
|
|
// return $rule->where('plant_id', $this->data['plant_id']);
|
|
// }
|
|
// )
|
|
// ->rule(function () {
|
|
// return function ($attribute, $value, $fail) {
|
|
// $exists = Line::where('name', $value)
|
|
// ->where('plant_id', request()->input('plant_id'))
|
|
// ->exists();
|
|
|
|
// if ($exists) {
|
|
// $fail('The combination of name and plant ID must be unique.');
|
|
// }
|
|
// };
|
|
// })
|
|
->reactive()
|
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
|
$lineNam = $get('name');
|
|
// Ensure `linestop_id` is not cleared
|
|
if (!$lineNam) {
|
|
$set('lNameError', 'Scan the valid name.');
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
// $exists = Line::where('name', $lineNam)
|
|
// ->where('plant_id', $get('plant_id'))
|
|
// ->exists();
|
|
|
|
// if ($exists) {
|
|
// $set('name', null);
|
|
// $set('lNameError', 'The name has already been taken.');
|
|
// // $set('lNameError', 'The combination of name and plant ID must be unique.');
|
|
// return;
|
|
// }
|
|
$set('lNameError', null);
|
|
}
|
|
})
|
|
->extraAttributes(fn ($get) => [
|
|
'class' => $get('lNameError') ? 'border-red-500' : '',
|
|
])
|
|
->hint(fn ($get) => $get('lNameError') ? $get('lNameError') : null)
|
|
->hintColor('danger')
|
|
->rule(function (callable $get) {
|
|
return Rule::unique('lines', 'name')
|
|
->where('plant_id', $get('plant_id'))
|
|
->ignore($get('id')); // Ignore current record during updates
|
|
}),
|
|
Forms\Components\TextInput::make('type')
|
|
->required()
|
|
->placeholder('Scan the valid type')
|
|
->reactive()
|
|
->afterStateUpdated(function ($state, callable $set, callable $get) {
|
|
$lineTyp = $get('type');
|
|
// Ensure `linestop_id` is not cleared
|
|
if (!$lineTyp) {
|
|
$set('lTypeError', 'Scan the valid type.');
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
$set('lTypeError', null);
|
|
}
|
|
})
|
|
->extraAttributes(fn ($get) => [
|
|
'class' => $get('lTypeError') ? 'border-red-500' : '',
|
|
])
|
|
->hint(fn ($get) => $get('lTypeError') ? $get('lTypeError') : 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('name')
|
|
->sortable()
|
|
->searchable(),
|
|
Tables\Columns\TextColumn::make('type')
|
|
->sortable()
|
|
->searchable(),
|
|
Tables\Columns\TextColumn::make('plant.name')
|
|
->sortable()
|
|
->searchable(),
|
|
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(LineImporter::class),
|
|
]);
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
//
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListLines::route('/'),
|
|
'create' => Pages\CreateLine::route('/create'),
|
|
'view' => Pages\ViewLine::route('/{record}'),
|
|
'edit' => Pages\EditLine::route('/{record}/edit'),
|
|
];
|
|
}
|
|
|
|
public static function getEloquentQuery(): Builder
|
|
{
|
|
return parent::getEloquentQuery()
|
|
->withoutGlobalScopes([
|
|
SoftDeletingScope::class,
|
|
]);
|
|
}
|
|
}
|