import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:service/constant/colors.dart';
import 'package:service/controller/functions/encrypt_decrypt.dart';
import 'package:service/controller/mainController/main_controller.dart';
import 'package:service/views/widgets/my_scafold.dart';

import 'registration_captain_page.dart';

class DriversCantRegister extends StatelessWidget {
  DriversCantRegister({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(MainController());
    return MyScaffold(
      title: 'Drivers Cant Register'.tr,
      isleading: true,
      body: [
        GetBuilder<MainController>(builder: (mainController) {
          return Column(
            children: [
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: CupertinoSearchTextField(
                  keyboardType: TextInputType.phone,
                  onChanged: (value) => mainController.searchDrivers(value),
                  placeholder: 'Search by phone number'.tr,
                ),
              ),
              Expanded(
                child: ListView.builder(
                  itemCount: mainController.filteredDrivers.length,
                  itemBuilder: (context, index) {
                    final driver = mainController.filteredDrivers[index];
                    return Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Container(
                        color: driver['note'] == null
                            ? AppColor.greenColor
                            : AppColor.greyColor,
                        child: CupertinoFormSection(
                          header: Text(
                            'Driver ID: ${driver['driverId']}',
                          ),
                          children: [
                            InkWell(
                              onTap: () => mainController
                                  .makePhoneCall(driver['phone_number']),
                              child: Container(
                                  height: 40,
                                  color: driver['note'] != null
                                      ? AppColor.greenColor
                                      : AppColor.greyColor,
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceAround,
                                    children: [
                                      Text((driver['phone_number'])),
                                      IconButton(
                                        onPressed: () {
                                          String message = "مرحباً،\n\n"
                                              "نلاحظ أنك لم تكمل عملية التسجيل في خدمة Tripz درايفر. نود تذكيرك بأن إكمال التسجيل يتيح لك فرصة الانضمام إلى فريق Tripz والاستفادة من خدماتنا المتنوعة.\n\n"
                                              "إذا كنت بحاجة إلى أي مساعدة أو لديك أي استفسارات، لا تتردد في الاتصال بنا. نحن هنا لمساعدتك.\n\n"
                                              "للاتصال بنا، يرجى الاتصال على الرقم التالي: +20 101 880 5430\n\n"
                                              "مع تحيات فريق Tripz.";

                                          mainController.launchCommunication(
                                              'whatsapp',
                                              '${driver['phone_number']}',
                                              message);
                                        },
                                        icon: const Icon(
                                          Icons.send_time_extension_sharp,
                                          color: AppColor.secondaryColor,
                                        ),
                                      ),
                                      IconButton(
                                        onPressed: () {
                                          Get.to(() => RegisterCaptain(),
                                              arguments: {
                                                "phone_number":
                                                    driver['phone_number']
                                                        .toString(),
                                                'driverId': driver['driverId']
                                                    .toString(),
                                                'email':
                                                    driver['email'].toString(),
                                              });
                                        },
                                        icon: const Icon(
                                          Icons.save,
                                          color: AppColor.gold,
                                        ),
                                      ),
                                    ],
                                  )
                                  //  CupertinoFormRow(
                                  //   prefix: Text('Phone Number'.tr),
                                  //   child: CupertinoTextFormFieldRow(
                                  //     initialValue: driver['phone_number'],
                                  //     readOnly: true,
                                  //     placeholder: 'Phone Number'.tr,
                                  //   ),
                                  // ),
                                  ),
                            ),
                            CupertinoFormRow(
                              prefix: Text('Created At'.tr),
                              child: CupertinoTextFormFieldRow(
                                initialValue: driver['created_at'],
                                readOnly: true,
                                placeholder: 'Created At',
                              ),
                            ),
                            CupertinoFormRow(
                              prefix: Text('Status'.tr),
                              child: GestureDetector(
                                onTap: () {
                                  showCupertinoModalPopup<void>(
                                    context: Get.context!,
                                    builder: (BuildContext context) =>
                                        Container(
                                      height: 216,
                                      padding: const EdgeInsets.only(top: 6.0),
                                      margin: EdgeInsets.only(
                                        bottom: MediaQuery.of(context)
                                            .viewInsets
                                            .bottom,
                                      ),
                                      color: CupertinoColors.systemBackground
                                          .resolveFrom(context),
                                      child: SafeArea(
                                        top: false,
                                        child: CupertinoPicker(
                                          magnification: 1.22,
                                          squeeze: 1.2,
                                          useMagnifier: true,
                                          itemExtent: 32.0,
                                          scrollController:
                                              FixedExtentScrollController(
                                            initialItem: mainController
                                                .selectedStatus
                                                .indexOf(mainController
                                                    .selectedStatus),
                                          ),
                                          onSelectedItemChanged:
                                              (int selectedItem) {
                                            mainController.setSelectedStatus(
                                                mainController
                                                    .statusOptions[selectedItem]
                                                    .tr);
                                          },
                                          children: List<Widget>.generate(
                                              mainController.statusOptions
                                                  .length, (int index) {
                                            return Center(
                                              child: Text(mainController
                                                  .statusOptions[index].tr),
                                            );
                                          }),
                                        ),
                                      ),
                                    ),
                                  );
                                },
                                child: CupertinoFormRow(
                                  child: Text(
                                    mainController.selectedStatus.tr,
                                    style: TextStyle(
                                        color: CupertinoColors.label
                                            .resolveFrom(Get.context!)),
                                  ),
                                ),
                              ),
                            ),
                            CupertinoFormRow(
                              prefix: Text('Notes'.tr),
                              child: CupertinoTextFormFieldRow(
                                cursorColor: AppColor.blueColor,
                                controller: mainController.notesController,
                                placeholder:
                                    driver['note'] ?? "Additional comments".tr,
                              ),
                            ),
                            CupertinoButton(
                              child: Text('Save Notes'.tr),
                              onPressed: () {
                                // Save the notes for the driver
                                String notes =
                                    mainController.notesController.text == ''
                                        ? mainController.selectedStatus
                                            .toString()
                                        : mainController.notesController.text;

                                mainController
                                    .saveNoteForDriverNotCompleteRegistration(
                                        driver['phone_number'],
                                        'girls name',
                                        notes);
                                print(
                                    'Notes for driver ${driver['id']}: $notes');
                              },
                            ),
                          ],
                        ),
                      ),
                    );
                  },
                ),
              ),
            ],
          );
        }),
      ],
    );
  }
}
