64 lines
1.5 KiB
PHP
64 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Modals;
|
|
|
|
use App\Models\Person;
|
|
use App\Services\UnifiClient;
|
|
use LivewireUI\Modal\ModalComponent;
|
|
|
|
class AddPerson extends ModalComponent
|
|
{
|
|
public string $name = '';
|
|
|
|
public string $mac = '';
|
|
|
|
/** @var array<int, array{mac:string,label:string}> */
|
|
public array $clientOptions = [];
|
|
|
|
public bool $unifiError = false;
|
|
|
|
public static function modalMaxWidth(): string
|
|
{
|
|
return 'lg';
|
|
}
|
|
|
|
public function mount(UnifiClient $unifi): void
|
|
{
|
|
try {
|
|
$this->clientOptions = collect($unifi->clients())
|
|
->map(fn ($c) => [
|
|
'mac' => strtolower($c->mac ?? ''),
|
|
'label' => $c->name ?? $c->hostname ?? ($c->mac ?? '?'),
|
|
])
|
|
->filter(fn ($o) => $o['mac'] !== '')
|
|
->sortBy('label', SORT_NATURAL | SORT_FLAG_CASE)
|
|
->values()->all();
|
|
} catch (\Throwable) {
|
|
$this->unifiError = true;
|
|
}
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
$this->validate([
|
|
'name' => 'required|string|max:100',
|
|
'mac' => ['nullable', 'string', 'max:32', 'unique:persons,mac'],
|
|
]);
|
|
|
|
Person::create([
|
|
'name' => $this->name,
|
|
'mac' => $this->mac ?: null,
|
|
'presence' => 'unknown',
|
|
]);
|
|
|
|
$this->closeModal();
|
|
|
|
return redirect()->route('persons.index');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.modals.add-person');
|
|
}
|
|
}
|