118 lines
3.9 KiB
PHP
118 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Booking;
|
|
use App\Models\User;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
class AuthController extends Controller
|
|
{
|
|
public function dashboardCustomer()
|
|
{
|
|
$user = Auth::user(); // bisa null kalau belum login
|
|
|
|
// Ambil booking milik user jika login, kalau tidak tampilkan pesan umum
|
|
$bookings = Booking::when($user && $user->role === 'customer', function ($query) use ($user) {
|
|
return $query->where('customer_id', $user->id);
|
|
})
|
|
->orderBy('tanggal_pemotretan', 'desc')
|
|
->get();
|
|
|
|
return view('dashboard.customer', compact('user', 'bookings'));
|
|
}
|
|
|
|
public function showLoginForm()
|
|
{
|
|
return view('auth.login');
|
|
}
|
|
|
|
public function login(Request $request)
|
|
{
|
|
$credentials = $request->only('email', 'password');
|
|
|
|
if (Auth::attempt($credentials)) {
|
|
$request->session()->regenerate(); // Pastikan sesi diregenerasi
|
|
return redirect()->route('dashboard');
|
|
}
|
|
|
|
return back()->withErrors(['email' => 'Email atau password salah'])->withInput();
|
|
}
|
|
|
|
public function logout()
|
|
{
|
|
Auth::logout();
|
|
return redirect('/login');
|
|
}
|
|
|
|
public function dashboard()
|
|
{
|
|
$user = Auth::user();
|
|
|
|
if (!$user) {
|
|
// Pengecekan login untuk admin dan user
|
|
return redirect()->route('login')->with('error', 'Silakan login terlebih dahulu.');
|
|
}
|
|
|
|
switch ($user->role) {
|
|
case 'admin':
|
|
$totalBookings = Booking::count();
|
|
$pendingBookings = Booking::where('status', 'pending')->count();
|
|
$approvedBookings = Booking::where('status', 'approved')->count();
|
|
$declinedBookings = Booking::where('status', 'declined')->count();
|
|
$totalCustomers = User::where('role', 'customer')->count();
|
|
$totalEmployees = User::where('role', 'user')->count();
|
|
|
|
return view('dashboard.admin', [
|
|
'user' => $user,
|
|
'totalBookings' => $totalBookings ?? 0,
|
|
'pendingBookings' => $pendingBookings ?? 0,
|
|
'approvedBookings' => $approvedBookings ?? 0,
|
|
'declinedBookings' => $declinedBookings ?? 0,
|
|
'totalCustomers' => $totalCustomers ?? 0,
|
|
'totalEmployees' => $totalEmployees ?? 0
|
|
]);
|
|
|
|
case 'user':
|
|
return view('dashboard.user', compact('user'));
|
|
|
|
case 'customer':
|
|
// Mengabaikan login untuk customer
|
|
$bookings = Booking::where('customer_id', $user->id)->orderBy('tanggal_pemotretan', 'desc')->get();
|
|
return view('dashboard.customer', compact('user', 'bookings'));
|
|
|
|
default:
|
|
return redirect()->route('login')->with('error', 'Akses tidak valid.');
|
|
}
|
|
}
|
|
|
|
|
|
public function registerCustomer(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'email' => 'required|email|unique:users,email',
|
|
'phone' => 'required|regex:/^[0-9]+$/|min:10',
|
|
'address' => 'required|string',
|
|
'password' => 'required|string|min:6',
|
|
]);
|
|
|
|
$user = User::create([
|
|
'name' => $validated['name'],
|
|
'email' => $validated['email'],
|
|
'phone' => $validated['phone'],
|
|
'address' => $validated['address'],
|
|
'password' => Hash::make($validated['password']),
|
|
'role' => 'customer' // kalau kamu pakai role
|
|
]);
|
|
|
|
auth()->login($user);
|
|
|
|
return redirect()->route('login'); // atau kemana kamu mau redirect
|
|
}
|
|
|
|
|
|
}
|