latest()->first(); if ($visitor) { $employee = EmployeeMaster::where('id', $visitor->employee_master_id)->first(); $this->form->fill([ 'mobile_number' => $mobile ?? '', 'name' => $visitor->name ?? '', 'company' => $visitor->company ?? '', 'type' => $visitor->type ?? '', 'department' => $employee->department ?? '', 'employee_master_id' => $visitor->employee_master_id->name ?? '', 'code' => $employee->code ?? '', ]); } else { $this->form->fill([ 'mobile_number' => $mobile ?? '', 'name' => $visitor->name ?? '', 'company' => $visitor->company ?? '', 'type' => $visitor->type ?? '', 'department' => $employee->department ?? '', 'employee_master_id' => $visitor->employee_master_id->name ?? '', 'code' => $employee->code ?? '', ]); } } #[On('photo-captured')] public function handlePhotoCapture(string $photo): void { $this->data['photo'] = $photo; \Log::info('WEBCAM: photo-captured event received, length: ' . strlen($photo)); } protected function mutateFormDataBeforeCreate(array $data): array { if ( !empty($data['photo']) && str_starts_with($data['photo'], 'data:image') ) { try { $imageData = explode(',', $data['photo'])[1]; $filename = 'visitor_' . time() . '_' . uniqid() . '.jpg'; $path = 'visitor-photos/' . $filename; $decoded = base64_decode($imageData); $saved = Storage::disk('public')->put($path, $decoded); $data['photo'] = $path; } catch (\Exception $e) { \Log::error('PHOTO UPLOAD ERROR: ' . $e->getMessage()); } } return $data; } public function setPhoto(string $photo): void { $this->capturedPhoto = $photo; $this->dispatch('photo-captured', photo: $photo)->to(\App\Filament\Resources\VisitorEntryResource\Pages\CreateVisitorEntry::class); } // ── Custom form action buttons ── protected function getFormActions(): array { return [ $this->getCreateFormAction(), $this->getCreateAnotherFormAction(), Action::make('createAndPrint') ->label('Create & Print') ->color('success') ->icon('heroicon-o-printer') ->action(function () { // Save the record using existing logic (calls mutateFormDataBeforeCreate internally) $this->create(); // Open the badge print page in a new tab $this->js("window.open('" . route('visitor.badge', $this->record->id) . "', '_blank')"); }), $this->getCancelFormAction(), ]; } protected function getRedirectUrl(): string { return $this->getResource()::getUrl('view', ['record' => $this->record]); } }