KTVRemote/app/Livewire/Pages/OrderedSongList.php
2025-08-28 17:27:48 +08:00

59 lines
1.7 KiB
PHP

<?php
namespace App\Livewire\Pages;
use Livewire\Component;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use App\Models\RoomSession;
use App\Models\OrderedSong;
class OrderedSongList extends Component
{
public ?string $roomSessionId = null;
public EloquentCollection $playing ;
public EloquentCollection $nextSong ;
public EloquentCollection $finished ;
public function mount()
{
$this->playing = new EloquentCollection();
$this->nextSong = new EloquentCollection();
$this->finished = new EloquentCollection();
$roomSession = $this->getRoomSession(session('room_code'))?->id ;
if ($roomSession) {
$this->roomSessionId = $roomSession->id;
$this->loadSongs($this->roomSessionId);
}
}
private function getRoomSession($apiToken): ?RoomSession
{
if (!$apiToken) return null;
return RoomSession::where('api_token', $apiToken)
->whereIn('status', ['active', 'maintain'])
->first();
}
public function refreshSongs()
{
if ($this->roomSessionId) {
$this->loadSongs($this->roomSessionId);
}
}
protected function loadSongs(string $roomSessionId)
{
$this->playing = OrderedSong::forSession($roomSessionId)->playing()->get();
$this->nextSong = OrderedSong::forSession($roomSessionId)->nextSong()->get();
$this->finished = OrderedSong::forSession($roomSessionId)->finished()->get();
}
public function render()
{
return view('livewire.pages.ordered-song-list', [
'playing' => $this->playing,
'nextSong' => $this->nextSong,
'finished' => $this->finished,
]);
}
}