71 lines
1.8 KiB
PHP
71 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Modals;
|
|
|
|
use App\Models\Device;
|
|
use App\Models\DiscoveryFinding;
|
|
use App\Models\Room;
|
|
use Livewire\Attributes\Computed;
|
|
use LivewireUI\Modal\ModalComponent;
|
|
|
|
class AssignDevice extends ModalComponent
|
|
{
|
|
public string $findingUuid = '';
|
|
|
|
public string $name = '';
|
|
|
|
public ?int $roomId = null;
|
|
|
|
public static function modalMaxWidth(): string
|
|
{
|
|
return 'lg';
|
|
}
|
|
|
|
public function mount(string $finding): void
|
|
{
|
|
$model = DiscoveryFinding::where('uuid', $finding)->firstOrFail();
|
|
$this->findingUuid = $model->uuid;
|
|
$this->name = $model->name ?? $model->identifier;
|
|
}
|
|
|
|
#[Computed]
|
|
public function finding(): DiscoveryFinding
|
|
{
|
|
return DiscoveryFinding::where('uuid', $this->findingUuid)->firstOrFail();
|
|
}
|
|
|
|
public function assign()
|
|
{
|
|
$this->validate([
|
|
'name' => 'required|string|max:100',
|
|
'roomId' => 'nullable|integer|exists:rooms,id',
|
|
]);
|
|
|
|
$finding = $this->finding();
|
|
|
|
$device = Device::create([
|
|
'name' => $this->name,
|
|
'vendor' => $finding->vendor,
|
|
'model' => $finding->model,
|
|
'protocol' => $finding->vendor === 'Shelly' ? 'mqtt' : null,
|
|
'config' => ['mqtt_prefix' => $finding->identifier],
|
|
'room_id' => $this->roomId ?: null,
|
|
'status' => 'active',
|
|
]);
|
|
|
|
$finding->update(['status' => 'assigned', 'device_id' => $device->id]);
|
|
|
|
$this->closeModal();
|
|
|
|
return redirect()->route('devices.show', $device);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.modals.assign-device', [
|
|
'finding' => $this->finding(),
|
|
'rooms' => Room::orderBy('sort')->orderBy('name')->get(),
|
|
]);
|
|
}
|
|
}
|