cms-laravel/app/Livewire/UserManagement/UserList.php

79 lines
2.5 KiB
PHP

<?php
namespace App\Livewire\UserManagement;
use Livewire\Component;
use App\Services\ApiService;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Log;
class UserList extends Component
{
public $users = [];
public $loading = false;
public $updating = false;
protected $apiService;
public function boot(ApiService $apiService)
{
$this->apiService = $apiService;
}
public function mount()
{
// Check role-based access
$userInfo = Session::get('userInfo');
if (!$userInfo || $userInfo['role'] != 1) {
return redirect()->route('404');
}
$this->loadUsers();
}
public function loadUsers()
{
$this->loading = true;
try {
$response = $this->apiService->get('admin?page=1&page_size=10&_sort_by=create_dt&_sort_order=desc');
if ($response && isset($response['data'])) {
$this->users = $response['data'];
Log::info('User list loaded', ['source' => 'UserManagementUserList']);
}
} catch (\Exception $e) {
Session::flash('error', 'Failed to load users: ' . $e->getMessage());
Log::error('Failed to load user list', ['error' => $e->getMessage(), 'source' => 'UserManagementUserList']);
} finally {
$this->loading = false;
}
}
public function updateStatus($admin_uuid, $status)
{
$this->updating = true;
try {
$response = $this->apiService->post('adminChangeStatus', [
'admin_uuid' => $admin_uuid,
'status' => $status,
]);
if ($response && isset($response['status']) && $response['status'] === 200) {
Session::flash('success', $response['message'] ?? 'Status updated successfully.');
Log::info('User status updated', ['source' => 'UserManagementUserList']);
$this->loadUsers(); // Refresh the list
}
} catch (\Exception $e) {
Session::flash('error', 'Something went wrong updating record: ' . $e->getMessage());
Log::error('Failed to update user status', ['error' => $e->getMessage(), 'source' => 'UserManagementUserList']);
} finally {
$this->updating = false;
}
}
public function render()
{
return view('livewire.user-management.user-list')->layout('layouts.app', ['title' => 'User Management']);
}
}