apiService = $apiService; } public function mount($id) { // Check role-based access $userInfo = Session::get('userInfo'); if (!$userInfo || $userInfo['role'] != 1) { return redirect()->route('404'); } $this->id = $id; try { $response = $this->apiService->get("topUp/{$id}"); if ($response && isset($response['data'])) { $this->userInfo = $response['data']; $this->fee_code = $this->userInfo['fee_code'] ?? ''; $this->name = $this->userInfo['name'] ?? ''; $this->amount = $this->userInfo['amount'] ?? ''; $this->type = $this->userInfo['type'] == 'PH peso' ? '1' : '2'; $this->mounted = true; Log::info('Top-up data loaded for edit', ['source' => 'TopUpEdit']); } } catch (\Exception $e) { Session::flash('error', 'Something went wrong loading data: ' . $e->getMessage()); Log::error('Failed to load top-up data', ['error' => $e->getMessage(), 'source' => 'TopUpEdit']); if ($e->getCode() == 404) { return redirect()->route('404'); } $this->mounted = false; } } public function update() { $this->loading = true; $this->errors = []; try { $this->validate([ 'fee_code' => ['required'], 'name' => ['required', 'max:128', 'regex:/^[A-Za-z0-9.ñÑ ]+$/'], 'amount' => ['required', 'numeric'], 'type' => ['required'], ], [ 'fee_code.required' => 'Fee Code is required!', 'name.required' => 'Name is required!', 'name.max' => 'Maximum character is 128.', 'name.regex' => 'Invalid Name.', 'amount.required' => 'Amount is required!', 'amount.numeric' => 'Amount must be a number.', 'type.required' => 'Type is required!', ]); $params = [ 'fee_code' => $this->fee_code, 'name' => trim($this->name), 'amount' => $this->amount, 'type' => $this->type, ]; $response = $this->apiService->put("topUp/{$this->userInfo['topup_uuid']}", $params); if ($response && isset($response['status']) && $response['status'] === 200) { Session::flash('success', 'Record was successfully updated.'); Log::info('Top-up updated successfully', ['source' => 'TopUpEdit']); return redirect()->route('top-up'); } } catch (\Illuminate\Validation\ValidationException $e) { $this->errors = $e->errors(); Session::flash('error', 'Something went wrong updating record: ' . implode(', ', array_merge(...array_values($e->errors())))); Log::error('Validation failed', ['errors' => $e->errors(), 'source' => 'TopUpEdit']); } catch (\Exception $e) { Session::flash('error', 'Something went wrong updating record: ' . $e->getMessage()); Log::error('Failed to update top-up', ['error' => $e->getMessage(), 'source' => 'TopUpEdit']); } finally { $this->loading = false; } } public function resetAmount() { $this->amount = ''; } public function render() { return view('livewire.top-up.edit')->layout('layouts.app', ['title' => 'Update Top-Up']); } }