84 lines
2.1 KiB
PHP
84 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Pages;
|
|
|
|
use Livewire\Component;
|
|
use App\Models\Song;
|
|
|
|
class SearchSong extends Component
|
|
{
|
|
public string $search = '';
|
|
public $searchCategory = '';
|
|
public $selectedLanguage = '國語'; // 預設語言
|
|
|
|
/**
|
|
* 初始化(接收外部傳入的分類參數)
|
|
*/
|
|
public function mount(string $searchCategory = '')
|
|
{
|
|
$this->searchCategory = $searchCategory;
|
|
}
|
|
|
|
/**
|
|
* 切換語言標籤
|
|
*/
|
|
public function selectLanguage(string $lang): void
|
|
{
|
|
$this->selectedLanguage = $lang;
|
|
}
|
|
//public function updatedSearch()
|
|
//{
|
|
// dd($this->search);
|
|
//}
|
|
|
|
/**
|
|
* 點歌
|
|
*/
|
|
public function orderSong(int $songId): void
|
|
{
|
|
// TODO: 加入已點歌曲邏輯,例如:
|
|
// auth()->user()->room->addSong($songId);
|
|
|
|
$this->dispatchBrowserEvent('notify', [
|
|
'message' => '已加入已點歌曲'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 取得歌曲清單
|
|
*/
|
|
protected function loadSongs()
|
|
{
|
|
return Song::query()
|
|
->when($this->search !== null && $this->search !== '', function ($q) {
|
|
$q->where('name', 'like', "{$this->search}%");
|
|
})
|
|
->when($this->selectedLanguage, function ($q) {
|
|
$q->where('language_type', $this->selectedLanguage);
|
|
})
|
|
->when($this->searchCategory === 'New', function ($q) {
|
|
$q->orderByDesc('created_at');
|
|
})
|
|
->when($this->searchCategory === 'Hot', function ($q) {
|
|
$q->orderByDesc('song_counts');
|
|
}, function ($q) {
|
|
$q->orderBy('name');
|
|
})
|
|
->take($this->search ? 100 : 200) // 搜尋就多拿點,不搜尋就少拿
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Render
|
|
*/
|
|
public function render()
|
|
{
|
|
$songs = $this->loadSongs();
|
|
|
|
return view('livewire.pages.search-song', [
|
|
'songs' => $songs,
|
|
'languages' => \App\Enums\SongLanguageType::options(),
|
|
'selectedLanguage' => $this->selectedLanguage,
|
|
]);
|
|
}
|
|
} |