KTVCentral/app/Livewire/Forms/RoomTable.php

136 lines
3.8 KiB
PHP

<?php
namespace App\Livewire\Forms;
use App\Models\Room;
use Illuminate\Support\Carbon;
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;
final class RoomTable extends PowerGridComponent
{
public string $tableName = 'room-table';
public bool $showFilters = false;
public function boot(): void
{
config(['livewire-powergrid.filter' => 'outside']);
}
public function setUp(): array
{
$actions = [];
$header = PowerGrid::header()
->withoutLoading()
->showToggleColumns();
$header->includeViewOnTop('livewire.header.admin.room');
$actions[]=$header;
$actions[]=PowerGrid::footer()->showPerPage()->showRecordCount();
return $actions;
}
public function datasource(): Builder
{
return Room::query();
}
public function relationSearch(): array
{
return [];
}
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('id')
->add('floor')
->add('room_name',function (Room $model){
return $model->type->labelPowergridFilter().$model->name;
})
->add('is_online')
->add('status_str',function (Room $model){
return $model->status->labelPowergridFilter();
})
->add('started_at_formatted', fn (Room $model) => Carbon::parse($model->started_at)->format('Y/m/d H:i:s'))
->add('ended_at_formatted', fn (Room $model) => Carbon::parse($model->ended_at)->format('Y/m/d H:i:s'))
->add('created_at');
}
public function columns(): array
{
return [
Column::make('Id', 'id'),
Column::make('Floor', 'floor')
->sortable()
->searchable(),
Column::make('Name', 'room_name')
->sortable()
->searchable(),
Column::make('Is online', 'is_online')
->sortable()
->searchable(),
Column::make('Status', 'status_str')
->sortable()
->searchable(),
Column::make('Started at', 'started_at_formatted', 'started_at')
->sortable(),
Column::make('Ended at', 'ended_at_formatted', 'ended_at')
->sortable(),
Column::make('Created at', 'created_at')
->sortable()
->searchable(),
Column::action('Action')
];
}
public function filters(): array
{
return [
Filter::datetimepicker('started_at'),
Filter::datetimepicker('ended_at'),
];
}
#[\Livewire\Attributes\On('edit')]
public function edit($rowId): void
{
$this->js('alert('.$rowId.')');
}
public function actions(Room $row): array
{
return [
Button::add('edit')
->slot('Edit: '.$row->id)
->id()
->class('pg-btn-white dark:ring-pg-primary-600 dark:border-pg-primary-600 dark:hover:bg-pg-primary-700 dark:ring-offset-pg-primary-800 dark:text-pg-primary-300 dark:bg-pg-primary-700')
->dispatch('edit', ['rowId' => $row->id])
];
}
/*
public function actionRules($row): array
{
return [
// Hide button edit for ID 1
Rule::button('edit')
->when(fn($row) => $row->id === 1)
->hide(),
];
}
*/
}