KTV/app/Livewire/Admin/SongTable.php

298 lines
12 KiB
PHP

<?php
namespace App\Livewire\Admin;
use App\Models\Song;
use App\Enums\SongLanguageType;
use App\Enums\SongSituation;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Blade;
use Illuminate\Database\Eloquent\Builder;
use PowerComponents\LivewirePowerGrid\Button;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Facades\Filter;
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\Traits\WithExport;
use PowerComponents\LivewirePowerGrid\Components\SetUp\Exportable;
use PowerComponents\LivewirePowerGrid\Facades\Rule;
use Livewire\Attributes\On;
final class SongTable extends PowerGridComponent
{
public string $tableName = 'song-table';
/* public bool $deferLoading = true;
public string $loadingComponent = 'components.my-custom-loading'; */
public bool $showFilters = false;
public function boot(): void
{
config(['livewire-powergrid.filter' => 'outside']);
}
public function setUp(): array
{
$this->showCheckBox();
return [
PowerGrid::exportable(fileName: 'song-file')
->type(Exportable::TYPE_XLS, Exportable::TYPE_CSV),
PowerGrid::header()
->showSoftDeletes()
->showToggleColumns()
//->showSearchInput()
,
PowerGrid::footer()
->showPerPage()
->showRecordCount(),
];
}
public function header(): array
{
return [
Button::add('bulk-delete')
->slot('Bulk delete (<span x-text="window.pgBulkActions.count(\'' . $this->tableName . '\')"></span>)')
->icon('solid-trash',['id' => 'my-custom-icon-id', 'class' => 'font-bold'])
->class('inline-flex items-center gap-1 px-3 py-1 rounded ')
->dispatch('bulkDelete.' . $this->tableName, []),
];
}
public function datasource(): Builder
{
return Song::query()->with([
'artists:id,name'
]);
}
public function relationSearch(): array
{
return [];
}
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('name')
->add('simplified')
->add('phonetic_abbr')
->add('pinyin_abbr')
->add('strokes_abbr')
->add('filename')
->add('adddate_formatted', fn (Song $model) => Carbon::parse($model->adddate)->format('Y-m-d'))
->add('song_artists' ,fn(Song $model)=> $model->str_artists())
//->add('language_type_str', fn (Song $model) => SongLanguageType::from($model->language_type->value)->labels())
->add('language_type_str', function (Song $model) {
return Blade::render(
'<x-select-category type="occurrence" :options=$options :modelId=$modelId :fieldName=$fieldName :selected=$selected/>',
[
'options' => $this->languageTypeSelectOptions(),
'modelId' => intval($model->id),
'fieldName'=>'language_type',
'selected' => $model->language_type->value
]);
} )
->add('song_categories', fn(Song $model) => $model->str_categories())
->add('db_change')
->add('vocal')
//->add('situation_str', fn (Song $model) => SongSituation::from($model->situation->value)->labels())
->add('situation_str', function (Song $model){
return Blade::render(
'<x-select-category type="occurrence" :options=$options :modelId=$modelId :fieldName=$fieldName :selected=$selected/>',
[
'options' => $this->situationSelectOptions(),
'modelId' => intval($model->id),
'fieldName'=>'situation',
'selected' => $model->situation->value
]);
} )
->add('copyright01')
->add('copyright02')
->add('note01')
->add('note02')
->add('note03')
->add('note04')
->add('enable')
->add('created_at_formatted', fn (Song $model) => Carbon::parse($model->created_at)->format('Y-m-d H:i:s'));
}
public function columns(): array
{
return [
Column::make(__('songs.id'), 'id'),
Column::make(__('songs.name'), 'name')
->sortable()
->searchable(),
Column::make(__('songs.simplified'), 'simplified')
->sortable()
->searchable()
->hidden(true, false),
Column::make(__('songs.name.phinetic'), 'phonetic_abbr')
->sortable()
->searchable()
->hidden(true, false),
Column::make(__('songs.name.pinyin'), 'pinyin_abbr')
->sortable()
->searchable()
->hidden(true, false),
Column::make(__('songs.name.strokes'), 'strokes_abbr')
->sortable()
->searchable()
->hidden(true, false),
Column::make(__('songs.filename'), 'filename')
->sortable()
->searchable(),
Column::make(__('songs.adddate'), 'adddate_formatted', 'adddate')
->sortable(),
//歌手
Column::make(__('songs.artists'), 'song_artists'),
Column::make(__('songs.language_type'),'language_type_str', 'songs.language_type')->searchable(),
//分類
Column::make(__('songs.categorys'), 'song_categories'),
//點播次數
Column::make('Db change', 'db_change'),
Column::make(__('songs.vocal'), 'vocal')
->toggleable(hasPermission: true, trueLabel: 'yes', falseLabel: 'no'),
Column::make(__('songs.situation'), 'situation_str','songs.situation')->searchable(),
Column::make(__('songs.copyright01'), 'copyright01')
->sortable()
->searchable()
->editOnClick(hasPermission: true, dataField: 'copyright01', fallback: 'N/A', saveOnMouseOut: true),
Column::make(__('songs.copyright02'), 'copyright02')
->sortable()
->searchable()
->editOnClick(hasPermission: true, dataField: 'copyright02', fallback: 'N/A', saveOnMouseOut: true)
->hidden(true, false),
Column::make(__('songs.note01'), 'note01')
->sortable()
->searchable()
->editOnClick(hasPermission: true, dataField: 'note01', fallback: 'N/A', saveOnMouseOut: true),
Column::make(__('songs.note02'), 'note02')
->sortable()
->searchable()
->editOnClick(hasPermission: true, dataField: 'note02', fallback: 'N/A', saveOnMouseOut: true)
->hidden(true, false),
Column::make(__('songs.note03'), 'note03')
->sortable()
->searchable()
->editOnClick(hasPermission: true, dataField: 'note03', fallback: 'N/A', saveOnMouseOut: true)
->hidden(true, false),
Column::make(__('songs.note04'), 'note04')
->sortable()
->searchable()
->editOnClick(hasPermission: true, dataField: 'note04', fallback: 'N/A', saveOnMouseOut: true)
->hidden(true, false),
Column::make(__('songs.enable'), 'enable')
->toggleable(hasPermission: true, trueLabel: 'yes', falseLabel: 'no'),
Column::make('Created at', 'created_at_formatted', 'created_at')
->sortable()
->hidden(true, false),
Column::action(__('songs.actions'))
];
}
public function filters(): array
{
return [
Filter::number('id')->placeholder(0,9999999),
Filter::inputText('name')->placeholder(__('songs.name')),
Filter::inputText('phonetic_abbr')->placeholder(__('songs.name.phinetic')),
Filter::inputText('pinyin_abbr')->placeholder(__('songs.name.pinyin_abbr')),
Filter::number('strokes_abbr'),
Filter::inputText('filename')->placeholder(__('songs.filename')),
Filter::datepicker('adddate'),
Filter::enumSelect('language_type_str','songs.language_type')
->datasource(SongLanguageType::cases())
->optionLabel('songs.language_type'),
Filter::inputText('copyright01')->placeholder(__('songs.copyright01')),
Filter::inputText('copyright02')->placeholder(__('songs.copyright02')),
Filter::inputText('note01')->placeholder(__('songs.note01')),
Filter::inputText('note02')->placeholder(__('songs.note02')),
Filter::inputText('note03')->placeholder(__('songs.note03')),
Filter::inputText('note04')->placeholder(__('songs.note04')),
Filter::boolean('enable')->label('✅', '❌'),
Filter::datetimepicker('created_at'),
];
}
public function languageTypeSelectOptions():Collection
{
return collect(SongLanguageType::cases())->mapWithKeys(function (SongLanguageType $case) {
return [$case->value => $case->labels()];
});
}
public function situationSelectOptions():Collection
{
return collect(SongSituation::cases())->mapWithKeys(function (SongSituation $case) {
return [$case->value => $case->labels()];
});
}
#[On('categoryChanged')]
public function categoryChanged($value,$fieldName, $modelId): void
{
// dd($value,$fieldName, $modelId);
if (in_array($fieldName, ['language_type', 'situation'])) {
Artist::find($modelId)?->update([$fieldName => $value]);
}
}
#[On('bulkDelete.{tableName}')]
public function bulkDelete(): void
{
$this->js('alert(window.pgBulkActions.get(\'' . $this->tableName . '\'))');
if($this->checkboxValues){
Artist::destroy($this->checkboxValues);
$this->js('window.pgBulkActions.clearAll()'); // clear the count on the interface.
}
}
public function actions(Song $row): array
{
return [
Button::add('edit')
->slot(__('songs.edit'))
->icon('solid-pencil-square')
->class('inline-flex items-center gap-1 px-3 py-1 rounded ')
->dispatchTo('admin.song-form', 'openEditSongModal', ['id' => $row->id]),
Button::add('delete')
->slot(__('songs.delete'))
->icon('solid-trash')
->class('inline-flex items-center gap-1 px-3 py-1 rounded ')
->dispatchTo('admin.song-form', 'deleteSong', ['id' => $row->id]),
];
}
public function onUpdatedToggleable($id, $field, $value): void
{
$updated = Song::query()->where('id', $id)->update([
$field => $value,
]);
}
/*
public function actionRules($row): array
{
return [
// Hide button edit for ID 1
Rule::button('edit')
->when(fn($row) => $row->id === 1)
->hide(),
];
}
*/
}