import 'package:alphabet_search_view/alphabet_search_view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../../../../Controller/Createorder/Createordercontroller.dart';
import '../../../../../../Controller/Dashboard/Pickup/Tabs/Tenantcustomers/Customeraddress/Addcustomer/Addcustomercontroller.dart';
import '../../../../../../Controller/Dashboard/Pickup/Tabs/Tenantcustomers/Tenantcustomercontroller.dart';
import '../../../../../../Helper/Constants/Assetconstants.dart';
import '../../../../../../Helper/Constants/Colorconstants.dart';
import '../../../../../../Helper/toast.dart';
import '../../../../../../Model/Response/Customers/GetCustomerById/GetCustomerByTenantId.dart';
import '../../../../Createorderview.dart';

class TenantPickUpCustomerView extends StatelessWidget {
  TenantPickUpCustomerView({super.key});

  TenantPickupCustomerController tenantPickupCustomerController = Get.put(TenantPickupCustomerController());
   AddPickupCustomerController addPickupCustomerController = Get.put(AddPickupCustomerController());
   CreateOrderController createOrderController = Get.put(CreateOrderController());
   // PickupCustomerAddressController pickupCustomerAddressController = Get.put(PickupCustomerAddressController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[100],

      body: GetBuilder<TenantPickupCustomerController>(
          initState: (_){
            tenantPickupCustomerController.progress = true;
            tenantPickupCustomerController.getCustomerByTenantId();

          },
          builder: (controller) {
            return  controller.getCustomerBytenantId.isEmpty && !controller.progress == true
                ? Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Image(
                      height: 160,
                      width: 160,
                      image: AssetImage(AssetConstants.NoRecords),
                    ),
                    SizedBox(
                      height: 8,
                    ),
                    Text(
                      "No customers at this moment",
                      style: TextStyle(
                          color: Colors.grey[600],
                          fontSize: 18),
                    ),
                  ],
                )):controller.progress?Center(child: CircularProgressIndicator(color: ColorConstants.primaryColor)): Padding(
              padding: const EdgeInsets.only(top: 5),
              child: SafeArea(
                bottom: false,
                child: AlphabetSearchView<GetCustomerByTenantIdDetails>.list(
                  decoration: AlphabetSearchDecoration.fromContext(
                    backgroundColor: ColorConstants.secondaryColor,
                    color: ColorConstants.primaryColor,
                    withSearch: true,
                    context,
                    subtitleStyle:  TextStyle(backgroundColor: ColorConstants.secondaryColor),
                    titleStyle: TextStyle(backgroundColor: ColorConstants.secondaryColor,),
                    letterHeaderTextStyle: TextStyle(
                        backgroundColor: ColorConstants.secondaryColor,
                        color: ColorConstants.primaryColor,
                        fontWeight: FontWeight.bold,
                        fontSize: 40),
                  ),
                  list: controller.getCustomerBytenantId.map(
                        (e) => AlphabetSearchModel<GetCustomerByTenantIdDetails>(
                      title: e.firstname.toString() +  e.lastname.toString(),
                      subtitle: e.contactno.toString(),
                      data: e,
                    ),
                  )
                      .toList(),
                  onItemTap: (_, index, item) {
                    // deliveryDetailsController.weightSelectedIndex = -1;
                    // deliveryDetailsController.paymentSelectedIndex = -1;
                    // deliveryDetailsController.timeSelectIndex = -1;
                    // deliveryDetailsController.weight = null;
                    // deliveryDetailsController.selectedDate = DateTime.now();
                    // deliveryDetailsController.datetime = DateTime.now();
                    // deliveryDetailsController.subCatName = null;
                    // deliveryDetailsController.tecNotes.clear();
                    // deliveryDetailsController.payment =null;
                    // savedAddressController.deliveryAddressList = false;
                    // savedAddressController.mSelectedAddressIndex = -1;
                    // pickupCustomerAddressController.selectedAddressIndex = -1;
                    createOrderController.pickupCustomerId = item.data.customerid;
                    createOrderController.pickUpCustomerName = "${item.data.firstname}" + "${item.data.lastname}";
                    createOrderController.pickUpCustomerContactNo = item.data.contactno;
                    createOrderController.pickupLocationId = item.data.deliverylocationid;
                    createOrderController.pickupTenantLocationId = item.data.tenantlocationid;
                    createOrderController.fromLatitude = item.data.latitude;
                    createOrderController.fromLongitude = item.data.longitude;
                    createOrderController.fromAddress =item.data.address;

                    if(createOrderController.pickupCustomerId == createOrderController.dropCustomerId){
                      Toast.showToast('Please select different customer it already selected');
                    }
                    else{
                      Get.to(CreateOrderView(pickUpData:item.data, isDashboard: false,));

                    }


                  },
                  buildItem: (_, index, item) {
                    print('dashboardgetCustomerBytenantId${controller.getCustomerBytenantId.length}');
                    return Container(
                      decoration: BoxDecoration(color: ColorConstants.secondaryColor),
                      child: Padding(
                        padding: const EdgeInsets.symmetric(
                          horizontal: 10,
                          vertical: 14,
                        ),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            SizedBox(width: 12),
                            Expanded(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  Flexible(
                                    child: Row(
                                      children: [
                                        Icon(Icons.person,color: ColorConstants.primaryColor,size: 20,),
                                        SizedBox(width: 3,),
                                        Text(
                                          item.title,
                                          style: TextStyle(color: ColorConstants.darkGreyColor,fontSize: 14,fontWeight: FontWeight.bold),
                                        ),
                                      ],
                                    ),
                                  ),
                                  SizedBox(height: 4),
                                  Flexible(
                                    child: Row(
                                      children: [
                                        Icon(Icons.location_city,color: ColorConstants.primaryColor,size: 20,),
                                        SizedBox(width: 3,),
                                        Text(
                                          '${item.data.suburb??''}',
                                          style: TextStyle(color: ColorConstants.darkGreyColor,fontSize: 14,fontWeight: FontWeight.bold),
                                        ),
                                      ],
                                    ),
                                  ),
                                  SizedBox(height: 4),
                                  Flexible(
                                    child: Row(
                                      children: [
                                        Icon(Icons.location_on,color: ColorConstants.primaryColor,size: 20,),
                                        SizedBox(width: 3,),
                                        Expanded(
                                          child: Text(
                                            '${item.data.address??''}',
                                            style: TextStyle(color: ColorConstants.darkGreyColor,fontSize: 14,fontWeight: FontWeight.bold),
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,

                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  SizedBox(height: 4),
                                  Flexible(
                                    child: Row(
                                      children: [
                                        Icon(Icons.phone,color: ColorConstants.primaryColor,size: 20,),
                                        SizedBox(width: 3,),
                                        Text(
                                          item.subtitle!,
                                          style: TextStyle(color: ColorConstants.darkGreyColor,fontSize: 14,fontWeight: FontWeight.bold),
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            )
                          ],
                        ),
                      ),
                    );
                  },
                ),
              ),
            );
          }
      ),
    );
  }
}
