import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sefer_admin1/constant/links.dart';
import 'package:sefer_admin1/controller/functions/crud.dart';
import 'package:sefer_admin1/controller/functions/wallet.dart';
import 'package:sefer_admin1/views/widgets/my_scafold.dart';

import '../../../controller/drivers/driverthebest.dart';

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

  @override
  Widget build(BuildContext context) {
    Get.put(DriverTheBestGizaController(), permanent: true);
    return MyScafolld(
      title: 'Giza'.tr,
      body: [
        GetBuilder<DriverTheBestGizaController>(builder: (driverthebest) {
          return driverthebest.driver.isNotEmpty
              ? ListView.builder(
                  itemCount: driverthebest.driver.length,
                  itemBuilder: (context, index) {
                    final driver = driverthebest.driver[index];
                    return ListTile(
                      leading: CircleAvatar(
                        child: Text(
                          ((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());
                                await CRUD().post(
                                    link: AppLink.deleteRecord,
                                    payload: {
                                      'driver_id': driver['id'].toString()
                                    });
                                driverthebest.driver.removeAt(index);
                                driverthebest.update();
                              },
                              onCancel: () => Get.back());
                        },
                        icon: const Icon(Icons.wallet_giftcard_rounded),
                      ),
                    );
                  },
                )
              : const Center(
                  child: Text('No drivers available.'),
                );
        })
      ],
      isleading: true,
    );
  }
}
