CluPilotCloud/app/Livewire/Admin/ConfirmDeleteDatacenter.php

48 lines
1.3 KiB
PHP

<?php
namespace App\Livewire\Admin;
use App\Models\Datacenter;
use LivewireUI\Modal\ModalComponent;
/**
* Confirmation for deleting a datacenter (R5). Guarded: a datacenter that still
* has hosts cannot be deleted — it would orphan host placement — so the modal
* blocks and explains instead. Otherwise it is safe to hard-delete an unused code.
*/
class ConfirmDeleteDatacenter extends ModalComponent
{
public string $uuid;
public string $name = '';
public int $hostCount = 0;
public function mount(string $uuid): void
{
$dc = Datacenter::query()->where('uuid', $uuid)->withCount('hosts')->firstOrFail();
$this->uuid = $uuid;
$this->name = $dc->name;
$this->hostCount = $dc->hosts_count;
}
public function delete()
{
$dc = Datacenter::query()->where('uuid', $this->uuid)->withCount('hosts')->first();
// Re-check under the current state — never delete a datacenter with hosts.
if ($dc === null || $dc->hosts_count > 0) {
return $this->redirectRoute('admin.datacenters', navigate: true);
}
$dc->delete();
return $this->redirectRoute('admin.datacenters', navigate: true);
}
public function render()
{
return view('livewire.admin.confirm-delete-datacenter');
}
}