$request->input('page', 1), 'page_size' => 5, '_search' => $request->input('_search'), 'sort' => $request->input('sort', 'code|asc'), ]; $response = Http::get($this->apiBaseUrl . 'cms/getStations', $params); $data = $response->json(); \Log::info('Stations API Response:', ['status' => $response->status(), 'data' => $data]); $stations = []; $currentPage = $params['page']; $lastPage = 1; $total = 0; if ($response->successful() && isset($data['data'])) { $stations = array_map(function ($item) { return [ 'id' => $item['station_uuid'], 'station_code' => $item['code'], 'station_name' => $item['description'], 'branch_name' => 'N/A', 'date_created' => 'N/A', 'created_by' => 'N/A', 'modified_by' => 'N/A', 'date_modified' => 'N/A', ]; }, $data['data']); $total = count($data['data']); $lastPage = ceil($total / $params['page_size']); $currentPage = min($currentPage, $lastPage); if ($params['_search']) { $searchTerm = strtolower($params['_search']); $stations = array_filter($stations, function ($station) use ($searchTerm) { return str_contains(strtolower($station['station_code']), $searchTerm) || str_contains(strtolower($station['station_name']), $searchTerm) || str_contains(strtolower($station['branch_name']), $searchTerm); }); $stations = array_values($stations); $total = count($stations); $lastPage = ceil($total / $params['page_size']); } if ($params['sort']) { [$sortField, $sortDir] = explode('|', $params['sort']); usort($stations, function ($a, $b) use ($sortField, $sortDir) { $aValue = $a[$sortField] ?? ''; $bValue = $b[$sortField] ?? ''; return $sortDir === 'asc' ? strcmp($aValue, $bValue) : strcmp($bValue, $aValue); }); } $start = ($currentPage - 1) * $params['page_size']; $stations = array_slice($stations, $start, $params['page_size']); } return view('pages.station locator.stations', compact('stations', 'currentPage', 'lastPage', 'total')); } // public function create() // { // return view('pages.add-station'); // } // public function store(Request $request) // { // $request->validate([ // 'station_code' => 'required|string|max:50|unique:stations,station_code', // 'station_name' => 'required|string|max:255', // 'branch_name' => 'required|string|max:255', // ]); // // Mock API call to store station // $response = Http::post($this->apiBaseUrl . 'cms/stations/store', [ // 'code' => $request->station_code, // 'description' => $request->station_name, // 'branch_name' => $request->branch_name, // 'is_viewable' => 1, // 'is_active' => 1, // ]); // if ($response->successful()) { // return redirect()->route('station-management')->with('success', 'Station added successfully'); // } // return back()->withErrors(['error' => 'Failed to add station']); // } // public function show($id) // { // $response = Http::get($this->apiBaseUrl . 'cms/getStations'); // $data = $response->json(); // $station = null; // if ($response->successful() && isset($data['data'])) { // $station = collect($data['data'])->firstWhere('station_uuid', $id); // if ($station) { // $station = [ // 'id' => $station['station_uuid'], // 'station_code' => $station['code'], // 'station_name' => $station['description'], // 'branch_name' => 'N/A', // 'date_created' => 'N/A', // 'created_by' => 'N/A', // 'modified_by' => 'N/A', // 'date_modified' => 'N/A', // ]; // } // } // if (!$station) { // abort(404, 'Station not found'); // } // return view('pages.view-station', compact('station')); // } // public function edit($id) // { // $response = Http::get($this->apiBaseUrl . 'cms/getStations'); // $data = $response->json(); // $station = null; // if ($response->successful() && isset($data['data'])) { // $station = collect($data['data'])->firstWhere('station_uuid', $id); // if ($station) { // $station = [ // 'id' => $station['station_uuid'], // 'station_code' => $station['code'], // 'station_name' => $station['description'], // 'branch_name' => 'N/A', // 'date_created' => 'N/A', // 'created_by' => 'N/A', // 'modified_by' => 'N/A', // 'date_modified' => 'N/A', // ]; // } // } // if (!$station) { // abort(404, 'Station not found'); // } // return view('pages.edit-station', compact('station')); // } // public function update(Request $request, $id) // { // $request->validate([ // 'station_code' => 'required|string|max:50|unique:stations,station_code,' . $id . ',id', // 'station_name' => 'required|string|max:255', // 'branch_name' => 'required|string|max:255', // ]); // // Mock API call to update station // $response = Http::put($this->apiBaseUrl . 'cms/stations/update/' . $id, [ // 'code' => $request->station_code, // 'description' => $request->station_name, // 'branch_name' => $request->branch_name, // ]); // if ($response->successful()) { // return redirect()->route('station-management')->with('success', 'Station updated successfully'); // } // return back()->withErrors(['error' => 'Failed to update station']); // } // public function destroy($id) // { // // Mock API call to delete station // $response = Http::delete($this->apiBaseUrl . 'cms/stations/delete/' . $id); // if ($response->successful()) { // return redirect()->route('station-management')->with('success', 'Station deleted successfully'); // } // return back()->withErrors(['error' => 'Failed to delete station']); // } }