This commit is contained in:
2026-07-21 23:39:19 +02:00
commit b33b217bdb
217 changed files with 32076 additions and 0 deletions

View File

@@ -0,0 +1,138 @@
<?php
namespace App\Livewire\Operator;
use App\Models\Category;
use App\Models\Subcategory;
use App\Models\User;
use App\Services\TicketService;
use App\Support\Settings;
use Illuminate\Support\Facades\Auth;
use Livewire\Attributes\Computed;
use Livewire\Component;
use Livewire\WithFileUploads;
class NewTicket extends Component
{
use WithFileUploads;
public int $step = 1;
public ?int $customerId = null;
public ?int $categoryId = null;
public ?int $subcategoryId = null;
public string $subject = '';
public string $body = '';
public array $customValues = [];
public array $attachments = [];
#[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;
}
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');
}
}