cms-frontend/app/Http/Controllers/StationController.php

200 lines
7.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
class StationController extends Controller
{
protected $apiBaseUrl = 'http://192.168.100.6:8081/api/';
public function index(Request $request)
{
$params = [
'page' => $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();
$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', // Mocked; replace with actual API field if available
'date_created' => 'N/A', // Mocked
'created_by' => 'N/A', // Mocked
'modified_by' => 'N/A', // Mocked
'date_modified' => 'N/A', // Mocked
];
}, $data['data']);
// Mock pagination since API may not support it
$total = count($data['data']);
$lastPage = ceil($total / $params['page_size']);
$currentPage = min($currentPage, $lastPage);
// Handle search client-side if API doesn't support _search
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']);
}
// Handle sorting client-side if API doesn't support sort
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);
});
}
// Paginate manually
$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']);
// }
}