['required', 'exists:users,id'], 'caseName' => ['required', 'string', 'max:255'], 'caseReference' => ['nullable', 'string', 'max:50', 'unique:timelines,case_reference'], ]; } public function messages(): array { return [ 'selectedUserId.required' => __('timelines.client_required'), 'caseReference.unique' => __('timelines.case_reference_exists'), ]; } public function updatedSearch(): void { if ($this->selectedUser && ! str_contains(strtolower($this->selectedUser->full_name), strtolower($this->search))) { $this->selectedUserId = null; $this->selectedUser = null; } } public function getClientsProperty() { if (strlen($this->search) < 2) { return collect(); } return User::query() ->clients() ->active() ->where(function ($query) { $query->where('full_name', 'like', "%{$this->search}%") ->orWhere('email', 'like', "%{$this->search}%"); }) ->limit(10) ->get(); } public function selectUser(int $userId): void { $this->selectedUserId = $userId; $this->selectedUser = User::find($userId); $this->search = $this->selectedUser->full_name; } public function clearSelection(): void { $this->selectedUserId = null; $this->selectedUser = null; $this->search = ''; } public function create(): void { $this->validate(); $timeline = Timeline::create([ 'user_id' => $this->selectedUserId, 'case_name' => $this->caseName, 'case_reference' => $this->caseReference ?: null, 'status' => TimelineStatus::Active, ]); if ($this->initialNotes) { $timeline->updates()->create([ 'admin_id' => auth()->id(), 'update_text' => $this->initialNotes, ]); } AdminLog::create([ 'admin_id' => auth()->id(), 'action' => 'create', 'target_type' => 'timeline', 'target_id' => $timeline->id, 'new_values' => $timeline->toArray(), 'ip_address' => request()->ip(), 'created_at' => now(), ]); session()->flash('success', __('messages.timeline_created')); $this->redirect(route('admin.dashboard'), navigate: true); } }; ?>