view('auth/login', [ 'title' => 'เข้าสู่ระบบ | ' . APP_NAME ]); } public function login() { $username = $_POST['username'] ?? ''; $password = $_POST['password'] ?? ''; if (empty($username) || empty($password)) { $_SESSION['error'] = 'กรุณากรอกข้อมูลให้ครบถ้วน'; header('Location: ' . BASE_URL . '/login'); exit; } $userModel = new UserModel(); $user = $userModel->getUserByUsername($username); if ($user && password_verify($password, $user['password_hash'])) { // Login success $_SESSION['user_id'] = $user['id']; $_SESSION['username'] = $user['username']; $_SESSION['role_id'] = $user['role_id']; $_SESSION['first_name'] = $user['first_name']; $_SESSION['last_name'] = $user['last_name']; $userModel->updateLastLogin($user['id']); // Record Login Log $logModel = new UserLogModel(); $logModel->logAction($user['id'], 'LOGIN', 'เข้าสู่ระบบสำเร็จ'); $_SESSION['success'] = 'เข้าสู่ระบบสำเร็จ ยินดีต้อนรับคุณ ' . htmlspecialchars($user['first_name']); header('Location: ' . BASE_URL . '/dashboard'); exit; } else { // Login failed $_SESSION['error'] = 'ชื่อผู้ใช้งานหรือรหัสผ่านไม่ถูกต้อง'; header('Location: ' . BASE_URL . '/login'); exit; } } public function logout() { session_destroy(); session_start(); // Start a new session for the success message $_SESSION['success'] = 'ออกจากระบบสำเร็จ'; header('Location: ' . BASE_URL . '/login'); exit; } } ?>