suggestedArticlesLoaded = true; } #[Computed] public function clients() { return User::query()->withRole('client')->orderBy('name')->get(); } #[Computed] public function categories() { return Category::query()->with('subcategories')->get(); } #[Computed] public function selectedCategory(): ?Category { return $this->categoryId ? Category::query()->with('subcategories')->find($this->categoryId) : null; } #[Computed] public function selectedSubcategory(): ?Subcategory { return $this->subcategoryId ? Subcategory::query()->with('customFields')->find($this->subcategoryId) : null; } public function selectCategory(int $id): void { $this->categoryId = $id; $this->subcategoryId = null; $this->step = 2; } public function selectSubcategory(int $id): void { $this->subcategoryId = $id; $this->step = 3; } /** * @return array */ #[Computed] public function suggestedArticles(): array { if (! $this->suggestedArticlesLoaded) { return []; } $query = trim(($this->selectedCategory?->name ?? '').' '.($this->selectedSubcategory?->name ?? '')); return app(BookStackClient::class)->search($query); } public function backToCategory(): void { $this->step = 1; } public function backToSubcategory(): void { $this->step = 2; } public function updatedAttachments(): void { if (! $this->attachments) { return; } if ($error = Settings::validateAttachments($this->attachments)) { $this->attachments = []; $this->addError('attachments', $error); } } public function removeAttachment(int $index): void { unset($this->attachments[$index]); $this->attachments = array_values($this->attachments); } public function submit(): void { $rules = [ 'customerId' => 'required|exists:users,id', 'subject' => 'required|string|max:255', 'body' => 'required|string', ]; foreach ($this->selectedSubcategory?->customFields ?? [] as $field) { $rules["customValues.{$field->id}"] = $field->required ? 'required' : 'nullable'; } $this->validate($rules); $customer = User::query()->findOrFail($this->customerId); $ticket = app(TicketService::class)->create([ 'email' => $customer->email, 'subcategory_id' => $this->subcategoryId, 'subject' => $this->subject, 'body' => $this->body, 'custom_values' => $this->customValues, 'assignee_id' => Auth::id(), ], $customer); $ticket->messages()->first()?->update([ 'body' => $ticket->body.' (zgłoszenie utworzone przez operatora w imieniu klienta)', ]); app(TicketService::class)->attachFiles($ticket, $ticket->messages()->first(), $this->attachments); $this->redirect(route('operator.ticket', $ticket), navigate: true); } public function render() { return view('livewire.operator.new-ticket'); } }