95 lines
3.3 KiB
PHP
95 lines
3.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Filament\Resources\SurgeryAppointmentResource\Pages;
|
|
|
|
use App\Filament\Resources\SurgeryAppointmentResource;
|
|
use App\Models\Patient;
|
|
use Filament\Notifications\Notification;
|
|
use Filament\Resources\Pages\CreateRecord;
|
|
use Filament\Support\Enums\Width;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Morilog\Jalali\Jalalian;
|
|
|
|
class CreateSurgeryAppointment extends CreateRecord
|
|
{
|
|
protected static string $resource = SurgeryAppointmentResource::class;
|
|
|
|
protected Width|string|null $maxContentWidth = Width::SevenExtraLarge;
|
|
|
|
protected function mutateFormDataBeforeCreate(array $data): array
|
|
{
|
|
$dateStr = $data['surgery_date_date'] ?? '';
|
|
$timeStr = $data['surgery_date_time'] ?? '09:00';
|
|
$data['surgery_date'] = \Carbon\Carbon::parse($dateStr . ' ' . $timeStr);
|
|
|
|
unset($data['surgery_date_date'], $data['surgery_date_time']);
|
|
|
|
return $data;
|
|
}
|
|
|
|
protected function afterCreate(): void
|
|
{
|
|
$data = $this->form->getState();
|
|
$dateStr = $data['surgery_date_date'] ?? $this->record->surgery_date?->format('Y-m-d') ?? '';
|
|
$timeStr = $data['surgery_date_time'] ?? $this->record->surgery_date?->format('H:i') ?? '09:00';
|
|
$surgeryDateTime = \Carbon\Carbon::parse($dateStr . ' ' . $timeStr);
|
|
$jalaliDate = Jalalian::fromCarbon($surgeryDateTime)->format('Y/m/d');
|
|
$patient = Patient::find($data['patient_id'] ?? $this->record->patient_id);
|
|
|
|
$apiKey = \App\Models\OsurgInitial::val('sms_api_key', config('sms.api_key'));
|
|
$apiUrl = \App\Models\OsurgInitial::val('sms_api_url', config('sms.api_url'));
|
|
|
|
try {
|
|
$response = Http::timeout(30)
|
|
->withHeader('X-API-Key', $apiKey)
|
|
->asJson()
|
|
->post($apiUrl . '/surgery-reminder', [
|
|
'phoneNumber' => $patient?->hand_phone ?? '',
|
|
'patientName' => $patient?->full_name ?? '',
|
|
'surgeryDate' => $jalaliDate,
|
|
'appointmentId' => $this->record->id,
|
|
'patientId' => $patient?->id,
|
|
'surgeryDateFull' => $surgeryDateTime->toDateTimeString(),
|
|
]);
|
|
|
|
if (! ($response->json('success') ?? false)) {
|
|
Notification::make()
|
|
->title(__('patients.actions.api_failed'))
|
|
->body($response->json('message') ?? '')
|
|
->danger()
|
|
->send();
|
|
|
|
return;
|
|
}
|
|
} catch (\Throwable $e) {
|
|
Log::warning('Surgery appointment SMS failed', [
|
|
'patient_id' => $patient?->id,
|
|
'appointment_id' => $this->record->id,
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
|
|
Notification::make()
|
|
->title(__('patients.actions.api_failed'))
|
|
->body($e->getMessage())
|
|
->danger()
|
|
->send();
|
|
|
|
return;
|
|
}
|
|
|
|
Notification::make()
|
|
->title(__('patients.actions.appointment_saved'))
|
|
->body(__('patients.actions.sms_sent'))
|
|
->success()
|
|
->send();
|
|
}
|
|
|
|
protected function getFormActions(): array
|
|
{
|
|
return [];
|
|
}
|
|
}
|