KTV/app/Livewire/Forms/TextAdTestForm.php

81 lines
2.1 KiB
PHP

<?php
namespace App\Livewire\Forms;
use Livewire\Component;
use WireUi\Traits\WireUiActions;
use App\Models\Room;
use App\Models\TextAd;
use App\Services\TcpSocketClient;
class TextAdTestForm extends Component
{
use WireUiActions;
protected $listeners = ['openModal','closeModal'];
public bool $showModal = false;
public ?string $prefix ="";
public ?string $content = "";
public ?int $roomId = null;
public array $roomOptions =[];
public function mount()
{
$this->roomOptions = Room::where('type', '!=', 'svr')->get()->map(fn ($room) => [
'name' => $room->type->value.$room->name,
'value' => $room->id,
])->toArray();
}
public function openModal($id = null)
{
$textAd=TextAd::findOrFail($id);
$this->prefix = "({$textAd->color->labels()})-測試:";
$this->content = $textAd->content;
$this->showModal = true;
}
public function closeModal()
{
$this->textAd=null;
$this->resetFields();
$this->showModal = false;
}
public function send()
{
$room = Room::find($this->roomId);
$roomCode = str_pad($room->name, 4, '0', STR_PAD_LEFT);
try {
$client = new TcpSocketClient($room->internal_ip, $room->port);
$client->send($roomCode.$this->prefix.$this->content);
$this->notification()->send([
'icon' => 'success',
'title' => '成功',
'description' => "✅ 已送出至房間 {$room->name}",
]);
} catch (\Throwable $e) {
$this->notification()->send([
'icon' => 'error',
'title' => '失敗',
'description' => "❌ 發送失敗:{$e->getMessage()}",
]);
}
$this->resetFields();
$this->showModal = false;
$this->dispatch('pg:eventRefresh-text-ads-table');
}
public function resetFields()
{
$this->content='';
$this->roomId=null;
}
public function render()
{
return view('livewire.forms.text-ad-test-form');
}
}