apiService = $apiService; } public function index() { try { $response = $this->apiService->get('/system-parameters'); if ($response->successful()) { return view('pages.system-parameters', [ 'parameters' => $response->json()['data'] ]); } return back()->with('error', 'Unable to fetch system parameters.'); } catch (\Exception $e) { return back()->with('error', 'Service unavailable.'); } } public function store(Request $request) { $validated = $request->validate([ 'name' => 'required|string|max:255|unique:system_parameters,name', 'value' => 'required|string', 'type' => 'required|string|in:String,Number,Boolean,JSON', 'description' => 'required|string' ]); try { // Format value based on type $validated['value'] = $this->formatValue($validated['value'], $validated['type']); $response = $this->apiService->post('/system-parameters', $validated); if ($response->successful()) { return redirect()->route('system-parameters') ->with('success', 'System parameter created successfully.'); } return back()->withErrors($response->json()['errors']); } catch (\Exception $e) { return back()->with('error', 'Unable to create system parameter.'); } } public function update(Request $request, $id) { $validated = $request->validate([ 'name' => 'required|string|max:255|unique:system_parameters,name,'.$id, 'value' => 'required|string', 'type' => 'required|string|in:String,Number,Boolean,JSON', 'description' => 'required|string' ]); try { // Format value based on type $validated['value'] = $this->formatValue($validated['value'], $validated['type']); $response = $this->apiService->put("/system-parameters/{$id}", $validated); if ($response->successful()) { return redirect()->route('system-parameters') ->with('success', 'System parameter updated successfully.'); } return back()->withErrors($response->json()['errors']); } catch (\Exception $e) { return back()->with('error', 'Unable to update system parameter.'); } } public function destroy($id) { try { $response = $this->apiService->delete("/system-parameters/{$id}"); if ($response->successful()) { return redirect()->route('system-parameters') ->with('success', 'System parameter deleted successfully.'); } return back()->with('error', 'Unable to delete system parameter.'); } catch (\Exception $e) { return back()->with('error', 'Service unavailable.'); } } protected function formatValue($value, $type) { switch ($type) { case 'Number': return is_numeric($value) ? (float) $value : $value; case 'Boolean': return filter_var($value, FILTER_VALIDATE_BOOLEAN); case 'JSON': return is_string($value) && json_decode($value) ? $value : json_encode($value); default: return (string) $value; } } }