import 'package:bookmywages/consts_widgets/app_colors.dart';
import 'package:bookmywages/model/booking_modify_model.dart';
import 'package:bookmywages/model/cancel_booking.dart';
import 'package:bookmywages/routers/consts_router.dart';
import 'package:bookmywages/viewmodel/api_controller.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

class ServiceBooking extends ConsumerStatefulWidget {
  const ServiceBooking({super.key});

  @override
  ConsumerState<ServiceBooking> createState() => _ServiceBookingState();
}

class _ServiceBookingState extends ConsumerState<ServiceBooking> {
  @override
  Widget build(BuildContext context) {
    final bookingAsyncValue = ref.watch(userbookinghistorydetailsProvider);

    return Scaffold(
      backgroundColor: AppColors.secondprimary,
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16),
        child: bookingAsyncValue.when(
          loading: () => const Center(child: CircularProgressIndicator()),
          error: (err, stack) => Center(child: Text('Error: $err')),
          data: (bookings) {
            return ListView.builder(
              itemCount: bookings.length,
              shrinkWrap: false,
              itemBuilder: (context, index) {
                final booking = bookings[index];

                return Container(
                  margin: const EdgeInsets.only(bottom: 10),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(20),
                    border: Border.all(color: AppColors.lightGrey),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      /// Top Row: ID and "View order"
                      Padding(
                        padding: const EdgeInsets.only(
                          right: 12,
                          left: 12,
                          top: 12,
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Text(
                              'ID : ${booking.id}',
                              style: const TextStyle(
                                fontWeight: FontWeight.bold,
                                fontSize: 16,
                              ),
                            ),
                            // Update the onTap method in the ServiceBooking class
                            GestureDetector(
                              onTap: () {
                                Get.toNamed(
                                  RouterConts.detailserivce,
                                  arguments: booking.serviceId,
                                );
                              },
                              child: Text(
                                'View order',
                                style: TextStyle(
                                  color: Colors.blue.shade600,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),

                      const Divider(),

                      /// Image, Company, and Status
                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 12),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Stack(
                              children: [
                                ClipRRect(
                                  borderRadius: BorderRadius.circular(12),
                                  child: (booking.images1.isNotEmpty)
                                      ? Image.network(
                                          booking.images1[0],
                                          width: 100,
                                          height: 110,
                                          fit: BoxFit.cover,
                                          loadingBuilder: (context, child, loadingProgress) {
                                            if (loadingProgress == null) {
                                              return child;
                                            }
                                            return Center(
                                              child: CircularProgressIndicator(
                                                value:
                                                    loadingProgress
                                                            .expectedTotalBytes !=
                                                        null
                                                    ? loadingProgress
                                                              .cumulativeBytesLoaded /
                                                          (loadingProgress
                                                                  .expectedTotalBytes ??
                                                              1)
                                                    : null,
                                              ),
                                            );
                                          },
                                          errorBuilder:
                                              (context, error, stackTrace) {
                                                return const Icon(
                                                  Icons.error,
                                                  size: 100,
                                                );
                                              },
                                        )
                                      : const Icon(
                                          Icons.image_not_supported,
                                          size: 100,
                                        ),
                                ),
                                Positioned(
                                  top: 6,
                                  left: 6,
                                  child: Container(
                                    padding: const EdgeInsets.symmetric(
                                      horizontal: 6,
                                      vertical: 2,
                                    ),
                                    decoration: BoxDecoration(
                                      color: Colors.white,
                                      borderRadius: BorderRadius.circular(8),
                                    ),
                                    child: Row(
                                      mainAxisSize: MainAxisSize.min,
                                      children: [
                                        Container(
                                          width: 6,
                                          height: 6,
                                          decoration: const BoxDecoration(
                                            color: Colors.green,
                                            shape: BoxShape.circle,
                                          ),
                                        ),
                                        const SizedBox(width: 4),
                                        const Text(
                                          'Live',
                                          style: TextStyle(
                                            fontSize: 12,
                                            color: Colors.green,
                                            fontWeight: FontWeight.bold,
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ],
                            ),
                            const SizedBox(width: 12),
                            Flexible(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    booking.vendorName ??
                                        'Arun Cleaning Company',
                                    style: const TextStyle(
                                      fontFamily: 'Gilroy-Bold',
                                      fontWeight: FontWeight.w700,
                                      fontSize: 16.11,
                                      height: 14.5 / 16.11,
                                      letterSpacing: 0.01 * 16.11,
                                    ),
                                  ),

                                  const SizedBox(height: 4),
                                  Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Expanded(
                                        child: Text(
                                          booking.serviceName ??
                                              'Cleaning service',
                                          style: const TextStyle(
                                            fontFamily: 'Gilroy-Medium',
                                            fontWeight: FontWeight.w500,
                                            fontSize: 13.91,
                                            height: 1.3,
                                            letterSpacing: 0.01 * 13.91,
                                            color: Color(0xFF5A5A5A),
                                          ),
                                        ),
                                      ),

                                      const SizedBox(width: 10),
                                      // Updated status display code in the ServiceBooking class
                                      Expanded(
                                        child: Container(
                                          width: booking.status == 3
                                              ? 70
                                              : booking.status == 1
                                              ? 85 // Width for Scheduled
                                              : 77.84, // Width for Pending and other statuses
                                          height: booking.status == 3
                                              ? 25
                                              : booking.status == 1
                                              ? 25 // Height for Scheduled
                                              : 27.96, // Height for Pending and other statuses
                                          decoration: BoxDecoration(
                                            color: booking.status == 3
                                                ? const Color(
                                                    0xFFFFEEEE,
                                                  ) // #FFEEEE for Cancel
                                                : booking.status == 1
                                                ? const Color(
                                                    0xFFE6F7E6,
                                                  ) // Light green for Scheduled
                                                : const Color(
                                                    0xFFDAE9FF,
                                                  ), // #DAE9FF for Pending
                                            borderRadius: BorderRadius.circular(
                                              6.05,
                                            ), // 6.05px border radius
                                          ),
                                          child: Center(
                                            child: Text(
                                              booking.status == 3
                                                  ? 'Cancel'
                                                  : booking.status == 1
                                                  ? 'Scheduled' // Text for status 1
                                                  : 'Pending', // Text for other statuses
                                              style: TextStyle(
                                                fontFamily: 'Gilroy-Bold',
                                                fontSize: 10.92,
                                                fontWeight: FontWeight.w400,
                                                color: booking.status == 3
                                                    ? const Color(
                                                        0xFFFF0000,
                                                      ) // Red color for Cancel
                                                    : booking.status == 1
                                                    ? const Color(
                                                        0xFF2E8B57,
                                                      ) // Green color for Scheduled
                                                    : const Color(
                                                        0xFF0066FF,
                                                      ), // Blue color for Pending
                                                letterSpacing: 1.0,
                                                height: 0.98,
                                              ),
                                            ),
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                  const SizedBox(height: 6),
                                  Container(
                                    padding: const EdgeInsets.all(8),
                                    decoration: BoxDecoration(
                                      border: Border.all(
                                        color: AppColors.lightGrey,
                                      ),
                                      color: Colors.white,
                                      borderRadius: BorderRadius.circular(12),
                                    ),
                                    child: Row(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.start,
                                      children: [
                                        CircleAvatar(
                                          radius: 15,
                                          backgroundImage:
                                              booking.profilePic != null &&
                                                  booking.profilePic!.isNotEmpty
                                              ? NetworkImage(
                                                  booking.profilePic.toString(),
                                                )
                                              : null,
                                          child:
                                              booking.profilePic == null ||
                                                  booking.profilePic!.isEmpty
                                              ? const Icon(
                                                  Icons.person,
                                                  size: 20,
                                                )
                                              : null,
                                        ),
                                        const SizedBox(width: 10),
                                        Expanded(
                                          child: Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            children: [
                                              Row(
                                                children: [
                                                  Expanded(
                                                    child: Text(
                                                      booking.vendorName ??
                                                          'no data',
                                                      style: const TextStyle(
                                                        fontFamily:
                                                            'Gilroy-Medium',
                                                        fontWeight:
                                                            FontWeight.w600,
                                                        fontSize: 11,
                                                        height: 10.65 / 11.84,
                                                        letterSpacing: 0,
                                                        color: Color(
                                                          0xFF353434,
                                                        ),
                                                      ),
                                                      overflow:
                                                          TextOverflow.visible,
                                                    ),
                                                  ),
                                                  const Icon(
                                                    Icons.star,
                                                    color: Colors.orange,
                                                    size: 15,
                                                  ),

                                                  const SizedBox(width: 4),
                                                  // Fixed: Changed to properly handle isRated as an integer
                                                  Text(
                                                    '${booking.isRated}',
                                                    textAlign: TextAlign.center,
                                                    style: const TextStyle(
                                                      fontFamily:
                                                          'SF UI Display',
                                                      fontWeight:
                                                          FontWeight.w800,
                                                      fontSize: 10,
                                                      height: 12.5 / 8.82,
                                                      letterSpacing: -0.31,
                                                    ),
                                                  ),
                                                ],
                                              ),
                                              SizedBox(height: 7),
                                              Text(
                                                booking.categoryName,
                                                style: const TextStyle(
                                                  fontFamily: 'Gilroy-Regular',
                                                  fontWeight: FontWeight.w400,
                                                  fontSize: 11.84,
                                                  height: 10.65 / 11.84,
                                                  letterSpacing: 0,
                                                  color: Color(0xFF717171),
                                                ),
                                                overflow: TextOverflow.ellipsis,
                                              ),
                                            ],
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                      const SizedBox(height: 16),

                      /// Date / Time / Hours
                      Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 12),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                const Text(
                                  'Date :',
                                  style: TextStyle(
                                    fontFamily: 'Gilroy-Bold',
                                    fontWeight: FontWeight.w700,
                                    fontSize: 14.45,
                                    height: 13 / 14.45,
                                    letterSpacing: 0,
                                  ),
                                ),

                                const SizedBox(height: 10),
                                Text(
                                  booking.serviceDate ?? 'April 23, 2024',
                                  style: const TextStyle(
                                    fontFamily: 'Gilroy-Medium',
                                    fontWeight: FontWeight.w400,
                                    fontSize: 14.45,
                                    height: 13 / 14.45,
                                    letterSpacing: 0,
                                  ),
                                ),
                              ],
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                const Text(
                                  'Time :',
                                  style: TextStyle(
                                    fontFamily: 'Gilroy-Bold',
                                    fontWeight: FontWeight.w700,
                                    fontSize: 14.45,
                                    height: 13 / 14.45,
                                    letterSpacing: 0,
                                  ),
                                ),
                                const SizedBox(height: 10),
                                Text(
                                  booking.serviceTime ?? '12:00 PM',
                                  style: const TextStyle(
                                    fontFamily: 'Gilroy-Medium',
                                    fontWeight: FontWeight.w400,
                                    fontSize: 14.45,
                                    height: 13 / 14.45,
                                    letterSpacing: 0,
                                  ),
                                ),
                              ],
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  'Working hours :',
                                  style: TextStyle(
                                    fontFamily: 'Gilroy-Bold',
                                    fontWeight: FontWeight.w700,
                                    fontSize: 14.45,
                                    height: 13 / 14.45,
                                    letterSpacing: 0,
                                  ),
                                ),
                                SizedBox(height: 10),
                                Text(
                                  booking.workingHours,
                                  style: const TextStyle(
                                    fontFamily: 'Gilroy-Medium',
                                    fontWeight: FontWeight.w400,
                                    fontSize: 14.45,
                                    height: 13 / 14.45,
                                    letterSpacing: 0,
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                      const SizedBox(height: 20),

                      /// Canceled Booking Button
                      /// Buttons Section - Modified for Scheduled Status
                      booking.status != 3
                          ? Padding(
                              padding: const EdgeInsets.symmetric(
                                horizontal: 12,
                                vertical: 10,
                              ),
                              child: Row(
                                mainAxisAlignment: booking.status == 1
                                    ? MainAxisAlignment
                                          .end // Align to the right for Scheduled status
                                    : MainAxisAlignment
                                          .spaceBetween, // Normal alignment for other statuses
                                children: [
                                  // Only show Modification button if status is NOT Scheduled (status 1)
                                  booking.status != 1
                                      ? Expanded(
                                          child: SizedBox(
                                            height: 42,
                                            child: ElevatedButton(
                                              onPressed: () {
                                                showDialog(
                                                  context: context,
                                                  barrierDismissible: false,
                                                  builder: (context) => Dialog(
                                                    shape: RoundedRectangleBorder(
                                                      borderRadius:
                                                          BorderRadius.circular(
                                                            20,
                                                          ),
                                                    ),
                                                    child: Container(
                                                      width: double.maxFinite,
                                                      decoration: BoxDecoration(
                                                        color: AppColors
                                                            .secondprimary,
                                                        borderRadius:
                                                            BorderRadius.circular(
                                                              16,
                                                            ),
                                                      ),
                                                      child:
                                                          BookingModificationDialog(
                                                            id: booking.id
                                                                .toString(),
                                                            serviceId: booking
                                                                .serviceId
                                                                .toString(),
                                                          ),
                                                    ),
                                                  ),
                                                );
                                              },
                                              style: ElevatedButton.styleFrom(
                                                backgroundColor: const Color(
                                                  0xFF0066FF,
                                                ),
                                                shape: RoundedRectangleBorder(
                                                  borderRadius:
                                                      BorderRadius.circular(20),
                                                ),
                                              ),
                                              child: const FittedBox(
                                                child: Text(
                                                  "Modification",
                                                  style: TextStyle(
                                                    fontFamily: 'Gilroy-Bold',
                                                    fontWeight: FontWeight.w800,
                                                    fontSize: 14.45,
                                                    height: 13 / 14.45,
                                                    letterSpacing: 0.289,
                                                    color: Colors.white,
                                                  ),
                                                ),
                                              ),
                                            ),
                                          ),
                                        )
                                      : const SizedBox.shrink(), // Empty space when Scheduled

                                  booking.status != 1
                                      ? const SizedBox(width: 8)
                                      : const SizedBox.shrink(),

                                  // Cancel Button - Always visible
                                  GestureDetector(
                                    onTap: () async {
                                      final data = CancelBookingRequest(
                                        id: booking.id.toString(),
                                        serviceId: booking.serviceId.toString(),
                                        type: booking.type == 0
                                            ? "0"
                                            : booking.type.toString(),
                                      );

                                      try {
                                        // Call the cancel API
                                        await ref.read(
                                          cancelbookingProvider(data).future,
                                        );

                                        // Show success message
                                        Fluttertoast.showToast(
                                          msg: 'Booking cancelled successfully',
                                          toastLength: Toast.LENGTH_SHORT,
                                          gravity: ToastGravity.BOTTOM,
                                          backgroundColor: Colors.green,
                                          textColor: Colors.white,
                                        );

                                        // Refresh the booking history data
                                        ref.invalidate(
                                          userbookinghistorydetailsProvider,
                                        );
                                      } catch (e) {
                                        // Show error message
                                        Fluttertoast.showToast(
                                          msg: 'Failed to cancel booking: $e',
                                          toastLength: Toast.LENGTH_LONG,
                                          gravity: ToastGravity.BOTTOM,
                                          backgroundColor: Colors.red,
                                          textColor: Colors.white,
                                        );
                                      }
                                    },

                                    child: Container(
                                      padding: const EdgeInsets.symmetric(
                                        horizontal: 12,
                                        vertical: 8,
                                      ),
                                      decoration: BoxDecoration(
                                        color: Colors.red.shade50,
                                        borderRadius: BorderRadius.circular(20),
                                      ),
                                      child: const Text(
                                        'Cancel Booking',
                                        style: TextStyle(
                                          color: Colors.red,
                                          fontWeight: FontWeight.bold,
                                        ),
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            )
                          : const SizedBox.shrink(),
                      const SizedBox(height: 20),
                    ],
                  ),
                );
              },
            );
          },
        ),
      ),
    );
  }

  // Updated _loadBookingImage and related methods to handle images properly

  // Check if URL is likely to cause a 500 error
}

class BookingModificationDialog extends ConsumerStatefulWidget {
  final String id;
  final String serviceId;

  const BookingModificationDialog({
    super.key,
    required this.id,
    required this.serviceId,
  });

  @override
  _BookingModificationDialogState createState() =>
      _BookingModificationDialogState();
}

class _BookingModificationDialogState
    extends ConsumerState<BookingModificationDialog> {
  int selectedDateIndex = 0;
  int selectedTimeIndex = 0;

  DateTime currentMonth = DateTime.now();
  late List<DateTime> dateList;
  List<String> times = [];

  @override
  void initState() {
    super.initState();
    _generateDateList();
    _generateTimes();
  }

  void _generateDateList() {
    final firstDay = DateTime(currentMonth.year, currentMonth.month, 1);
    final lastDay = DateTime(currentMonth.year, currentMonth.month + 1, 0);

    dateList = List.generate(
      lastDay.day,
      (index) => firstDay.add(Duration(days: index)),
    );
    selectedDateIndex = 0;
  }

  void _generateTimes() {
    times = [];
    for (int hour = 0; hour < 24; hour++) {
      for (int minute = 0; minute < 60; minute += 30) {
        final time = DateTime(0, 0, 0, hour, minute);
        times.add(DateFormat('h:mm a').format(time));
      }
    }
  }

  void _goToPreviousMonth() {
    final newMonth = DateTime(currentMonth.year, currentMonth.month - 1);
    if (newMonth.isBefore(
      DateTime(DateTime.now().year, DateTime.now().month, 1),
    )) {
      return;
    }

    setState(() {
      currentMonth = newMonth;
      _generateDateList();
    });
  }

  void _goToNextMonth() {
    setState(() {
      currentMonth = DateTime(currentMonth.year, currentMonth.month + 1);
      _generateDateList();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(
        bottom: MediaQuery.of(context).viewInsets.bottom,
        top: 20,
        left: 20,
        right: 20,
      ),
      child: SingleChildScrollView(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              'Booking Modification',
              style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 20),

            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  'Change Date',
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                ),
                Icon(Icons.calendar_today),
              ],
            ),
            SizedBox(height: 10),

            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                IconButton(
                  onPressed: _goToPreviousMonth,
                  icon: Icon(Icons.arrow_back_ios),
                ),
                Text(
                  DateFormat('MMMM yyyy').format(currentMonth),
                  style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                ),
                IconButton(
                  onPressed: _goToNextMonth,
                  icon: Icon(Icons.arrow_forward_ios),
                ),
              ],
            ),
            SizedBox(height: 10),

            SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: Row(
                children: List.generate(dateList.length, (index) {
                  final date = dateList[index];
                  final dayLabel =
                      '${DateFormat('E').format(date)}\n${DateFormat('d').format(date)}';
                  final isSelected = selectedDateIndex == index;

                  return GestureDetector(
                    onTap: () {
                      setState(() {
                        selectedDateIndex = index;
                      });
                    },
                    child: Container(
                      margin: EdgeInsets.symmetric(horizontal: 5),
                      padding: EdgeInsets.all(10),
                      decoration: BoxDecoration(
                        color: isSelected ? Colors.blue : Colors.grey.shade200,
                        borderRadius: BorderRadius.circular(20),
                      ),
                      child: Text(
                        dayLabel,
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          color: isSelected ? Colors.white : Colors.black,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  );
                }),
              ),
            ),
            SizedBox(height: 20),

            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  'Change Time',
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                ),
                Icon(Icons.access_time),
              ],
            ),
            SizedBox(height: 10),

            SingleChildScrollView(
              scrollDirection: Axis.horizontal,
              child: Row(
                children: List.generate(times.length, (index) {
                  final isSelected = selectedTimeIndex == index;
                  return GestureDetector(
                    onTap: () {
                      setState(() {
                        selectedTimeIndex = index;
                      });
                    },
                    child: Container(
                      margin: EdgeInsets.symmetric(horizontal: 5),
                      padding: EdgeInsets.symmetric(
                        horizontal: 20,
                        vertical: 10,
                      ),
                      decoration: BoxDecoration(
                        color: isSelected ? Colors.blue : Colors.grey.shade200,
                        borderRadius: BorderRadius.circular(30),
                      ),
                      child: Text(
                        times[index],
                        style: TextStyle(
                          color: isSelected ? Colors.white : Colors.black,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  );
                }),
              ),
            ),
            SizedBox(height: 20),

            // GestureDetector(
            //   onTap: () {
            //     Navigator.pop(context);
            //   },
            //   child: Container(
            //     width: double.infinity,
            //     padding: EdgeInsets.all(15),
            //     decoration: BoxDecoration(
            //       color: Colors.red.shade50,
            //       borderRadius: BorderRadius.circular(15),
            //     ),
            //     child: Center(
            //       child: Text(
            //         'Cancel the Booking',
            //         style: TextStyle(
            //           color: Colors.red,
            //           fontWeight: FontWeight.bold,
            //           fontSize: 16,
            //         ),
            //       ),
            //     ),
            //   ),
            // ),
            SizedBox(height: 20),

            ElevatedButton(
              onPressed: () async {
                final selectedDate = DateFormat(
                  'yyyy-MM-dd',
                ).format(dateList[selectedDateIndex]);
                final selectedTime = times[selectedTimeIndex];

                final model = BookingModifyModel(
                  id: widget.id,
                  serviceId: widget.serviceId,
                  servicedate: selectedDate,
                  servicetime: selectedTime,
                );

                final result = await ref
                    .read(modifybookingProvider(model).future)
                    .catchError((e) {
                      Fluttertoast.showToast(
                        msg: 'Error: ${e.toString()}',
                        toastLength: Toast.LENGTH_LONG,
                        gravity: ToastGravity.BOTTOM,
                        backgroundColor: Colors.red,
                        textColor: Colors.white,
                      );
                      return false;
                    });

                if (result) {
                  Fluttertoast.showToast(
                    msg: 'Booking modified successfully',
                    toastLength: Toast.LENGTH_SHORT,
                    gravity: ToastGravity.BOTTOM,
                    backgroundColor: Colors.green,
                    textColor: Colors.white,
                  );
                  Navigator.pop(context);
                  ref.invalidate(userbookinghistorydetailsProvider);
                } else {
                  Fluttertoast.showToast(
                    msg: 'Failed to modify booking',
                    toastLength: Toast.LENGTH_SHORT,
                    gravity: ToastGravity.BOTTOM,
                    backgroundColor: Colors.red,
                    textColor: Colors.white,
                  );
                }
              },

              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.blue,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(30),
                ),
                padding: EdgeInsets.symmetric(horizontal: 100, vertical: 15),
              ),
              child: Text('Save', style: TextStyle(fontSize: 18)),
            ),
            SizedBox(height: 20),
          ],
        ),
      ),
    );
  }
}
