parfum/app/Livewire/Forms/OutletForm.php

130 lines
3.6 KiB
PHP

<?php
namespace App\Livewire\Forms;
use App\Enums\OutletStatus;
use App\Models\Outlet;
use App\Rules\PhoneNumber;
use Illuminate\Validation\Rule;
use Livewire\Form;
class OutletForm extends Form
{
public ?Outlet $outlet;
public string $name = '';
public string $phone_number = '';
public string $address = '';
public string $landmark = '';
public ?string $maps_url = null;
public array $opening_hours = [
'senin' => ['open' => null, 'close' => null],
'selasa' => ['open' => null, 'close' => null],
'rabu' => ['open' => null, 'close' => null],
'kamis' => ['open' => null, 'close' => null],
'jumat' => ['open' => null, 'close' => null],
'sabtu' => ['open' => null, 'close' => null],
'minggu' => ['open' => null, 'close' => null],
];
public array $facilities = [''];
public string $opened_date = '';
public ?string $status = null;
public function rules(): array
{
$days = ['senin', 'selasa', 'rabu', 'kamis', 'jumat', 'sabtu', 'minggu'];
$rules = [
'name' => ['required', 'string', 'max:100'],
'phone_number' => ['required', 'string', new PhoneNumber],
'address' => ['required', 'string'],
'landmark' => ['required', 'string', 'max:40'],
'maps_url' => [
'nullable',
'string',
'regex:/^(https?:\/\/)?(www\.)?(google\.[a-z.]+\/maps\/.+|goo\.gl\/maps\/.+|maps\.app\.goo\.gl\/.+)$/i'
],
'opening_hours' => ['required', 'array'],
'facilities' => ['required', 'array', 'min:1'],
'facilities.*' => ['required', 'string', 'max:20'],
'opened_date' => ['required', 'date'],
'status' => ['required', Rule::in(OutletStatus::cases())],
];
foreach ($days as $day) {
$rules["opening_hours.$day"] = ['required', 'array'];
$rules["opening_hours.$day.open"] = ['nullable', 'date_format:H:i'];
$rules["opening_hours.$day.close"] = ['nullable', 'date_format:H:i'];
}
return $rules;
}
public function validationAttributes(): array
{
return [
'name' => 'nama',
'phone_number' => 'nomor telepon',
'address' => 'alamat',
'landmark' => 'patokan',
'maps_url' => 'tautan google maps',
'opening_hours.*' => 'jam beroperasi',
'facilities.*' => 'fasilitas',
'opened_date' => 'tanggal buka',
];
}
public function setOutlet(Outlet $outlet)
{
$this->outlet = $outlet;
$this->name = $outlet->name;
$this->phone_number = $outlet->phone_number;
$this->address = $outlet->address;
$this->landmark = $outlet->landmark;
$this->maps_url = $outlet->maps_url;
$this->opening_hours = $outlet->opening_hours;
$this->facilities = $outlet->facilities;
$this->opened_date = $outlet->opened_date;
$this->status = $outlet->status->value;
}
public function store()
{
$this->validate();
$data = $this->all();
if ($data['status'] == OutletStatus::TERMINATED) {
$data['closed_date'] = now();
} else {
$data['closed_date'] = null;
}
Outlet::create($data);
}
public function update()
{
$this->validate();
$data = $this->all();
if ($data['status'] == OutletStatus::TERMINATED->value) {
$data['closed_date'] = now()->toDateString();
} else {
$data['closed_date'] = null;
}
$this->outlet->update($data);
}
}