import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'package:intl/intl.dart'; import 'package:telemednet/data/models/consultation_center.dart'; import 'package:telemednet/data/models/doctor.dart'; import 'package:telemednet/data/services/consultation_booking_service.dart'; import 'package:telemednet/data/services/patient_registration_service.dart'; import 'package:telemednet/widgets/alert_screen.dart'; class ConsultationBookingScreen extends StatelessWidget { final Doctor doctor; final ConsultationCenter selectedConsultation; final DateTime selectedDate; final String selectedTime; const ConsultationBookingScreen({ super.key, required this.doctor, required this.selectedConsultation, required this.selectedDate, required this.selectedTime, }); String get formattedAddress { final parts = [ selectedConsultation.floorBuilding, selectedConsultation.street, selectedConsultation.city, selectedConsultation.state, selectedConsultation.postalCode ].where((part) => part != null && part.isNotEmpty).toList(); return parts.join(', '); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: const Color(0xFFF5F7FF), appBar: _buildAppBar(context), body: SingleChildScrollView( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ _buildAppointmentCard(), const SizedBox(height: 24), _buildDoctorDetails(), const SizedBox(height: 24), _buildLocationDetails(), const SizedBox(height: 24), _buildPaymentDetails(), const SizedBox(height: 24), _buildConfirmButton(context), ], ), ), ), ); } PreferredSizeWidget _buildAppBar(BuildContext context) { return AppBar( backgroundColor: Colors.white, elevation: 0, leading: IconButton( icon: const Icon(Icons.arrow_back, color: Colors.black87), onPressed: () => Navigator.pop(context), ), title: Text( 'Booking Overview', style: GoogleFonts.poppins( color: Colors.black87, fontWeight: FontWeight.w600, fontSize: 20, ), ), centerTitle: true, ); } Widget _buildAppointmentCard() { return Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.circular(16), boxShadow: [ BoxShadow( color: Colors.blue.withOpacity(0.3), blurRadius: 10, offset: const Offset(0, 2), ), ], ), child: Column( children: [ Row( children: [ const Icon(Icons.calendar_today, color: Colors.white), const SizedBox(width: 12), Text( DateFormat('EEEE, MMMM d').format(selectedDate), style: GoogleFonts.poppins( color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500, ), ), ], ), const SizedBox(height: 12), Row( children: [ const Icon(Icons.access_time, color: Colors.white), const SizedBox(width: 12), Text( selectedTime, style: GoogleFonts.poppins( color: Colors.white, fontSize: 16, fontWeight: FontWeight.w500, ), ), ], ), ], ), ); } Widget _buildDoctorDetails() { return Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(16), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.1), blurRadius: 10, offset: const Offset(0, 2), ), ], ), child: Row( children: [ ClipRRect( borderRadius: BorderRadius.circular(12), child: Image.asset( doctor.profileImage!, width: 80, height: 80, fit: BoxFit.cover, errorBuilder: (context, error, stackTrace) { return Container( width: 80, height: 80, color: Colors.grey[300], child: Icon(Icons.person, size: 40, color: Colors.grey[600]), ); }, ), ), const SizedBox(width: 16), Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( doctor.lastName!, style: GoogleFonts.poppins( fontSize: 18, fontWeight: FontWeight.w600, ), ), Text( doctor.speciality!, style: GoogleFonts.poppins( fontSize: 14, color: Colors.grey[600], ), ), Text( '${doctor.yearsOfExperience} years experience', style: GoogleFonts.poppins( fontSize: 14, color: Colors.grey[600], ), ), ], ), ), ], ), ); } Widget _buildLocationDetails() { return Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(16), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.1), blurRadius: 10, offset: const Offset(0, 2), ), ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'Location', style: GoogleFonts.poppins( fontSize: 16, fontWeight: FontWeight.w600, ), ), const SizedBox(height: 8), Text( formattedAddress, style: GoogleFonts.poppins( fontSize: 14, color: Colors.grey[600], ), ), const SizedBox(height: 8), Text( 'Average consultation time: ${selectedConsultation.averageDurationMinutes} minutes', style: GoogleFonts.poppins( fontSize: 14, color: Colors.grey[600], ), ), ], ), ); } Widget _buildPaymentDetails() { return Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.circular(16), boxShadow: [ BoxShadow( color: Colors.grey.withOpacity(0.1), blurRadius: 10, offset: const Offset(0, 2), ), ], ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'Payment Details', style: GoogleFonts.poppins( fontSize: 16, fontWeight: FontWeight.w600, ), ), const SizedBox(height: 16), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( 'Consultation Fee', style: GoogleFonts.poppins( fontSize: 14, color: Colors.grey[600], ), ), Text( '₹${selectedConsultation.consultationFee ?? "500"}', style: GoogleFonts.poppins( fontSize: 14, fontWeight: FontWeight.w500, ), ), ], ), ], ), ); } Widget _buildConfirmButton(BuildContext context) { return SizedBox( width: double.infinity, child: ElevatedButton( onPressed: () { // Handle payment and booking confirmation _showConfirmationDialog(context); }, style: ElevatedButton.styleFrom( backgroundColor: Colors.blue, padding: const EdgeInsets.symmetric(vertical: 16), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(12), ), ), child: Text( 'Confirm & Pay', style: GoogleFonts.poppins( fontSize: 16, fontWeight: FontWeight.w600, color: Colors.white, ), ), ), ); } void _showConfirmationDialog(BuildContext context) async { final bookingService = BookingService(); final currentUser = FirebaseAuth.instance.currentUser; final patientProfile = await PatientProfileService.getPatientProfile(); if (patientProfile == null) { return; } try { if (context.mounted) { showDialog( context: context, barrierDismissible: false, builder: (context) => const Center( child: CircularProgressIndicator(), ), ); } final bookingId = await bookingService.createBooking( doctorName: doctor.lastName!, patientId: currentUser!.uid, patientName: patientProfile.name ?? 'Patient', location: formattedAddress, appointmentDate: selectedDate, appointmentTime: selectedTime, consultationFee: int.parse(selectedConsultation.consultationFee ?? "500"), specialization: doctor.speciality!, ); if (context.mounted) { Navigator.pop(context); Navigator.push( context, MaterialPageRoute( builder: (context) => AlertScreen( arguments: AlertArguments( title: 'Booking Confirmed', message: 'Your appointment has been successfully booked. Booking ID: ${bookingId.substring(0, 8)}\n\nPlease complete the payment to confirm your appointment.', actionTitle: 'View Appointments', type: AlertType.success, onActionPressed: () { Navigator.of(context).popUntil((route) => route.isFirst); }, ), ), ), ); } } catch (e) { if (context.mounted) { Navigator.pop(context); Navigator.push( context, MaterialPageRoute( builder: (context) => AlertScreen( arguments: AlertArguments( title: 'Booking Failed', message: 'Unable to create booking. ${e.toString()}', actionTitle: 'Try Again', type: AlertType.error, onActionPressed: () { Navigator.of(context).pop(); }, ), ), ), ); } } } }