requireRole('doctor')) { return $r; } $db = \Config\Database::connect(); $userId = (int) session()->get('id'); $doctorModel = new DoctorModel(); $doctor = $doctorModel->where('user_id', $userId)->first(); if (! $doctor) { return redirect()->to(site_url('/'))->with('error', 'Doctor profile not found.'); } $doctorId = (int) $doctor['id']; $query = $db->query(' SELECT a.*, u.name as patient_name FROM appointments a JOIN patients p ON p.id = a.patient_id JOIN users u ON u.id = p.user_id WHERE a.doctor_id = ? ', [$doctorId]); $data['appointments'] = $query->getResult(); return view('doctor/dashboard', $data); } public function profile() { if ($r = $this->requireRole('doctor')) { return $r; } $doctorModel = new DoctorModel(); $userId = (int) session()->get('id'); $doctor = $doctorModel->where('user_id', $userId)->first(); if (! $doctor) { return redirect()->to(site_url('/'))->with('error', 'Doctor profile not found.'); } if ($this->request->is('post')) { $rules = [ 'specialization' => 'required|min_length[2]|max_length[191]', 'experience' => 'permit_empty|max_length[100]', 'fees' => 'permit_empty|decimal', 'available_from' => 'permit_empty', 'available_to' => 'permit_empty', ]; if (! $this->validate($rules)) { return redirect()->back()->withInput(); } $update = [ 'specialization' => $this->request->getPost('specialization'), 'experience' => $this->request->getPost('experience') ?: null, 'fees' => $this->request->getPost('fees') !== '' && $this->request->getPost('fees') !== null ? $this->request->getPost('fees') : null, 'available_from' => $this->request->getPost('available_from') ?: null, 'available_to' => $this->request->getPost('available_to') ?: null, ]; if (! $doctorModel->update($doctor['id'], $update)) { return redirect()->back()->withInput()->with('error', 'Could not update profile.'); } return redirect()->to(site_url('doctor/profile'))->with('success', 'Profile updated.'); } return view('doctor/profile', ['doctor' => $doctor]); } public function accept($id): RedirectResponse { if ($r = $this->requireRole('doctor')) { return $r; } if (! $this->request->is('post')) { return redirect()->to(site_url('doctor/dashboard'))->with('error', 'Invalid request.'); } return $this->updateAppointmentStatus((int) $id, 'approved'); } public function reject($id): RedirectResponse { if ($r = $this->requireRole('doctor')) { return $r; } if (! $this->request->is('post')) { return redirect()->to(site_url('doctor/dashboard'))->with('error', 'Invalid request.'); } return $this->updateAppointmentStatus((int) $id, 'rejected'); } private function updateAppointmentStatus(int $appointmentId, string $status): RedirectResponse { if ($appointmentId < 1) { return redirect()->back(); } $appointmentModel = new AppointmentModel(); $doctorModel = new DoctorModel(); $userId = (int) session()->get('id'); $doctor = $doctorModel->where('user_id', $userId)->first(); if (! $doctor) { return redirect()->to(site_url('/'))->with('error', 'Doctor profile not found.'); } $appointment = $appointmentModel->find($appointmentId); if (! $appointment || (int) $appointment['doctor_id'] !== (int) $doctor['id']) { return redirect()->back()->with('error', 'Invalid appointment.'); } $appointmentModel->update($appointmentId, ['status' => $status]); return redirect()->back()->with('success', 'Appointment updated.'); } }