parfum/app/Livewire/Datatable/Loyalty/RewardsTable.php

100 lines
3.8 KiB
PHP

<?php
namespace App\Livewire\Datatable\Loyalty;
use App\Models\Reward;
use App\Traits\Datatable\WithAppendColumn;
use App\Traits\Datatable\WithConfiguration;
use App\Traits\Datatable\WithPrependColumn;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Blade;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
class RewardsTable extends DataTableComponent
{
use WithAppendColumn, WithConfiguration, WithPrependColumn;
protected $model = Reward::class;
public function columns(): array
{
return [
Column::make('Nama', 'name')
->searchable()
->sortable(),
Column::make('Poin', 'points')
->format(fn ($value) => formatCurrencyNumber($value))
->searchable()
->sortable(),
Column::make('Stok', 'stock')
->format(fn ($value) => $value === null ? 'Tidak Terbatas' : formatCurrencyNumber($value))
->searchable()
->sortable(),
Column::make('Kategori', 'category')
->label(fn ($row, $column) => Blade::render('<flux:badge color="'.$row->category->color().'">'.$row->category->label().'</flux:badge>'))
->html(),
Column::make('Visibilitas', 'is_show')
->label(fn ($row, $column) => Blade::render('<flux:badge color="'.$row->is_show->color().'">'.$row->is_show->label().'</flux:badge>'))
->html(),
Column::make('Tanggal')
->label(function ($row) {
$startDate = formatDateLocalized($row->start_date);
$startAgo = formatRelativeTime($row->start_date);
$endDate = formatDateLocalized($row->end_date);
$endAgo = formatRelativeTime($row->end_date);
return <<<HTML
<div class="flex flex-col text-xs text-gray-700 dark:text-gray-300 gap-0.5">
<div>
<span class="font-medium text-gray-900 dark:text-white">Tgl Dimulai:</span>
<span>{$startDate}</span>
<span class="text-gray-500">({$startAgo})</span>
</div>
<div>
<span class="font-medium text-gray-900 dark:text-white">Tgl Berakhir:</span>
<span>{$endDate}</span>
<span class="text-gray-500">({$endAgo})</span>
</div>
</div>
HTML;
})
->html(),
Column::make('Aksi')
->label(function ($row) {
$actions = '';
if (auth()->user()->can('update reward') && $row->redemptions_count === 0) {
$actions .= view('components.actions.table.edit-modal', [
'id' => $row->hash,
'method' => 'update',
'modalTitle' => 'Ubah Hadiah',
])->render();
}
if (auth()->user()->can('delete reward')) {
$actions .= view('components.actions.table.delete', [
'id' => $row->hash,
])->render();
}
return $actions;
})
->html()
->hideIf(auth()->user()->cannot('update reward') && auth()->user()->cannot('delete reward')),
];
}
public function builder(): Builder
{
return Reward::select('id', 'name', 'points', 'stock', 'category', 'is_show', 'start_date', 'end_date')->withCount('redemptions');
}
}