KTV/app/Livewire/Admin/SongForm.php
2025-04-25 18:21:20 +08:00

104 lines
2.3 KiB
PHP

<?php
namespace App\Livewire\Admin;
use Livewire\Component;
use App\Models\Song;
use App\Models\SongCategory;
use App\Models\SongLanguage;
class SongForm extends Component
{
protected $listeners = ['openCreateSongModal','openEditSongModal', 'deleteSong'];
public bool $showCreateModal = false;
public $songCategories =[];
public $songLanguage =[];
public $selectedCategories=[];
public $selectedArtists=[];
public ?int $songId = null;
public array $fields = [
'id' =>'',
'name' => '',
'adddate' => '',
'filename' => '',
'language_type' => '',
'db_change' => '',
'vocal' => '',
'situation' => '',
'copyright01' => '',
'copyright02' => '',
'note01' => '',
'note02' => '',
'note03' => '',
'note04' => '',
'enable' => true,
];
protected $rules = [
'name' => 'required|string|max:255',
];
public function mount()
{
$this->songCategories = SongCategory::all();
$this->songLanguage = SongLanguage::all();
}
public function openCreateSongModal()
{
$this->resetFields();
$this->showCreateModal = true;
}
public function openEditSongModal($id)
{
$song = Song::findOrFail($id);
$this->songId = $song->id;
$this->fields = $song->only(array_keys($this->fields));
$this->showCreateModal = true;
}
public function save()
{
$this->validate();
if ($this->songId) {
$song = Song::findOrFail($this->songId);
$song->update($this->fields);
session()->flash('message', '歌曲已更新');
} else {
Song::create($this->fields);
session()->flash('message', '歌曲已新增');
}
$this->resetFields();
$this->showCreateModal = false;
}
public function deleteSong($id)
{
Song::findOrFail($id)->delete();
session()->flash('message', '歌曲已刪除');
}
public function resetFields()
{
foreach ($this->fields as $key => $value) {
$this->fields[$key] = is_bool($value) ? false : '';
}
$this->songId = null;
}
public function render()
{
return view('livewire.admin.song-form');
}
}