139 lines
4.6 KiB
PHP
139 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\StoreUserRequest;
|
|
use App\Http\Requests\Admin\UpdateUserRequest;
|
|
use App\Http\Requests\Admin\UpdateAddCardRequest;
|
|
use App\DTOs\UserFilterDto;
|
|
use App\Services\Admin\Contracts\AdminServiceInterface;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class AdminController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly AdminServiceInterface $adminService
|
|
) {}
|
|
|
|
/**
|
|
* Display a listing of the users with statistics.
|
|
*/
|
|
public function index(Request $request): View|JsonResponse
|
|
{
|
|
$filters = UserFilterDto::fromRequest($request);
|
|
|
|
[
|
|
'users' => $users,
|
|
'topReceivedUser' => $topReceivedUser,
|
|
'topSentUser' => $topSentUser,
|
|
'topReceivedUsers' => $topReceivedUsers,
|
|
'topSentUsers' => $topSentUsers
|
|
] = $this->adminService->getUserListWithStats($filters);
|
|
|
|
if ($request->ajax() || $request->wantsJson()) {
|
|
return response()->json([
|
|
'title' => view('admin.users.partials.title', ['selectedMonth' => $filters->selectedMonth])->render(),
|
|
'stats' => view('admin.users.partials.stats', compact('topReceivedUser', 'topSentUser', 'topReceivedUsers', 'topSentUsers'))->render(),
|
|
'table' => view('admin.users.partials.table', compact('users'))->render(),
|
|
]);
|
|
}
|
|
|
|
return view('admin.users.index', compact(
|
|
'users',
|
|
'topReceivedUser',
|
|
'topSentUser',
|
|
'topReceivedUsers',
|
|
'topSentUsers'
|
|
))->with([
|
|
'selectedMonth' => $filters->selectedMonth,
|
|
'search' => $filters->search,
|
|
'status' => $filters->status,
|
|
'role' => $filters->role,
|
|
'department' => $filters->department,
|
|
'flagSend' => $filters->flagSend,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new user.
|
|
*/
|
|
public function create(): View
|
|
{
|
|
return view('admin.users.create');
|
|
}
|
|
|
|
/**
|
|
* Store a newly created user in storage.
|
|
*/
|
|
public function store(StoreUserRequest $request): RedirectResponse
|
|
{
|
|
$this->adminService->createUser($request->validated());
|
|
|
|
return redirect()->route('admin.users.index')->with('success', __('messages.user_create_success'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified user.
|
|
*/
|
|
public function edit(Request $request, string $msnv): View
|
|
{
|
|
$user = $this->adminService->getUserByMsnv($msnv);
|
|
$selectedMonth = (string) $request->input('month', Carbon::now()->format('Y-m'));
|
|
$administrations = $this->adminService->getUserTransactions($msnv, $selectedMonth);
|
|
$addCards = $this->adminService->getAddCardsHistory($user->msnv);
|
|
$admins = $this->adminService->getActiveAdmins();
|
|
|
|
return view('admin.users.edit', compact('user', 'administrations', 'selectedMonth', 'addCards', 'admins'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified user in storage.
|
|
*/
|
|
public function update(UpdateUserRequest $request, string $msnv): RedirectResponse
|
|
{
|
|
$user = $this->adminService->getUserByMsnv($msnv);
|
|
|
|
$this->adminService->updateUser($user, $request->validated());
|
|
|
|
return redirect()->back()->with('success', __('messages.user_update_success'));
|
|
}
|
|
|
|
/**
|
|
* Remove (deactivate) the specified user from storage.
|
|
*/
|
|
public function destroy(string $msnv): RedirectResponse
|
|
{
|
|
$this->adminService->deactivateUser($msnv);
|
|
|
|
return redirect()->route('admin.users.index')->with('success', __('messages.user_deactivate_success'));
|
|
}
|
|
|
|
/**
|
|
* Update card allocation history record.
|
|
*/
|
|
public function updateAddCard(UpdateAddCardRequest $request, int $id): RedirectResponse
|
|
{
|
|
try {
|
|
$this->adminService->updateAddCard($id, $request->validated());
|
|
return redirect()->back()->with('success', 'Cập nhật lịch sử cấp phát thẻ thành công.');
|
|
} catch (\Exception $e) {
|
|
return redirect()->back()->withErrors(['error' => $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reset all cards for active users.
|
|
*/
|
|
public function resetCards(): RedirectResponse
|
|
{
|
|
$this->adminService->resetAllCards();
|
|
|
|
return redirect()->back()->with('success', __('messages.cards_reset_success'));
|
|
}
|
|
}
|