$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.form', ['mode' => '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, $selectedMonth); $admins = $this->adminService->getActiveAdmins(); return view('admin.users.form', compact('user', 'administrations', 'selectedMonth', 'addCards', 'admins'))->with('mode', 'edit'); } /** * Update the specified user in storage. */ public function update(UpdateUserRequest $request, string $msnv) { $user = $this->adminService->getUserByMsnv($msnv); $this->adminService->updateUser($user, $request->validated()); if ($request->expectsJson()) { return response()->json([ 'success' => true, 'message' => __('messages.user_update_success'), 'card_balance' => $user->fresh()->card, ]); } 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) { try { $this->adminService->updateAddCard($id, $request->validated()); if ($request->expectsJson()) { return response()->json([ 'success' => true, 'message' => 'Cập nhật lịch sử cấp phát thẻ thành công.', ]); } return redirect()->back()->with('success', 'Cập nhật lịch sử cấp phát thẻ thành công.'); } catch (\Exception $e) { if ($request->expectsJson()) { return response()->json([ 'success' => false, 'message' => $e->getMessage(), ], 422); } return redirect()->back()->withErrors(['error' => $e->getMessage()]); } } /** * Delete card allocation history record. */ public function destroyAddCard(Request $request, int $id) { try { $this->adminService->destroyAddCard($id); if ($request->expectsJson()) { return response()->json([ 'success' => true, 'message' => 'Xóa lịch sử cấp phát thẻ thành công.', ]); } return redirect()->back()->with('success', 'Xóa lịch sử cấp phát thẻ thành công.'); } catch (\Exception $e) { if ($request->expectsJson()) { return response()->json([ 'success' => false, 'message' => $e->getMessage(), ], 422); } 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')); } }