parfum/app/Livewire/Studio/Master/Outlet/Index.php

103 lines
2.8 KiB
PHP

<?php
namespace App\Livewire\Studio\Master\Outlet;
use App\Enums\Day;
use App\Models\Outlet;
use App\Traits\WithAuthorization;
use App\Traits\WithCloseModal;
use App\Traits\WithConfirmation;
use App\Traits\WithMediaHandler;
use App\Traits\WithToast;
use Flux\Flux;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Str;
use Illuminate\View\View;
use Livewire\Attributes\Title;
use Livewire\Component;
#[Title('Outlet')]
class Index extends Component
{
use WithAuthorization, WithCloseModal, WithConfirmation, WithMediaHandler, WithToast;
public Collection $outlets;
public string $search = '';
public array $status = [];
public function mount(): void
{
$this->loadOutlets();
}
protected function loadOutlets(): void
{
$this->outlets = Outlet::with(['openingHours', 'facilities'])
->when(! empty($this->search), fn ($query) => $query->where('name', 'like', '%'.$this->search.'%'))
->when(! empty($this->status), fn ($query) => $query->whereIn('status', $this->status))
->latest()
->get();
}
protected function formatOpeningHours(Outlet $outlet): array
{
if (! $outlet->openingHours || $outlet->openingHours->isEmpty()) {
return [];
}
return $outlet->openingHours->mapWithKeys(fn ($hour) => [
Str::ucfirst(Str::lower(Day::from($hour->day->value)->label())) => [
'open' => formatTime($hour->open_time),
'close' => formatTime($hour->close_time),
'is_closed' => $hour->open_time === null && $hour->close_time === null,
],
])->toArray();
}
protected function getFeaturedImage(Outlet $outlet): string
{
$mediaCollection = $this->mapMediaCollection($outlet->getMedia('featured_image'));
return data_get($mediaCollection[0] ?? null, 'temporaryUrl', asset('assets/images/logo.png'));
}
protected function getGalleryImages(Outlet $outlet): array
{
return collect($this->mapMediaCollection($outlet->getMedia('images')))
->pluck('temporaryUrl')
->filter()
->values()
->toArray();
}
public function updatedSearch(string $value): void
{
$this->search = $value;
$this->loadOutlets();
}
public function updatedStatus(): void
{
$this->loadOutlets();
}
public function delete(Outlet $outlet): void
{
$this->canOrAbort('delete outlet');
$outlet->delete();
$this->toast('Outlet berhasil dihapus.');
$this->loadOutlets();
Flux::modals()->close();
}
public function render(): View
{
return view('livewire.studio.master.outlet.index', [
'pageTitle' => 'Outlet',
]);
}
}