KTV/app/Livewire/Forms/SongForm.php

184 lines
5.9 KiB
PHP
Raw Normal View History

2025-04-25 09:33:28 +08:00
<?php
namespace App\Livewire\Forms;
2025-04-25 09:33:28 +08:00
use Illuminate\Validation\Rule;
use Illuminate\Support\Facades\Auth;
2025-04-25 09:33:28 +08:00
use Livewire\Component;
use WireUi\Traits\WireUiActions;
2025-04-25 09:33:28 +08:00
use Illuminate\Validation\Rules\Enum;
use App\Enums\SongLanguageType;
use App\Enums\SongSituation;
2025-04-25 18:21:20 +08:00
use App\Models\Song;
use App\Models\SongCategory;
2025-04-25 09:33:28 +08:00
class SongForm extends Component
{
use WireUiActions;
2025-05-05 16:54:21 +08:00
protected $listeners = ['openModal','closeModal', 'deleteSong'];
public bool $canCreate;
public bool $canEdit;
public bool $canDelect;
2025-04-25 18:21:20 +08:00
2025-05-05 16:54:21 +08:00
public bool $showModal = false;
2025-04-25 18:21:20 +08:00
public $songCategories =[];
public $songLanguageType =[];
public $songSituation =[];
2025-04-25 18:21:20 +08:00
public $selectedCategories=[];
public $selectedArtists=[];
public ?int $songId = null;
public array $fields = [
'id' =>'',
'name' => '',
'adddate' => '',
'filename' => '',
'language_type' => '',
'vocal' => true,
2025-04-25 18:21:20 +08:00
'situation' => '',
'copyright01' => '',
'copyright02' => '',
'note01' => '',
'note02' => '',
'note03' => '',
'note04' => '',
'enable' => true,
];
public function rules()
{
return [
'fields.name' => 'required|string|max:255',
'fields.adddate' => 'required|date', // 或 required, 依照你的需求
'fields.filename' => 'required|string|max:255',
'fields.language_type'=> ['required',new Enum(SongLanguageType::class)],
'fields.vocal' => 'required|boolean',
'fields.situation' => ['required',new Enum(SongSituation::class)],
'fields.copyright01' => 'nullable|string|max:255',
'fields.copyright02' => 'nullable|string|max:255',
'fields.note01' => 'nullable|string|max:255',
'fields.note02' => 'nullable|string|max:255',
'fields.note03' => 'nullable|string|max:255',
'fields.note04' => 'nullable|string|max:255',
'fields.enable' => 'required|boolean',
// 如果你在表單中也綁定了 selectedArtists / selectedCategories
'selectedArtists' => 'required|array',
'selectedArtists.*' => 'integer|exists:artists,id',
'selectedCategories' => 'nullable|array',
'selectedCategories.*'=> 'integer|exists:categories,id',
];
}
2025-04-25 18:21:20 +08:00
public function mount()
{
$this->songCategories = SongCategory::all();
$this->songLanguageType = collect(SongLanguageType::cases())->map(fn ($languageType) => [
'name' => $languageType->labels(),
'value' => $languageType->value,
])->toArray();
$this->songSituation = collect(SongSituation::cases())->map(fn ($situation) => [
'name' => $situation->labels(),
'value' => $situation->value,
])->toArray();
2025-07-24 16:59:25 +08:00
$this->canCreate = Auth::user()?->can('song-edit') ?? false;
$this->canEdit = Auth::user()?->can('song-edit') ?? false;
$this->canDelect = Auth::user()?->can('song-delete') ?? false;
2025-04-25 18:21:20 +08:00
}
2025-05-05 16:54:21 +08:00
public function openModal($id = null)
2025-04-25 18:21:20 +08:00
{
$this->resetFields();
2025-05-05 16:54:21 +08:00
if ($id) {
$song = Song::findOrFail($id);
$this->songId = $song->id;
$this->fields = $song->only(array_keys($this->fields));
$this->selectedCategories = $song->categories()->pluck('id')->toArray();
$this->selectedArtists = $song->artists()->pluck('id')->toArray();
//dd($this->fields,$this->selectedCategories,$this->selectedArtists);
}
$this->showModal = true;
2025-04-25 18:21:20 +08:00
}
2025-05-05 16:54:21 +08:00
public function closeModal()
2025-04-25 18:21:20 +08:00
{
2025-05-05 16:54:21 +08:00
$this->resetFields();
$this->showModal = false;
2025-04-25 18:21:20 +08:00
}
public function save()
{
$validated = $this->validate($this->rules());
2025-04-25 18:21:20 +08:00
if ($this->songId) {
if ($this->canEdit) {
$song = Song::findOrFail($this->songId);
$song->update($this->fields);
// ⭐ 同步多對多關聯
$song->artists()->sync($this->selectedArtists ?? []);
$song->categories()->sync($this->selectedCategories ?? []);
$this->notification()->send([
'icon' => 'success',
'title' => '成功',
'description' => '歌曲已更新',
]);
}
2025-04-25 18:21:20 +08:00
} else {
if ($this->canCreate) {
$song = Song::create($this->fields);
// ⭐ 同步多對多關聯
$song->artists()->sync($this->selectedArtists ?? []);
$song->categories()->sync($this->selectedCategories ?? []);
$this->notification()->send([
'icon' => 'success',
'title' => '成功',
'description' => '歌曲已新增',
]);
}
2025-04-25 18:21:20 +08:00
}
$this->resetFields();
2025-05-05 16:54:21 +08:00
$this->showModal = false;
2025-05-02 18:07:25 +08:00
$this->dispatch('pg:eventRefresh-song-table');
2025-04-25 18:21:20 +08:00
}
public function deleteSong($id)
{
if ($this->canDelect) {
Song::findOrFail($id)->delete();
$this->notification()->send([
'icon' => 'success',
'title' => '成功',
'description' => '歌曲已刪除',
]);
}
2025-04-25 18:21:20 +08:00
}
public function resetFields()
{
foreach ($this->fields as $key => $value) {
if($key == 'vocal' || $key == 'enable'){
$this->fields[$key] = true ;
}else if($key == 'adddate'){
$this->fields[$key] = now()->format('Y-m-d');
}else{
$this->fields[$key] = '' ;
}
2025-04-25 18:21:20 +08:00
}
$this->songId = null;
}
2025-04-25 09:33:28 +08:00
public function render()
{
return view('livewire.forms.song-form');
2025-04-25 09:33:28 +08:00
}
}