139 lines
3.7 KiB
PHP
139 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Client;
|
|
|
|
use App\Models\Ticket;
|
|
use App\Models\TicketMessage;
|
|
use App\Services\TicketService;
|
|
use App\Support\Settings;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Livewire\Attributes\Computed;
|
|
use Livewire\Component;
|
|
use Livewire\WithFileUploads;
|
|
|
|
class TicketShow extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
public Ticket $ticket;
|
|
|
|
public string $reply = '';
|
|
|
|
public array $attachments = [];
|
|
|
|
public ?int $editingMessageId = null;
|
|
|
|
public string $editingDraft = '';
|
|
|
|
public ?int $pendingDeleteMessageId = null;
|
|
|
|
public function mount(Ticket $ticket): void
|
|
{
|
|
abort_unless($ticket->customer_id === Auth::id(), 403);
|
|
|
|
$this->ticket = $ticket;
|
|
}
|
|
|
|
#[Computed]
|
|
public function ticketMessages()
|
|
{
|
|
return $this->ticket->publicMessages()->with(['author', 'authorLink.role', 'attachments'])->get();
|
|
}
|
|
|
|
#[Computed]
|
|
public function otherTickets()
|
|
{
|
|
return Auth::user()->ticketsAsCustomer()->where('id', '!=', $this->ticket->id)->get();
|
|
}
|
|
|
|
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 sendReply(): void
|
|
{
|
|
$this->validate(['reply' => 'required|string']);
|
|
|
|
app(TicketService::class)->clientReply($this->ticket, Auth::user(), $this->reply, $this->attachments);
|
|
$this->reset(['reply', 'attachments']);
|
|
unset($this->ticketMessages);
|
|
$this->ticket->refresh();
|
|
}
|
|
|
|
public function close(): void
|
|
{
|
|
app(TicketService::class)->setStatus($this->ticket, 'closed');
|
|
$this->ticket->refresh();
|
|
}
|
|
|
|
public function reopen(): void
|
|
{
|
|
app(TicketService::class)->setStatus($this->ticket, 'open');
|
|
$this->ticket->refresh();
|
|
}
|
|
|
|
public function startEdit(int $messageId, string $body): void
|
|
{
|
|
$message = TicketMessage::query()->findOrFail($messageId);
|
|
abort_unless($message->ticket_id === $this->ticket->id && $message->author_id === Auth::id() && $message->role === 'client', 403);
|
|
|
|
$this->editingMessageId = $messageId;
|
|
$this->editingDraft = $body;
|
|
}
|
|
|
|
public function cancelEdit(): void
|
|
{
|
|
$this->editingMessageId = null;
|
|
$this->editingDraft = '';
|
|
}
|
|
|
|
public function saveEdit(): void
|
|
{
|
|
$message = TicketMessage::query()->findOrFail($this->editingMessageId);
|
|
abort_unless($message->ticket_id === $this->ticket->id && $message->author_id === Auth::id() && $message->role === 'client', 403);
|
|
|
|
$message->update(['body' => $this->editingDraft, 'edited' => true]);
|
|
$this->cancelEdit();
|
|
unset($this->ticketMessages);
|
|
}
|
|
|
|
public function requestDelete(int $messageId): void
|
|
{
|
|
$this->pendingDeleteMessageId = $messageId;
|
|
}
|
|
|
|
public function cancelDelete(): void
|
|
{
|
|
$this->pendingDeleteMessageId = null;
|
|
}
|
|
|
|
public function confirmDelete(): void
|
|
{
|
|
$message = TicketMessage::query()->findOrFail($this->pendingDeleteMessageId);
|
|
abort_unless($message->ticket_id === $this->ticket->id && $message->author_id === Auth::id() && $message->role === 'client', 403);
|
|
|
|
$message->delete();
|
|
$this->pendingDeleteMessageId = null;
|
|
unset($this->ticketMessages);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.client.ticket-show');
|
|
}
|
|
}
|