parfum/app/Livewire/Datatable/Studio/Master/OutletsTable.php
2025-10-03 09:54:53 +07:00

147 lines
5.8 KiB
PHP

<?php
namespace App\Livewire\Datatable\Studio\Master;
use App\Enums\Day;
use App\Enums\OutletStatus;
use App\Models\Outlet;
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 Illuminate\Support\Str;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use Rappasoft\LaravelLivewireTables\Views\Columns\ArrayColumn;
use Rappasoft\LaravelLivewireTables\Views\Filters\MultiSelectFilter;
class OutletsTable extends DataTableComponent
{
use WithAppendColumn, WithConfiguration, WithPrependColumn;
protected $model = Outlet::class;
public function columns(): array
{
return [
Column::make('Nama', 'name')->searchable(),
Column::make('Nomor Telepon', 'phone_number')->searchable(),
Column::make('Alamat')
->label(function ($row) {
return <<<HTML
<div>
<div>{$row->landmark}</div>
<div>{$row->address}</div>
</div>
HTML;
})
->searchable(function ($query, $searchTerm) {
$query->orWhere('landmark', 'like', "%{$searchTerm}%")
->orWhere('address', 'like', "%{$searchTerm}%");
})
->html(),
ArrayColumn::make('Jam Operasioanl')
->data(
fn ($value, $row) => $row->openingHours
->mapWithKeys(fn ($hour) => [
Str::lower(Day::from($hour->day->value)->label()) => [
'open' => formatTime($hour->open_time),
'close' => formatTime($hour->close_time),
],
])
)
->outputFormat(
fn ($index, $value) => "<span class='flex items-center gap-2 py-1 text-xs'>
<span class='font-semibold'>".Str::ucfirst($index).': </span>
<span class="text-gray-600 dark:text-gray-400">'.(
($value['open'] === null && $value['close'] === null)
? 'Tutup'
: "{$value['open']} - {$value['close']}"
).'</span>
</span>'
)
->flexCol(['class' => 'gap-2 flex-wrap']),
ArrayColumn::make('Fasilitas')
->data(fn ($value, $row) => $row->facilities->pluck('name')->toArray())
->outputFormat(fn ($index, $value) => Blade::render('<flux:badge color="zinc" dot>'.$value.'</flux:badge>'))
->flexRow(['class' => 'gap-2 flex-wrap']),
Column::make('Tanggal Operasi')
->label(function ($row) {
$openedDate = formatDate($row->opened_date);
$openedAgo = timeAgo($row->opened_date);
$closedDate = formatDate($row->closed_date);
$closedAgo = timeAgo($row->closed_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 Buka:</span>
<span>{$openedDate}</span>
<span class="text-gray-500">({$openedAgo})</span>
</div>
<div>
<span class="font-medium text-gray-900 dark:text-white">Tgl Tutup:</span>
<span>{$closedDate}</span>
<span class="text-gray-500">({$closedAgo})</span>
</div>
</div>
HTML;
})
->html(),
Column::make('Status', 'status')
->format(fn ($value) => Blade::render('<flux:badge color="'.$value->color().'" dot>'.$value->label().'</flux:badge>'))
->html(),
Column::make('Aksi')
->label(function ($row) {
$actions = '';
if (auth()->user()->can('update outlet')) {
$actions .= view('components.datatables.edit', [
'id' => $row->hash,
'editRoute' => route('studio.master.outlet.edit', $row->hash),
])->render();
}
if (auth()->user()->can('delete outlet')) {
$actions .= view('components.datatables.delete', [
'id' => $row->hash,
'deleteRoute' => route('studio.master.outlet.delete', $row->hash),
])->render();
}
return $actions;
})
->html(),
];
}
public function builder(): Builder
{
return Outlet::select('id', 'name', 'phone_number', 'landmark', 'address', 'opened_date', 'closed_date', 'status')->with(['openingHours', 'facilities']);
}
public function filters(): array
{
return [
MultiSelectFilter::make('Status')
->options(
collect(OutletStatus::cases())
->mapWithKeys(fn ($case) => [$case->value => $case->label()])
->toArray()
)
->filter(function (Builder $builder, array $values) {
$builder->whereIn('status', $values);
}),
];
}
}