import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:service/controller/functions/encrypt_decrypt.dart';
import 'package:service/views/widgets/my_scafold.dart';

import '../../../constant/colors.dart';
import '../../../constant/links.dart';
import '../../../views/widgets/elevated_btn.dart';
import '../../functions/crud.dart';
import 'alexandria_besr_driver.dart';
import 'giza_best_driver.dart';

class DriverTheBest extends StatelessWidget {
  const DriverTheBest({super.key});

  @override
  Widget build(BuildContext context) {
    Get.put(Driverthebest());
    return MyScaffold(
      title: 'Best Drivers'.tr,
      body: [
        Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                MyElevatedButton(
                    title: 'Giza',
                    onPressed: () {
                      Get.to(() => DriverTheBestGiza());
                    }),
                MyElevatedButton(
                    title: 'Alexandria',
                    onPressed: () {
                      Get.to(() => DriverTheBestAlexandria());
                    }),
              ],
            ),
            GetBuilder<Driverthebest>(builder: (driverthebest) {
              return driverthebest.driver.isNotEmpty
                  ? SizedBox(
                      height: Get.height * .7,
                      child: ListView.builder(
                        itemCount: driverthebest.driver.length,
                        itemBuilder: (context, index) {
                          final driver = driverthebest.driver[index];
                          return ListTile(
                            leading: CircleAvatar(
                              child: Text(
                                ((int.parse(driver['driver_count']) * 5) / 3600)
                                    .toStringAsFixed(0),
                              ),
                            ),
                            title: Text((driver['name_arabic']) ??
                                'Unknown Name'),
                            subtitle: Text(
                                'Phone: ${(driver['phone']) ?? 'N/A'}'),
                            trailing: IconButton(
                              onPressed: () async {
                                // Get.defaultDialog(
                                //     title:
                                //         'are you sure to pay to this driver gift'.tr,
                                //     middleText: '',
                                //     onConfirm: () async {
                                //       // final wallet = Get.put(WalletController());
                                //       // await wallet.addPaymentToDriver('100',
                                //       //     driver['id'].toString(), driver['token']);
                                //       // await wallet.addSeferWallet(
                                //       //     '100', driver['id'].toString());
                                //     },
                                //     onCancel: () => Get.back());
                              },
                              icon: const Icon(Icons.wallet_giftcard_rounded),
                            ),
                          );
                        },
                      ),
                    )
                  : const Center(
                      child: Text('No drivers available.'),
                    );
            }),
          ],
        )
      ],
      isleading: true,
    );
  }
}

class Driverthebest extends GetxController {
  bool isLoading = false;
  List driver = [];
  getBestDriver() async {
    var res = await CRUD().get(link: AppLink.getBestDriver, payload: {});
    if (res != 'failure') {
      driver = jsonDecode(res)['message'];
      update();
    } else {
      Get.snackbar('error', '', backgroundColor: AppColor.redColor);
    }
  }

  @override
  void onInit() {
    getBestDriver();
    super.onInit();
  }
}
