import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:Intaleq/constant/colors.dart';
import 'package:Intaleq/constant/style.dart';
import 'package:Intaleq/controller/home/map_passenger_controller.dart';
import '../../widgets/elevated_btn.dart';

// دالة لإظهار الشيت
void showCancelRideBottomSheet() {
  Get.bottomSheet(
    const CancelRidePageWidget(),
    backgroundColor: Colors.transparent,
    isScrollControlled: true,
  );
}

// الويدجت مفصولة لترتيب الكود
class CancelRidePageWidget extends StatelessWidget {
  const CancelRidePageWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // تأكد من وجود الكنترولر
    final controller = Get.find<MapPassengerController>();

    final List<String> reasons = [
      "Changed my mind".tr,
      "Found another transport".tr,
      "Driver is taking too long".tr,
      "Driver asked me to cancel".tr,
      "Wrong pickup location".tr,
      "Other".tr,
    ];

    return Container(
      height: Get.height * 0.7, // ارتفاع مناسب
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 15),
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.vertical(top: Radius.circular(25)),
      ),
      child: GetBuilder<MapPassengerController>(
        builder: (controller) => Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            // مؤشر السحب
            Center(
              child: Container(
                width: 50,
                height: 5,
                decoration: BoxDecoration(
                  color: Colors.grey[300],
                  borderRadius: BorderRadius.circular(10),
                ),
              ),
            ),
            const SizedBox(height: 20),

            Text(
              'Why do you want to cancel?'.tr,
              style: AppStyle.title
                  .copyWith(fontSize: 18, fontWeight: FontWeight.bold),
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 10),

            Expanded(
              child: ListView.separated(
                itemCount: reasons.length,
                separatorBuilder: (context, index) => const Divider(height: 1),
                itemBuilder: (context, index) {
                  bool isSelected = controller.selectedReasonIndex == index;
                  return Column(
                    children: [
                      ListTile(
                        contentPadding: EdgeInsets.zero,
                        title: Text(
                          reasons[index],
                          style: TextStyle(
                              fontWeight: isSelected
                                  ? FontWeight.bold
                                  : FontWeight.normal,
                              color: isSelected
                                  ? AppColor.primaryColor
                                  : Colors.black87,
                              fontSize: 15),
                        ),
                        trailing: isSelected
                            ? Icon(Icons.radio_button_checked,
                                color: AppColor.primaryColor)
                            : Icon(Icons.radio_button_off, color: Colors.grey),
                        onTap: () {
                          controller.selectReason(index, reasons[index]);
                        },
                      ),

                      // إظهار حقل النص فقط عند اختيار "أخرى"
                      if (isSelected && reasons[index] == "Other".tr)
                        Padding(
                          padding: const EdgeInsets.only(
                              bottom: 10, left: 10, right: 10),
                          child: TextField(
                            controller: controller.otherReasonController,
                            decoration: InputDecoration(
                              hintText: "Please write the reason...".tr,
                              filled: true,
                              fillColor: Colors.grey[100],
                              border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(10),
                                borderSide: BorderSide.none,
                              ),
                              contentPadding: const EdgeInsets.symmetric(
                                  horizontal: 15, vertical: 12),
                            ),
                            maxLines: 2,
                          ),
                        )
                    ],
                  );
                },
              ),
            ),

            const SizedBox(height: 20),

            // زر التأكيد
            SizedBox(
              height: 50,
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: AppColor.redColor,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(12)),
                  elevation: 0,
                ),
                onPressed: () => controller.cancelRide(),
                child: Text(
                  'Confirm Cancellation'.tr,
                  style: const TextStyle(
                      color: Colors.white,
                      fontWeight: FontWeight.bold,
                      fontSize: 16),
                ),
              ),
            ),
            const SizedBox(height: 10),
            // زر التراجع
            Center(
              child: TextButton(
                onPressed: () => Get.back(),
                child: Text("Don't Cancel".tr,
                    style: TextStyle(color: Colors.grey[600])),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
