60 lines
1.7 KiB
PHP
60 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Devices;
|
|
|
|
use App\Livewire\Concerns\TogglesEntities;
|
|
use App\Models\Device;
|
|
use App\Models\Room;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\Attributes\Url;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.app')]
|
|
class Index extends Component
|
|
{
|
|
use TogglesEntities;
|
|
|
|
#[On('echo-private:home,.DeviceStateChanged')]
|
|
public function onDeviceStateChanged(): void {}
|
|
|
|
#[Url(as: 'q')]
|
|
public string $search = '';
|
|
|
|
#[Url]
|
|
public string $room = '';
|
|
|
|
#[Url]
|
|
public string $status = '';
|
|
|
|
public function clearFilters(): void
|
|
{
|
|
$this->reset('search', 'room', 'status');
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$devices = Device::query()
|
|
->with(['room', 'entities.state'])
|
|
->when($this->search, fn ($q) => $q->where(fn ($w) => $w
|
|
->where('name', 'ilike', "%{$this->search}%")
|
|
->orWhere('model', 'ilike', "%{$this->search}%")
|
|
->orWhere('vendor', 'ilike', "%{$this->search}%")))
|
|
->when($this->room, fn ($q) => $q->whereHas('room', fn ($r) => $r->where('uuid', $this->room)))
|
|
->orderBy('name')
|
|
->get();
|
|
|
|
if ($this->status === 'online') {
|
|
$devices = $devices->filter->isOnline()->values();
|
|
} elseif ($this->status === 'offline') {
|
|
$devices = $devices->reject->isOnline()->values();
|
|
}
|
|
|
|
return view('livewire.devices.index', [
|
|
'groups' => $devices->groupBy(fn ($d) => $d->room?->name ?? __('devices.no_room')),
|
|
'total' => $devices->count(),
|
|
'rooms' => Room::orderBy('sort')->orderBy('name')->get(),
|
|
]);
|
|
}
|
|
}
|