Dr-Panel/app/Filament/Resources/MedicationResource.php

203 lines
7.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Filament\Resources;
use App\Filament\Resources\MedicationResource\Pages;
use App\Models\Medication;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
class MedicationResource extends Resource
{
protected static ?string $model = Medication::class;
protected static ?string $recordTitleAttribute = 'name';
public static function getNavigationIcon(): \BackedEnum|string|null
{
return 'heroicon-o-beaker';
}
public static function getNavigationGroup(): ?string
{
return __('navigation.groups.diseases');
}
public static function getNavigationSort(): ?int
{
return 3;
}
public static function getNavigationLabel(): string
{
return __('navigation.medications.title');
}
public static function getModelLabel(): string
{
return __('navigation.medications.singular');
}
public static function getPluralModelLabel(): string
{
return __('navigation.medications.title');
}
public static function form(Schema $schema): Schema
{
return $schema
->components([
Grid::make()
->schema([
Section::make(__('medications.sections.info'))
->schema([
TextInput::make('name')
->label(__('medications.fields.name'))
->required()
->maxLength(255),
TextInput::make('dosage_form')
->label(__('medications.fields.dosage_form'))
->maxLength(50)
->placeholder('Tab, Cap, Amp, Drop, Serum, Cream ...'),
TextInput::make('strength')
->label(__('medications.fields.strength'))
->maxLength(100)
->placeholder('500mg, 5mg/5ml ...'),
TextInput::make('quantity')
->label(__('medications.fields.quantity'))
->numeric()
->minValue(0),
TextInput::make('timing')
->label(__('medications.fields.timing'))
->maxLength(100)
->placeholder('q8h, q12h, q24h ...'),
Textarea::make('notes')
->label(__('medications.fields.notes'))
->rows(2)
->columnSpanFull(),
Toggle::make('is_default')
->label(__('medications.fields.is_default')),
Toggle::make('is_active')
->label(__('medications.fields.is_active'))
->default(true),
])
->columns(3)
->columnSpanFull()
->footerActions([
fn (string $operation) => Action::make('create')
->label(__('filament-panels::resources/pages/create-record.form.actions.create.label'))
->submit('create')
->keyBindings(['mod+s'])
->visible($operation === 'create'),
fn (string $operation) => Action::make('createAnother')
->label(__('filament-panels::resources/pages/create-record.form.actions.create_another.label'))
->action('createAnother')
->keyBindings(['mod+shift+s'])
->color('gray')
->visible($operation === 'create'),
fn (string $operation) => Action::make('cancelCreate')
->label(__('filament-panels::resources/pages/create-record.form.actions.cancel.label'))
->url(static::getUrl('index'))
->color('gray')
->visible($operation === 'create'),
fn (string $operation) => Action::make('save')
->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
->submit('save')
->keyBindings(['mod+s'])
->visible($operation === 'edit'),
fn (string $operation) => Action::make('cancelEdit')
->label(__('filament-panels::resources/pages/edit-record.form.actions.cancel.label'))
->url(static::getUrl('index'))
->color('gray')
->visible($operation === 'edit'),
]),
])
->columnSpanFull(),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('name')
->label(__('medications.fields.name'))
->searchable()
->sortable(),
TextColumn::make('dosage_form')
->label(__('medications.fields.dosage_form'))
->badge()
->placeholder('-'),
TextColumn::make('strength')
->label(__('medications.fields.strength'))
->placeholder('-'),
TextColumn::make('quantity')
->label(__('medications.fields.quantity'))
->placeholder('-'),
TextColumn::make('timing')
->label(__('medications.fields.timing'))
->placeholder('-'),
IconColumn::make('is_default')
->label(__('medications.fields.is_default'))
->boolean(),
IconColumn::make('is_active')
->label(__('medications.fields.is_active'))
->boolean(),
TextColumn::make('created_at')
->label(__('medications.fields.created_at'))
->sortable(),
])
->defaultSort('name')
->recordActions([
EditAction::make(),
DeleteAction::make(),
])
->toolbarActions([
\Filament\Actions\BulkActionGroup::make([
DeleteBulkAction::make(),
]),
]);
}
public static function getPages(): array
{
return [
'index' => Pages\ListMedications::route('/'),
'create' => Pages\CreateMedication::route('/create'),
'edit' => Pages\EditMedication::route('/{record}/edit'),
];
}
}