2025-05-28 09:24:03 +08:00
|
|
|
<?php
|
|
|
|
|
2025-06-11 17:37:31 +08:00
|
|
|
namespace App\Livewire\Forms\Modals;
|
2025-05-28 09:24:03 +08:00
|
|
|
|
|
|
|
use App\Models\Room;
|
2025-05-28 10:21:05 +08:00
|
|
|
use App\Models\Branch;
|
2025-05-28 09:24:03 +08:00
|
|
|
|
|
|
|
use Livewire\Component;
|
|
|
|
use App\Services\ApiClient;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use WireUi\Traits\WireUiActions;
|
|
|
|
|
|
|
|
class RoomDetailModal extends Component
|
|
|
|
{
|
|
|
|
use WireUiActions;
|
|
|
|
|
|
|
|
protected $listeners = [
|
|
|
|
'openModal', 'closeModal',
|
|
|
|
'startNotify', 'stopNotify', 'fireNotify',
|
|
|
|
'openAccountNotify','closeAccountNotify'
|
|
|
|
];
|
|
|
|
|
2025-06-04 16:11:45 +08:00
|
|
|
public $room_name;
|
2025-05-28 10:21:05 +08:00
|
|
|
public $branch;
|
2025-05-28 09:24:03 +08:00
|
|
|
public bool $showModal = false;
|
|
|
|
|
|
|
|
public function openModal($roomId)
|
|
|
|
{
|
2025-06-04 16:11:45 +08:00
|
|
|
$room = Room::find($roomId);
|
|
|
|
$this->room_name=$room->type->value . $room->name;
|
2025-06-05 11:15:24 +08:00
|
|
|
$this->branch = Branch::find($room->branch_id);
|
2025-05-28 09:24:03 +08:00
|
|
|
$this->showModal = true;
|
|
|
|
}
|
|
|
|
public function closeModal()
|
|
|
|
{
|
|
|
|
$this->showModal = false;
|
|
|
|
}
|
|
|
|
public function startNotify()
|
|
|
|
{
|
|
|
|
$data = $this->buildNotifyData('active', now(), null);
|
|
|
|
$this->send($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stopNotify()
|
|
|
|
{
|
|
|
|
$data = $this->buildNotifyData('closed', null, null);
|
|
|
|
$chk =$this->send($data);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fireNotify()
|
|
|
|
{
|
|
|
|
$data = $this->buildNotifyData('fire', null, null);
|
|
|
|
$this->send($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function openAccountNotify()
|
|
|
|
{
|
|
|
|
$data = $this->buildNotifyData('active', now(), null);
|
|
|
|
$this->send($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function closeAccountNotify()
|
|
|
|
{
|
|
|
|
$data = $this->buildNotifyData('closed', now(), null);
|
|
|
|
$this->send($data);
|
|
|
|
}
|
|
|
|
protected function buildNotifyData(string $command, $startedAt = null, $endedAt = null): array
|
|
|
|
{
|
2025-05-28 10:21:05 +08:00
|
|
|
|
2025-05-28 09:24:03 +08:00
|
|
|
return [
|
2025-05-28 10:21:05 +08:00
|
|
|
'branch_name' => $this->branch->name ?? '',
|
2025-06-04 16:11:45 +08:00
|
|
|
'room_name' => $this->room_name ?? '',
|
2025-05-28 09:24:03 +08:00
|
|
|
'command' => $command,
|
|
|
|
'started_at' => $startedAt ? $startedAt->toDateTimeString() : null,
|
|
|
|
'ended_at' => $endedAt ? $endedAt->toDateTimeString() : null,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function send(array $data){
|
|
|
|
|
|
|
|
$user = Auth::user();
|
|
|
|
$token = $user->api_plain_token ?? null;
|
|
|
|
|
|
|
|
if (!$token) {
|
|
|
|
$this->sendErrorNotification('api', 'API token is missing.');
|
|
|
|
return false;
|
|
|
|
}
|
2025-06-03 17:29:50 +08:00
|
|
|
$apiClient = new ApiClient($this->branch->external_ip,$token);
|
|
|
|
$response = $apiClient->post('/api/room/sendSwitch', $data);
|
2025-05-28 09:24:03 +08:00
|
|
|
if ($response->failed()) {
|
|
|
|
$this->sendErrorNotification('api', 'API request failed: ' . $response->body());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// ✅ 成功提示
|
|
|
|
$this->notification()->send([
|
|
|
|
'icon' => 'success',
|
|
|
|
'title' => '成功',
|
|
|
|
'description' => '命令已成功發送:' . $data['command'],
|
|
|
|
]);
|
|
|
|
// ✅ 關閉 modal
|
|
|
|
$this->showModal = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
public function sendErrorNotification(string $title = '錯誤', string $description = '發生未知錯誤')
|
|
|
|
{
|
|
|
|
$this->notification()->send([
|
|
|
|
'icon' => 'error',
|
|
|
|
'title' => $title,
|
|
|
|
'description' =>$description,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function render()
|
|
|
|
{
|
2025-06-11 17:37:31 +08:00
|
|
|
return view('livewire.forms.modals.room-detail-modal');
|
2025-05-28 09:24:03 +08:00
|
|
|
}
|
|
|
|
}
|