matab-panel/app/Filament/Resources/SurgeryAppointmentResource.php

195 lines
7.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Filament\Resources;
use App\Filament\Resources\SurgeryAppointmentResource\Pages;
use App\Models\Patient;
use App\Models\SurgeryAppointment;
use App\Models\SurgeryCenter;
use Filament\Actions\Action;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TimePicker;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Morilog\Jalali\Jalalian;
class SurgeryAppointmentResource extends Resource
{
protected static ?string $model = SurgeryAppointment::class;
protected static bool $isGloballySearchable = false;
public static function getNavigationIcon(): \BackedEnum|string|null
{
return 'heroicon-o-calendar-days';
}
public static function getNavigationGroup(): ?string
{
return __('navigation.groups.patients');
}
public static function getNavigationSort(): ?int
{
return 3;
}
public static function getNavigationLabel(): string
{
return __('navigation.surgery_appointments.title');
}
public static function getModelLabel(): string
{
return __('navigation.surgery_appointments.singular');
}
public static function getPluralModelLabel(): string
{
return __('navigation.surgery_appointments.title');
}
public static function form(Schema $schema): Schema
{
return $schema
->components([
Grid::make()
->schema([
Section::make(__('surgery_appointments.sections.info'))
->schema([
Select::make('patient_id')
->label(__('surgery_appointments.fields.patient'))
->options(fn () => Patient::all()->mapWithKeys(
fn ($p) => [$p->id => $p->full_name]
)->toArray())
->searchable()
->required(),
Select::make('surgery_center_id')
->label(__('surgery_appointments.fields.surgery_center'))
->options(fn () => SurgeryCenter::pluck('name', 'id')->toArray())
->searchable()
->required(),
DatePicker::make('surgery_date_date')
->label(__('surgery_appointments.fields.surgery_date'))
->jalali()
->minDate(today())
->required(),
TimePicker::make('surgery_date_time')
->label(__('surgery_appointments.fields.surgery_time'))
->native(false)
->seconds(false)
->minutesStep(15)
->default('09:00'),
])
->columns(2)
->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('patient.first_name')
->label(__('surgery_appointments.fields.patient'))
->formatStateUsing(fn ($record) => $record->patient?->full_name ?? '-')
->searchable()
->sortable(),
TextColumn::make('surgeryCenter.name')
->label(__('surgery_appointments.fields.surgery_center'))
->searchable()
->sortable()
->placeholder('-'),
TextColumn::make('surgery_date')
->label(__('surgery_appointments.fields.surgery_date'))
->formatStateUsing(function ($state) {
if (! $state) {
return '-';
}
try {
$date = \Carbon\Carbon::parse($state);
return Jalalian::fromCarbon($date)->format('Y/m/d - H:i');
} catch (\Exception $e) {
return $state;
}
})
->sortable(),
TextColumn::make('creator.name')
->label(__('surgery_appointments.fields.created_by'))
->placeholder('-'),
TextColumn::make('created_at')
->label(__('surgery_appointments.fields.created_at'))
->sortable(),
])
->defaultSort('surgery_date', 'desc')
->recordActions([
EditAction::make(),
DeleteAction::make(),
])
->toolbarActions([
DeleteBulkAction::make(),
]);
}
public static function getPages(): array
{
return [
'index' => Pages\ListSurgeryAppointments::route('/'),
'create' => Pages\CreateSurgeryAppointment::route('/create'),
'edit' => Pages\EditSurgeryAppointment::route('/{record}/edit'),
];
}
}