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

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

class TenantDropTenantsView extends StatelessWidget {

  TenantDropTenantsView({super.key,});

  TenantDropCustomerController tenantDropCustomerController = Get.put(TenantDropCustomerController());
  AddDropCustomerController addDropCustomerController = Get.put(AddDropCustomerController());
  CreateOrderController createOrderController = Get.put(CreateOrderController());
  // DropCustomerAddressController dropCustomerAddressController = Get.put(DropCustomerAddressController());

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

      body: GetBuilder<TenantDropCustomerController>(
          initState: (_){
            tenantDropCustomerController.progress = true;
            tenantDropCustomerController.getDropTenantByTenantId();

          },
          builder: (controller) {
            return  controller.getCustomerBytenantId.isEmpty && !controller.progress == true
                ? Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    const Image(
                      height: 160,
                      width: 160,
                      image: AssetImage(AssetConstants.NoRecords),
                    ),
                    const 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<GetDropCustomerByTenantIdDetails>.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<GetDropCustomerByTenantIdDetails>(
                      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;
                    // dropCustomerAddressController.selectedAddressIndex = -1;

                    createOrderController.dropCustomerId = item.data.customerid;
                    createOrderController.dropCustomerName ="${item.data.firstname}" + "${item.data.lastname}";
                    createOrderController.dropCustomerContactNo =  item.data.contactno;
                    createOrderController.dropLocationId =  item.data.deliverylocationid;
                    createOrderController.dropTenantLocationId =  item.data.tenantlocationid;
                    createOrderController.toLatitude = item.data.latitude;
                    createOrderController.toLongitude = item.data.longitude;
                    createOrderController.toAddress = item.data.address;
                    print('dropTenantLocationIdTenantview ${createOrderController.dropTenantLocationId}');

                    if(createOrderController.pickupCustomerId == createOrderController.dropCustomerId){
                      Toast.showToast('Please select different customer it already selected');
                    }
                    else{
                      Get.to(CreateOrderView(dropData: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: [
                            const 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,),
                                        const SizedBox(width: 3,),
                                        Text(
                                          item.title,
                                          style: TextStyle(color: ColorConstants.darkGreyColor,fontSize: 14,fontWeight: FontWeight.bold),
                                        ),
                                      ],
                                    ),
                                  ),
                                  const SizedBox(height: 4),
                                  Flexible(
                                    child: Row(
                                      children: [
                                        Icon(Icons.location_city,color: ColorConstants.primaryColor,size: 20,),
                                        const SizedBox(width: 3,),
                                        Text(
                                          '${item.data.suburb??''}',
                                          style: TextStyle(color: ColorConstants.darkGreyColor,fontSize: 14,fontWeight: FontWeight.bold),
                                        ),
                                      ],
                                    ),
                                  ),
                                  const SizedBox(height: 4),
                                  Flexible(
                                    child: Row(
                                      children: [
                                        Icon(Icons.location_on,color: ColorConstants.primaryColor,size: 20,),
                                        const SizedBox(width: 3,),
                                        Expanded(
                                          child: Text(
                                            '${item.data.address??''}',
                                            style: TextStyle(color: ColorConstants.darkGreyColor,fontSize: 14,fontWeight: FontWeight.bold),
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,

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

