import 'package:sefer_driver/constant/box_name.dart';
import 'package:sefer_driver/constant/colors.dart';
import 'package:sefer_driver/constant/style.dart';
import 'package:sefer_driver/controller/functions/encrypt_decrypt.dart';
import 'package:sefer_driver/main.dart';
import 'package:sefer_driver/views/widgets/my_scafold.dart';
import 'package:sefer_driver/views/widgets/mycircular.dart';
import 'package:sefer_driver/views/widgets/mydialoug.dart';
import 'package:flutter/material.dart';
import 'package:flutter_font_icons/flutter_font_icons.dart';
import 'package:get/get.dart';

import '../../auth/captin/driver_car_controller.dart';
import '../../widgets/elevated_btn.dart';
import 'cars_inserting_page.dart';

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

  @override
  Widget build(BuildContext context) {
    Get.put(DriverCarController());
    return MyScafolld(
        title: "Add new car".tr,
        body: [
          Column(
            children: [
              MyElevatedButton(
                title: "Add new car".tr,
                onPressed: () async {
                  Get.to(() => CarsInsertingPage());
                },
              ),
              Expanded(
                child: GetBuilder<DriverCarController>(
                  builder: (controller) {
                    return controller.isLoading
                        ? const MyCircularProgressIndicator()
                        : ListView.builder(
                            itemCount: controller.cars.length,
                            itemBuilder: (context, index) {
                              final car = controller.cars[index];
                              return Padding(
                                padding: const EdgeInsets.all(4.0),
                                child: Card(
                                  color: car['isDefault'].toString() == '0'
                                      ? AppColor.accentColor
                                      : AppColor.blueColor,
                                  elevation: 2,
                                  child: ListTile(
                                    leading: Icon(
                                      Fontisto.car,
                                      size: 50,
                                      color: Color(int.parse(car['color_hex']
                                          .replaceFirst('#', '0xff'))),
                                    ),
                                    title: Text(
                                      car['make'],
                                      style: AppStyle.title,
                                    ), // Assuming `make` is a field in each car item
                                    subtitle: Row(
                                      mainAxisAlignment:
                                          MainAxisAlignment.spaceBetween,
                                      children: [
                                        Text(
                                          car['model'],
                                          style: AppStyle.title,
                                        ),
                                        Container(
                                          decoration: BoxDecoration(
                                              border: Border.all(
                                                  color: AppColor.blueColor)),
                                          child: Padding(
                                            padding: const EdgeInsets.symmetric(
                                                horizontal: 4),
                                            child: Text(
                                              (car['car_plate']),
                                              style: AppStyle.title,
                                            ),
                                          ),
                                        ),
                                        Text(
                                          car['year'],
                                          style: AppStyle.title,
                                        ),
                                      ],
                                    ), // Assuming `model` is a field in each car item
                                    // trailing: IconButton(
                                    //   icon: const Icon(
                                    //     Icons.delete,
                                    //     color: AppColor.redColor,
                                    //   ),
                                    //   onPressed: () {
                                    //     // Add logic here to remove a car
                                    //     MyDialog()
                                    //         .getDialog('Are you sure to delete this car', '', () {
                                    //       controller
                                    //           .removeCar(car['id'].toString());
                                    //     });

                                    //   },
                                    // ),
                                    onTap: () {
                                      MyDialog().getDialog(
                                          'Are you sure to make this car as default'
                                              .tr,
                                          '', () {
                                        Get.back();
                                        //make it default
                                        controller.updateCarRegistration(
                                          car['id'].toString(),
                                          box.read(BoxName.driverID).toString(),
                                        );
                                      });
                                      // Add logic to view or edit the car details
                                    },
                                  ),
                                ),
                              );
                            },
                          );
                  },
                ),
              ),
            ],
          )
        ],
        isleading: true);
  }
}
