From e08b7f011bcac511bc792cc42f35e3bcdc6a187d Mon Sep 17 00:00:00 2001 From: ramzdhani11 <158116439+ramzdhani11@users.noreply.github.com> Date: Wed, 4 Feb 2026 19:02:45 +0700 Subject: [PATCH] Fix tambah admin --- app/Http/Controllers/AdminController.php | 138 ++++++ database/seeders/AdminSeeder.php | 5 + resources/views/Admin/index.blade.php | 8 +- resources/views/Admin/layouts/app.blade.php | 193 ++++++++- resources/views/Admin/manage-admin.blade.php | 402 +++++++++++------- .../views/Admin/partials/sidebar.blade.php | 8 +- resources/views/welcome.blade.php | 30 +- routes/web.php | 15 +- 8 files changed, 619 insertions(+), 180 deletions(-) create mode 100644 app/Http/Controllers/AdminController.php diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php new file mode 100644 index 0000000..070bb58 --- /dev/null +++ b/app/Http/Controllers/AdminController.php @@ -0,0 +1,138 @@ +route('admin.login')->with('error', 'Silakan login terlebih dahulu.'); + } + + $admins = User::where('role', 'admin')->get(); + \Log::info('Admins fetched for UI', ['count' => $admins->count(), 'admins' => $admins->pluck('email')->toArray()]); + + return view('Admin.manage-admin', compact('admins')); + } + + public function store(Request $request) + { + if (!session('admin_logged_in')) { + return response()->json(['error' => 'Unauthorized'], 401); + } + + // Debug log + \Log::info('Admin store request data', ['request_data' => $request->all()]); + + $validator = Validator::make($request->all(), [ + 'name' => 'required|string|max:255', + 'email' => 'required|string|email|max:255|unique:users', + 'password' => 'required|string|min:6', + 'role' => 'required|in:admin' + ]); + + if ($validator->fails()) { + return response()->json(['errors' => $validator->errors()], 422); + } + + $admin = User::create([ + 'name' => $request->name, + 'email' => $request->email, + 'password' => Hash::make($request->password), + 'role' => $request->role, + 'email_verified_at' => now() + ]); + + return response()->json(['success' => 'Admin berhasil ditambahkan', 'admin' => $admin]); + } + + public function edit($id) + { + if (!session('admin_logged_in')) { + return response()->json(['error' => 'Unauthorized'], 401); + } + + $admin = User::findOrFail($id); + return response()->json($admin); + } + + public function update(Request $request, $id) + { + if (!session('admin_logged_in')) { + return response()->json(['error' => 'Unauthorized'], 401); + } + + $admin = User::findOrFail($id); + + $validator = Validator::make($request->all(), [ + 'name' => 'required|string|max:255', + 'email' => 'required|string|email|max:255|unique:users,email,'.$id, + 'role' => 'required|in:admin' + ]); + + if ($validator->fails()) { + return response()->json(['errors' => $validator->errors()], 422); + } + + $admin->update([ + 'name' => $request->name, + 'email' => $request->email, + 'role' => $request->role + ]); + + if ($request->filled('password')) { + $admin->update(['password' => Hash::make($request->password)]); + } + + return response()->json(['success' => 'Admin berhasil diperbarui', 'admin' => $admin]); + } + + public function destroy($id) + { + if (!session('admin_logged_in')) { + return response()->json(['error' => 'Unauthorized'], 401); + } + + $admin = User::findOrFail($id); + + // Prevent deleting the currently logged in admin + if ($admin->id == session('admin_user_id')) { + return response()->json(['error' => 'Tidak dapat menghapus akun yang sedang digunakan'], 422); + } + + $admin->delete(); + + return response()->json(['success' => 'Admin berhasil dihapus']); + } + + public function toggleStatus($id) + { + if (!session('admin_logged_in')) { + return response()->json(['error' => 'Unauthorized'], 401); + } + + $admin = User::findOrFail($id); + + // Prevent deactivating the currently logged in admin + if ($admin->id == session('admin_user_id')) { + return response()->json(['error' => 'Tidak dapat menonaktifkan akun yang sedang digunakan'], 422); + } + + // For simplicity, we'll use email_verified_at as status indicator + if ($admin->email_verified_at) { + $admin->update(['email_verified_at' => null]); + $status = 'inactive'; + } else { + $admin->update(['email_verified_at' => now()]); + $status = 'active'; + } + + return response()->json(['success' => "Status admin berhasil diubah menjadi $status", 'status' => $status]); + } +} diff --git a/database/seeders/AdminSeeder.php b/database/seeders/AdminSeeder.php index 8f6f6dd..aa16804 100644 --- a/database/seeders/AdminSeeder.php +++ b/database/seeders/AdminSeeder.php @@ -13,11 +13,16 @@ class AdminSeeder extends Seeder */ public function run(): void { + // Clear existing admin users first + DB::table('users')->where('role', 'admin')->delete(); + + // Create single admin user DB::table('users')->insert([ 'name' => 'Admin', 'email' => 'admin@gmail.com', 'password' => Hash::make('admin123'), 'role' => 'admin', + 'email_verified_at' => now(), 'created_at' => now(), 'updated_at' => now(), ]); diff --git a/resources/views/Admin/index.blade.php b/resources/views/Admin/index.blade.php index 6f46aa2..a200aaf 100644 --- a/resources/views/Admin/index.blade.php +++ b/resources/views/Admin/index.blade.php @@ -47,12 +47,12 @@
Admin Aktif
+Total admin terdaftar
diff --git a/resources/views/Admin/layouts/app.blade.php b/resources/views/Admin/layouts/app.blade.php index 9379956..a94ea41 100644 --- a/resources/views/Admin/layouts/app.blade.php +++ b/resources/views/Admin/layouts/app.blade.php @@ -16,6 +16,9 @@ + + + @if(request()->routeIs('admin.dashboard') || request()->routeIs('admin.system-statistics')) @@ -26,6 +29,155 @@ font-family: 'Inter', sans-serif; } + /* Dark Mode Styles */ + .dark { + background-color: #1a1a1a; + color: #e5e5e5; + } + + .dark .bg-white { + background-color: #2d2d2d !important; + } + + .dark .bg-gray-50 { + background-color: #1f1f1f !important; + } + + .dark .bg-gray-100 { + background-color: #2d2d2d !important; + } + + .dark .text-gray-900 { + color: #e5e5e5 !important; + } + + .dark .text-gray-800 { + color: #d4d4d4 !important; + } + + .dark .text-gray-700 { + color: #b3b3b3 !important; + } + + .dark .text-gray-600 { + color: #999999 !important; + } + + .dark .text-gray-500 { + color: #808080 !important; + } + + .dark .border-gray-200 { + border-color: #404040 !important; + } + + .dark .border-gray-300 { + border-color: #404040 !important; + } + + .dark .hover\:bg-gray-50:hover { + background-color: #2d2d2d !important; + } + + .dark .hover\:bg-gray-100:hover { + background-color: #3d3d3d !important; + } + + .dark .hover\:bg-blue-50:hover { + background-color: #1e3a5f !important; + } + + .dark .hover\:bg-orange-50:hover { + background-color: #5c3d1e !important; + } + + .dark .hover\:bg-red-50:hover { + background-color: #5c1e1e !important; + } + + .dark .divide-gray-200 > :not([hidden]) ~ :not([hidden]) { + border-color: #404040 !important; + } + + .dark .shadow-sm { + box-shadow: 0 1px 2px 0 rgba(255, 255, 255, 0.05) !important; + } + + .dark .bg-gradient-to-r { + background-image: none !important; + } + + .dark .btn-primary { + background-color: #dc2626 !important; + } + + .dark .btn-primary:hover { + background-color: #b91c1c !important; + } + + /* Modal Dark Mode */ + .dark .fixed.inset-0 { + background-color: rgba(0, 0, 0, 0.8) !important; + } + + .dark .fixed.inset-0 > .bg-white { + background-color: #2d2d2d !important; + color: #e5e5e5 !important; + } + + .dark .text-gray-700 { + color: #b3b3b3 !important; + } + + /* Form elements dark mode */ + .dark input, .dark select, .dark textarea { + background-color: #3d3d3d !important; + border-color: #404040 !important; + color: #e5e5e5 !important; + } + + .dark input:focus, .dark select:focus, .dark textarea:focus { + border-color: #dc2626 !important; + } + + /* Sidebar dark mode */ + .dark .bg-gradient-to-b { + background-image: none !important; + background-color: #1f1f1f !important; + } + + .dark .sidebar-link { + color: #b3b3b3 !important; + } + + .dark .sidebar-link:hover { + color: #e5e5e5 !important; + background-color: #2d2d2d !important; + } + + .dark .sidebar-link.active { + color: #ffffff !important; + background-color: #dc2626 !important; + } + + .sidebar-link.active { + background-color: #dc2626 !important; + color: white !important; + } + + .sidebar-link:hover { + background-color: #f3f4f6 !important; + } + + /* Admin card dark mode */ + .dark .hover\:bg-gray-50:hover { + background-color: #2d2d2d !important; + } + + .dark .border-t.border-gray-100 { + border-color: #404040 !important; + } + .stat-card { transition: all 0.3s ease; } @@ -117,6 +269,11 @@Tomat yang belum matang sempurna, berwarna hijau dan tekstur masih keras.
@@ -102,7 +102,7 @@ class="rounded-lg shadow-md w-full h-auto">Tomat dalam proses pematangan, perpaduan warna hijau dan merah.
@@ -114,7 +114,7 @@ class="rounded-lg shadow-md w-full h-auto">Tomat matang sempurna, berwarna merah cerah dan tekstur lembut.
@@ -138,7 +138,7 @@ class="rounded-lg shadow-md w-full h-auto">Ambil foto tomat atau unggah gambar dari galeri perangkat Anda.
@@ -154,7 +154,7 @@ class="rounded-lg shadow-md w-full h-auto">Sistem AI kami menganalisis warna, tekstur, dan bentuk tomat secara otomatis.
@@ -170,7 +170,7 @@ class="rounded-lg shadow-md w-full h-auto">Lihat hasil klasifikasi kematangan tomat dengan akurasi tinggi.
@@ -208,16 +208,16 @@ class="rounded-lg shadow-md w-full h-auto">