Files
portal-api/app/Http/Livewire/VPSReinstallModal.php
T
2025-08-03 23:06:08 +07:00

106 lines
3.0 KiB
PHP

<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Os;
use App\Models\OsGroup;
use App\Models\VPS;
use App\Models\InvoiceItem;
class VPSReinstallModal extends Component
{
public $productId;
public $selectedOsGroupId;
public $selectedOsId;
public $osGroups = [];
public $operatingSystems = [];
public $currentOs;
public $invoiceId;
protected $listeners = ['showOsModal' => 'loadOsData'];
public function mount($productId = null, $invoiceId = null)
{
$this->productId = $productId;
$this->invoiceId = $invoiceId;
if ($productId) {
$this->loadOsData($productId);
}
}
public function loadOsData($productId)
{
$this->productId = $productId;
// Get the VPS product
$vps = VPS::with('operatingSystems.osGroup')->find($productId);
if (!$vps) {
return;
}
// Get OS groups that are assigned to this VPS product
$osGroupIds = $vps->operatingSystems->pluck('osGroup.id')->filter()->unique();
$this->osGroups = OsGroup::with(['os' => function($query) use ($vps) {
// Only get OS versions that are assigned to this specific VPS product
$query->whereHas('vpsProducts', function($q) use ($vps) {
$q->where('product_id', $vps->id)
->where('product_type', 4);
})->where('status', 1);
}])
->whereIn('id', $osGroupIds)
->where('status', 1)
->get();
// Get current OS from invoice_item
if ($this->invoiceId) {
$invoiceItem = InvoiceItem::where('invoice_id', $this->invoiceId)
->where('product_id', $productId)
->where('product_type', 4)
->with('os.osGroup')
->first();
$this->currentOs = $invoiceItem?->os;
}
}
public function updatedSelectedOsGroupId($value)
{
$this->selectedOsId = null;
if ($value) {
$this->operatingSystems = Os::where('os_group_id', $value)
->where('status', 1)
->whereHas('vpsProducts', function($query) {
$query->where('product_id', $this->productId)
->where('product_type', 4);
})
->get();
} else {
$this->operatingSystems = [];
}
}
public function confirmReinstall()
{
if (!$this->selectedOsId) {
session()->flash('error', __('Please select an operating system.'));
return;
}
// Here you would implement the reinstall logic
// For now, just show a success message
session()->flash('success', __('Operating system reinstall initiated.'));
// Close modal
$this->dispatchBrowserEvent('close-modal', ['name' => 'reinstall-vm']);
}
public function render()
{
return view('livewire.v-p-s-reinstall-modal');
}
}