88 lines
2.2 KiB
PHP
88 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
use Livewire\Component;
|
|
use Illuminate\Validation\Rule;
|
|
use App\Models\Artist;
|
|
use App\Enums\ArtistCategory;
|
|
|
|
class ArtistForm extends Component
|
|
{
|
|
protected $listeners = ['openCreateArtistModal','openEditArtistModal', 'deleteArtist'];
|
|
|
|
public bool $showCreateModal = false;
|
|
public ?int $artistId = null;
|
|
|
|
public array $categoryOptions =[];
|
|
public array $fields = [
|
|
'category' =>'unset',
|
|
'name' =>'',
|
|
];
|
|
public $selectedCategory = []; // 表單中選到的權限
|
|
|
|
|
|
|
|
public function mount()
|
|
{
|
|
$this->categoryOptions = collect(ArtistCategory::cases())->map(fn ($category) => [
|
|
'name' => $category->labels(),
|
|
'value' => $category->value,
|
|
])->toArray();
|
|
}
|
|
|
|
public function openCreateArtistModal()
|
|
{
|
|
$this->resetFields();
|
|
$this->showCreateModal = true;
|
|
}
|
|
|
|
public function openEditArtistModal($id)
|
|
{
|
|
$artist = Artist::findOrFail($id);
|
|
$this->artistId = $artist->id;
|
|
$this->fields = $artist->only(array_keys($this->fields));
|
|
$this->showCreateModal = true;
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
|
|
if ($this->artistId) {
|
|
$artist = Artist::findOrFail($this->artistId);
|
|
$artist->update($this->fields);
|
|
session()->flash('message', '歌手已更新');
|
|
} else {
|
|
$artist = Artist::create($this->fields);
|
|
session()->flash('message', '歌手已新增');
|
|
}
|
|
|
|
$this->resetFields();
|
|
$this->showCreateModal = false;
|
|
$this->dispatch('pg:eventRefresh-artist-table');
|
|
}
|
|
|
|
public function deleteArtist($id)
|
|
{
|
|
Artist::findOrFail($id)->delete();
|
|
session()->flash('message', '歌手已刪除');
|
|
}
|
|
|
|
public function resetFields()
|
|
{
|
|
foreach ($this->fields as $key => $value) {
|
|
if ($key == 'category') {
|
|
$this->fields[$key] = 'unset';
|
|
} else {
|
|
$this->fields[$key] = '';
|
|
}
|
|
}
|
|
$this->artistId = null;
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.admin.artist-form');
|
|
}
|
|
}
|