'loadOsData']; public function mount($productId = null, $invoiceId = null) { $this->productId = $productId; $this->invoiceId = $invoiceId; $this->operatingSystems = collect(); // Initialize as empty collection if ($productId) { $this->loadOsData($productId); } } public function loadOsData($productId) { $this->productId = $productId; $this->isLoading = true; try { // Get the VPS product $vps = VPS::with('operatingSystems.osGroup')->find($productId); if (!$vps) { session()->flash('error', __('Không tìm thấy thông tin VPS.')); return; } // Store VPS info for confirmation message $this->vpsInfo = $vps; // 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 and ModuleVmware info from invoice_item if ($this->invoiceId) { $this->invoiceItem = InvoiceItem::where('invoice_id', $this->invoiceId) ->where('product_id', $productId) ->where('product_type', 4) ->with('os.osGroup') ->first(); $this->currentOs = $this->invoiceItem?->os; // Debug InvoiceItem info first Log::info('InvoiceItem debug info', [ 'invoice_id' => $this->invoiceId, 'product_id' => $productId, 'product_type' => 4, 'invoice_item_found' => $this->invoiceItem ? 'yes' : 'no', 'invoice_item_id' => $this->invoiceItem?->id ?? 'null', 'invoice_item_relid' => $this->invoiceItem?->relid ?? 'null', 'invoice_item_type_id' => $this->invoiceItem?->type_id ?? 'null', 'invoice_item_product_id' => $this->invoiceItem?->product_id ?? 'null', 'invoice_item_product_type' => $this->invoiceItem?->product_type ?? 'null' ]); // Get ModuleVmware info to access IP address if ($this->invoiceItem) { // Try to get ModuleVmware directly using invoice_id and customer_id $this->invoice = Invoice::find($this->invoiceId); if ($this->invoice && $this->invoice->customer_id) { $this->moduleVmware = ModuleVmware::where('customer_id', $this->invoice->customer_id) ->where('order_id', $this->invoiceId) ->with('ipAddress') ->first(); // If not found by order_id, try to find by customer_id and product info if (!$this->moduleVmware) { $this->moduleVmware = ModuleVmware::where('customer_id', $this->invoice->customer_id) ->with('ipAddress') ->first(); } } // Try to get IP from vCenter API first, fallback to database $this->vpsIpAddress = $this->getIPFromVCenter() ?? $this->moduleVmware?->ipAddress?->ip ?? null; // Debug ModuleVmware info Log::info('ModuleVmware debug info', [ 'invoice_item_id' => $this->invoiceItem->id, 'invoice_item_relid' => $this->invoiceItem->relid, 'invoice_item_type_id' => $this->invoiceItem->type_id, 'invoice_customer_id' => $this->invoice?->customer_id ?? 'null', 'module_vmware' => $this->moduleVmware ? 'found' : 'not_found', 'module_vmware_id' => $this->moduleVmware?->id ?? 'null', 'module_vmware_name' => $this->moduleVmware?->name ?? 'null', 'module_vmware_customer_id' => $this->moduleVmware?->customer_id ?? 'null', 'module_ip_address_id' => $this->moduleVmware?->module_ip_address_id ?? 'null', 'ip_address_relationship' => $this->moduleVmware?->ipAddress ? 'found' : 'not_found', 'ip_address_from_db' => $this->moduleVmware?->ipAddress?->ip ?? 'null', 'ip_address_from_vcenter' => $this->vpsIpAddress ?? 'null' ]); } else { Log::warning('InvoiceItem not found', [ 'invoice_id' => $this->invoiceId, 'product_id' => $productId, 'product_type' => 4 ]); } } Log::info('OS data loaded successfully', [ 'product_id' => $productId, 'os_groups_count' => is_object($this->osGroups) ? $this->osGroups->count() : count($this->osGroups), 'current_os' => $this->currentOs ? $this->currentOs->id : null, 'module_vmware_id' => $this->moduleVmware ? $this->moduleVmware->id : null, 'ip_address' => $this->moduleVmware?->ipAddress?->ip ?? 'N/A' ]); } catch (\Exception $e) { Log::error('Error loading OS data', [ 'product_id' => $productId, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); session()->flash('error', __('Có lỗi xảy ra khi tải dữ liệu hệ điều hành.')); } finally { $this->isLoading = false; } } public function updatedSelectedOsGroupId($value) { // Reset selected OS when changing group $this->selectedOsId = null; $this->selectedOsDetails = null; $this->isLoading = true; try { 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); }) ->with('osGroup') ->get(); Log::info('Operating systems loaded for group', [ 'os_group_id' => $value, 'os_count' => is_object($this->operatingSystems) ? $this->operatingSystems->count() : count($this->operatingSystems), 'selected_os_id' => $this->selectedOsId ]); } else { $this->operatingSystems = collect(); } } catch (\Exception $e) { Log::error('Error loading operating systems', [ 'os_group_id' => $value, 'error' => $e->getMessage() ]); session()->flash('error', __('Có lỗi xảy ra khi tải danh sách phiên bản hệ điều hành.')); $this->operatingSystems = collect(); } finally { $this->isLoading = false; } } public function updatedSelectedOsId($value) { Log::info('Selected OS ID updated', [ 'selected_os_id' => $value, 'is_loading' => $this->isLoading ]); // Store selected OS details for confirmation message if ($value) { $this->selectedOsDetails = Os::with('osGroup')->find($value); Log::info('OS details loaded', [ 'os_id' => $value, 'os_name' => $this->selectedOsDetails?->osGroup?->name ?? 'null', 'os_version' => $this->selectedOsDetails?->version ?? 'null' ]); } else { $this->selectedOsDetails = null; } } public function confirmReinstall() { Log::info('confirmReinstall called', [ 'selected_os_id' => $this->selectedOsId, 'invoice_id' => $this->invoiceId, 'is_loading' => $this->isLoading ]); if (!$this->selectedOsId) { session()->flash('error', __('Vui lòng chọn hệ điều hành.')); return; } if (!$this->invoiceId) { session()->flash('error', __('Thiếu thông tin hóa đơn.')); return; } $this->isLoading = true; try { $response = Http::withoutVerifying() ->withHeaders([ 'X-CSRF-TOKEN' => csrf_token(), 'Accept' => 'application/json', 'Content-Type' => 'application/json', ]) ->post(route('service.reinstall.vm'), [ 'invoice_id' => $this->invoiceId, 'os_id' => $this->selectedOsId, ]); Log::info('Reinstall response', [ 'status' => $response->status(), 'body' => $response->body() ]); if ($response->successful()) { session()->flash('success', __('Yêu cầu cài lại hệ điều hành đã được gửi thành công.')); $this->dispatch('close-modal', ['name' => 'reinstall-vm']); } else { $errorMessage = $response->json('message') ?? __('Có lỗi xảy ra khi gửi yêu cầu.'); session()->flash('error', $errorMessage); } } catch (\Exception $e) { Log::error('Error during reinstall request', [ 'invoice_id' => $this->invoiceId, 'os_id' => $this->selectedOsId, 'error' => $e->getMessage() ]); session()->flash('error', __('Có lỗi xảy ra khi gửi yêu cầu cài lại.')); } finally { $this->isLoading = false; } } /** * Get IP address from vCenter API */ private function getIPFromVCenter() { try { if (!$this->moduleVmware || !$this->moduleVmware->vmware_id) { Log::warning('No ModuleVmware or vmware_id found for vCenter IP lookup'); return null; } // Get vCenter configuration from ModuleVmware $vcenter = $this->moduleVmware->ipAddress?->host?->datacenter?->vCenter; if (!$vcenter) { Log::warning('No vCenter configuration found for ModuleVmware', [ 'module_vmware_id' => $this->moduleVmware->id, 'vmware_id' => $this->moduleVmware->vmware_id ]); return null; } // Initialize vCenter service $vCenterService = new \App\Http\Services\vCenter(); // Login to vCenter $loginSuccess = $vCenterService->loginServer($vcenter); if (!$loginSuccess) { Log::warning('Failed to login to vCenter for IP lookup', [ 'vmware_id' => $this->moduleVmware->vmware_id, 'vcenter_id' => $vcenter->id ]); return null; } // Get IP address from vCenter $ipInfo = $vCenterService->GetIPAddressVirtualMachine($this->moduleVmware->vmware_id); Log::info('vCenter IP lookup result', [ 'vmware_id' => $this->moduleVmware->vmware_id, 'ip_info' => $ipInfo ]); if (is_array($ipInfo) && isset($ipInfo['error']) && $ipInfo['error'] === false) { // Try different possible structures if (isset($ipInfo['data']['ip_address'])) { return $ipInfo['data']['ip_address']; } elseif (isset($ipInfo['data']['value']['ip_address'])) { return $ipInfo['data']['value']['ip_address']; } } Log::warning('No valid IP address found in vCenter response', [ 'vmware_id' => $this->moduleVmware->vmware_id, 'ip_info' => $ipInfo ]); return null; } catch (\Exception $e) { Log::error('Error getting IP from vCenter', [ 'vmware_id' => $this->moduleVmware?->vmware_id ?? 'unknown', 'error' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine() ]); return null; } } public function render() { return view('livewire.v-p-s-reinstall-modal'); } }