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.symmetric(horizontal: 12.0, vertical: 6.0),
                                child: Card(
                                  elevation: 2,
                                  color: car['isDefault'].toString() == '1'
                                      ? AppColor.primaryColor
                                      : Theme.of(context).cardColor,
                                  shape: RoundedRectangleBorder(
                                      borderRadius: BorderRadius.circular(12),
                                      side: car['isDefault'].toString() == '1'
                                          ? BorderSide.none
                                          : BorderSide(color: Theme.of(context).dividerColor)),
                                  child: ListTile(
                                    contentPadding: const EdgeInsets.all(12),
                                    leading: Container(
                                      padding: const EdgeInsets.all(8),
                                      decoration: BoxDecoration(
                                        color: Colors.white12,
                                        borderRadius: BorderRadius.circular(8),
                                      ),
                                      child: Icon(
                                        Fontisto.car,
                                        size: 32,
                                        color: car['isDefault'].toString() == '1'
                                            ? Colors.white
                                            : Color(int.parse(car['color_hex']
                                                .replaceFirst('#', '0xff'))),
                                      ),
                                    ),
                                    title: Text(
                                      car['make'],
                                      style: TextStyle(
                                        fontWeight: FontWeight.bold,
                                        fontSize: 16,
                                        color: car['isDefault'].toString() == '1'
                                            ? Colors.white
                                            : Theme.of(context).textTheme.bodyLarge?.color,
                                      ),
                                    ),
                                    subtitle: Padding(
                                      padding: const EdgeInsets.only(top: 8.0),
                                      child: Row(
                                        children: [
                                          Text(
                                            car['model'],
                                            style: TextStyle(
                                              color: car['isDefault'].toString() == '1'
                                                  ? Colors.white70
                                                  : Theme.of(context).hintColor,
                                            ),
                                          ),
                                          const SizedBox(width: 12),
                                          Container(
                                            padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                                            decoration: BoxDecoration(
                                              borderRadius: BorderRadius.circular(4),
                                              border: Border.all(
                                                color: car['isDefault'].toString() == '1'
                                                    ? Colors.white54
                                                    : AppColor.primaryColor,
                                              ),
                                            ),
                                            child: Text(
                                              car['car_plate'],
                                              style: TextStyle(
                                                fontSize: 12,
                                                fontWeight: FontWeight.bold,
                                                color: car['isDefault'].toString() == '1'
                                                    ? Colors.white
                                                    : AppColor.primaryColor,
                                              ),
                                            ),
                                          ),
                                          const Spacer(),
                                          Text(
                                            car['year'],
                                            style: TextStyle(
                                              color: car['isDefault'].toString() == '1'
                                                  ? Colors.white70
                                                  : Theme.of(context).hintColor,
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),

                                    // 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);
  }
}
