import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:SEFER/views/admin/captain/captain_details.dart';
import 'package:SEFER/views/widgets/my_scafold.dart';

import '../../../constant/colors.dart';
import '../../../constant/style.dart';
import '../../../controller/admin/captain_admin_controller.dart';
import '../../widgets/elevated_btn.dart';
import '../../widgets/my_textField.dart';
import '../../widgets/mycircular.dart';
import 'form_captain.dart';

class Captain extends StatelessWidget {
  Captain({super.key});
  final CaptainAdminController captainAdminController =
      Get.put(CaptainAdminController());
  @override
  Widget build(BuildContext context) {
    return MyScafolld(
      title: 'Captain'.tr,
      body: [
        GetBuilder<CaptainAdminController>(
            builder: (captainAdminController) => Column(
                  children: [
                    captainAdminController.isLoading
                        ? const MyCircularProgressIndicator()
                        : Column(
                            children: [
                              Padding(
                                padding: const EdgeInsets.all(5),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    captainAdmin(
                                      captainAdminController,
                                      'Captains Count',
                                      'countPassenger',
                                    ),
                                    MyElevatedButton(
                                        title: 'Add Prize to Gold Captains',
                                        onPressed: () {
                                          var date = DateTime.now();
                                          var day = date.weekday;

                                          if (day == 6) {
                                            // Saturday is 6
                                            // print(day);
                                            Get.defaultDialog(
                                              title:
                                                  'Add Prize to Gold Captains',
                                              titleStyle: AppStyle.title,
                                              content: Column(
                                                children: [
                                                  Text(
                                                    'Add Points to their wallet as prize'
                                                        .tr,
                                                    style: AppStyle.title,
                                                  ),
                                                  Form(
                                                      key: captainAdminController
                                                          .formCaptainPrizeKey,
                                                      child: MyTextForm(
                                                          controller:
                                                              captainAdminController
                                                                  .captainPrizeController,
                                                          label:
                                                              'Count of prize'
                                                                  .tr,
                                                          hint: 'Count of prize'
                                                              .tr,
                                                          type: TextInputType
                                                              .number))
                                                ],
                                              ),
                                              confirm: MyElevatedButton(
                                                title: 'Add',
                                                onPressed: () async {
                                                  if (captainAdminController
                                                      .formCaptainPrizeKey
                                                      .currentState!
                                                      .validate()) {
                                                    captainAdminController
                                                        .addCaptainsPrizeToWalletSecure();
                                                  }
                                                },
                                              ),
                                            );
                                          } else {
                                            Get.defaultDialog(
                                                title:
                                                    'This day is not allowed',
                                                titleStyle: AppStyle.title,
                                                middleText:
                                                    'Saturday only Allowed day',
                                                middleTextStyle: AppStyle.title,
                                                confirm: MyElevatedButton(
                                                    title: 'Ok'.tr,
                                                    onPressed: () {
                                                      Get.back();
                                                    }));
                                          }
                                        })
                                  ],
                                ),
                              ),
                              const SizedBox(
                                height: 10,
                              ),
                              InkWell(
                                onTap: () {
                                  //todo search
                                },
                                child: Padding(
                                  padding: const EdgeInsets.all(3),
                                  child: Container(
                                      width: Get.width,
                                      height: 110,
                                      decoration: BoxDecoration(
                                          border: Border.all(
                                              width: 2,
                                              color: AppColor.greenColor)),
                                      child: formSearchCaptain()
                                      //   ],
                                      // ),
                                      ),
                                ),
                              ),
                              SizedBox(
                                height: Get.height * .5,
                                child: ListView.builder(
                                  itemCount: captainAdminController
                                      .captainData['message'].length,
                                  itemBuilder: (context, index) {
                                    final user = captainAdminController
                                        .captainData['message'][index];

                                    return InkWell(
                                      onTap: () {
                                        Get.to(const CaptainsDetailsPage(),
                                            arguments: {
                                              'data': user,
                                            });
                                      },
                                      child: Padding(
                                        padding: const EdgeInsets.all(3),
                                        child: Container(
                                          decoration: BoxDecoration(
                                              border: Border.all(width: 2)),
                                          child: ListTile(
                                            title: Row(
                                              mainAxisAlignment:
                                                  MainAxisAlignment
                                                      .spaceBetween,
                                              children: [
                                                Text(
                                                  'Name : ${user['first_name']} ${user['last_name']}',
                                                  style: AppStyle.title,
                                                ),
                                                Text(
                                                  'Rating : ${user['ratingPassenger']}',
                                                  style: AppStyle.title,
                                                ),
                                              ],
                                            ),
                                            subtitle: Row(
                                              mainAxisAlignment:
                                                  MainAxisAlignment
                                                      .spaceBetween,
                                              children: [
                                                Text(
                                                  'Count Trip : ${user['countPassengerRide']}',
                                                  style: AppStyle.title,
                                                ),
                                                Text(
                                                  'Count Driver Rate : ${user['countDriverRate']}',
                                                  style: AppStyle.title,
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),
                                      ),
                                    );
                                  },
                                ),
                              ),
                            ],
                          ),
                  ],
                ))
      ],
      isleading: true,
    );
  }

  Container captainAdmin(CaptainAdminController captainAdminController,
      String title, String jsonField) {
    return Container(
      height: Get.height * .1,
      decoration: BoxDecoration(border: Border.all(width: 2)),
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: GestureDetector(
          onTap: () {},
          child: Column(
            children: [
              Text(
                title.tr,
                style: AppStyle.title,
              ),
              Text(
                captainAdminController.captainData['message'][0][jsonField]
                    .toString(),
                style: AppStyle.title,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
