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

182 lines
7.1 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;
class LockedAccountController extends Controller
{
protected $apiBaseUrl = 'http://192.168.100.6:8081/api';
/**
* Display the main page with locked accounts.
*
* @param Request $request
* @return \Illuminate\View\View
*/
public function index(Request $request)
{
try {
// Force a log to confirm the method is reached
Log::debug('Entering LockedAccountController index method', ['request' => $request->all()]);
$user = Session::get('user');
$accessToken = $user['access_token'] ?? null;
if (!$accessToken) {
Log::warning('No access token found, redirecting to login from locked-accounts');
return redirect()->route('login')->with('error', 'Please log in to view locked accounts.');
}
Log::debug('Access token found', ['access_token' => $accessToken]);
// Prepare query parameters
$params = [
'page' => $request->input('page', 1),
'page_size' => $request->input('page_size', 5), // Match CardMemberController's default
'_search' => $request->input('_search', null),
'status' => $request->input('status', null),
'_locked' => 1,
];
Log::debug('Making API call to fetch locked accounts', [
'url' => "{$this->apiBaseUrl}/cms/member",
'params' => $params,
]);
$response = Http::withHeaders([
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
])->get("{$this->apiBaseUrl}/cms/member", $params);
Log::debug('API response received', [
'status' => $response->status(),
'body' => $response->body(),
]);
if ($response->status() === 401 || $response->status() === 403) {
Log::warning('Unauthorized or Forbidden API response', ['response' => $response->json()]);
return redirect()->route('login')->with('error', 'Your session has expired. Please log in again.');
}
$json = $response->json();
Log::info('Locked Accounts API Raw Response', ['response' => $json]);
if ($response->successful() && isset($json['data']) && is_array($json['data'])) {
$accounts = array_map(function ($account) {
Log::info('Processing locked account record', ['account' => $account]);
return [
'id' => $account['lcard_uuid'] ?? null,
'cardNumber' => $account['card_number'] ?? '',
'firstName' => $account['firstname'] ?? '',
'lastName' => $account['lastname'] ?? '',
'birthday' => $account['birthdate'] ?? '',
'cardType' => $account['card_type'] ?? '',
'status' => $account['status'] ? 'Active' : 'Inactive',
'is_locked' => $account['is_locked'] ?? 1, // Ensure locked status
];
}, $json['data']);
$total = $json['meta']['total'] ?? count($accounts);
$lastPage = $json['meta']['last_page'] ?? ceil($total / $params['page_size']);
} else {
Log::warning('No locked account data found or invalid API response', ['response' => $json]);
$accounts = [];
$total = 0;
$lastPage = 1;
}
Log::debug('Rendering view with data', [
'accounts' => $accounts,
'currentPage' => $params['page'],
'lastPage' => $lastPage,
'total' => $total,
'params' => $params,
]);
return view('pages.member management.locked-accounts', [
'members' => $accounts, // Match naming convention with CardMemberController
'currentPage' => $params['page'],
'lastPage' => $lastPage,
'total' => $total,
'search' => $params['_search'],
'params' => $params, // Include params for view compatibility
]);
} catch (\Exception $e) {
Log::error('Error in LockedAccountController index method', [
'message' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
return view('pages.member management.locked-accounts', [
'members' => [],
'currentPage' => 1,
'lastPage' => 1,
'total' => 0,
'search' => $params['_search'] ?? null,
'params' => $params ?? [
'page' => 1,
'page_size' => 5,
'_search' => null,
'status' => null,
'_locked' => 1,
],
]);
}
}
/**
* Activate a locked account.
*
* @param string $uuid
* @return \Illuminate\Http\RedirectResponse
*/
public function activate($uuid)
{
try {
$user = Session::get('user');
$accessToken = $user['access_token'] ?? null;
if (!$accessToken) {
Log::warning('No access token found, redirecting to login from activate account');
return redirect()->route('login')->with('error', 'Please log in to activate an account.');
}
Log::debug('Making API call to activate account', [
'url' => "{$this->apiBaseUrl}/cms/memberActivate/{$uuid}",
'uuid' => $uuid,
]);
$response = Http::withHeaders([
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $accessToken,
])->post("{$this->apiBaseUrl}/cms/memberActivate/{$uuid}");
Log::debug('Activate API response received', [
'status' => $response->status(),
'body' => $response->body(),
]);
if ($response->successful()) {
return redirect()->route('locked-accounts')->with('success', 'Account activated successfully.');
} else {
Log::warning('Failed to activate account', ['uuid' => $uuid, 'response' => $response->json()]);
return redirect()->route('locked-accounts')->with('error', $response->json()['message'] ?? 'Failed to activate account. Please try again.');
}
} catch (\Exception $e) {
Log::error('Error activating account', [
'uuid' => $uuid,
'message' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
return redirect()->route('locked-accounts')->with('error', 'An error occurred while activating the account.');
}
}
public function show()
{
return view('pages.locked-account-view');
}
}