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

import '../../Controller/Customers/Customercontroller.dart';
import '../../Globalwidgets/textwidget.dart';
import '../../Helper/Constants/Assetconstants.dart';
import '../../Helper/Constants/Colorconstants.dart';
import '../../Helper/utility.dart';
import '../../Model/Response/Customers/GetCustomerById/GetCustomerByTenantId.dart';
import '../Dashboard/Dashboardview.dart';

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

  final CustomerController customerController = Get.put(CustomerController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const TextWidget(
          text: 'Customers',
          fontSize: 20,
          fontWeight: FontWeight.bold,
        ),
        elevation: 0,
        backgroundColor: ColorConstants.secondaryColor,
        automaticallyImplyLeading: false,
      ),
      body: GetBuilder<CustomerController>(
        initState: (_) {
          customerController.getCustomerByTenantId();
        },
        builder: (controller) {
          if (controller.progress.value) {
            return Center(
              child: ShimmerListView(
                  height: 100
              ),
            );
          }

          if (controller.getCustomerBytenantId.isEmpty) {
            return emptyCustomerWidget();
          }

          return Padding(
            padding: const EdgeInsets.only(top: 5),
            child: SafeArea(
              bottom: false,
              child: ListView.builder(
                  itemCount: controller.getCustomerBytenantId.length,
                  itemBuilder: (context, index) {
                    var customer = controller.getCustomerBytenantId[index];
                    return CustomerCard(
                      name: customer.firstname ?? '',
                      address: (customer.suburb?.isNotEmpty ?? false) ?
                      customer.suburb ?? '' : customer.city ?? '',
                      phoneNumber: customer.contactno ?? '',
                      profileImageUrl: '',
                    );
                  }
              ),
            ),
          );
        },
      ),
    );
  }

  Widget emptyCustomerWidget() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(height:10),
          Image.asset(
            AssetConstants.noCustomersFound,
            height: 200,
            width: 200,
            fit: BoxFit.fill,
          ),
          TextWidget(
            text: 'No Customers Found!',
            color: ColorConstants.blackColor,
            fontSize: 18,
            fontWeight: FontWeight.w700,
            maxLines: 2,
            textAlign: TextAlign.center,
          ),
          SizedBox(height: 10),
          TextWidget(
            text: 'Stay tuned - your next loyal customer could appear here soon!',
            color: ColorConstants.blackColor,
            fontSize: 14,
            fontWeight: FontWeight.normal,
            maxLines: 2,
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }


}




class CustomerCard extends StatelessWidget {
  final String name;
  final String address;
  final String phoneNumber;
  final String profileImageUrl;

  const CustomerCard({
    super.key,
    required this.name,
    required this.address,
    required this.phoneNumber,
    required this.profileImageUrl,
  });

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: const EdgeInsets.symmetric(horizontal: 0, vertical: 6),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(0)),
      elevation: 0,
      child: Padding(
        padding: const EdgeInsets.all(12.0),
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            /// Profile image
            profileImageUrl.isNotEmpty ?? false ?
            CircleAvatar(
              radius: 30,
              backgroundImage: NetworkImage(profileImageUrl),
              backgroundColor: Colors.grey.shade300,
            ) :
            CircleAvatar(
              radius: 30,
              child: TextWidget(
                text: name[0],
                fontWeight: FontWeight.bold,
                color: Colors.black,
                fontSize: 22,
              ),
              backgroundColor: Colors.grey.shade300,
            ),
            const SizedBox(width: 16),

            // Customer details
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Icon(Icons.person, color: ColorConstants.darkGreyColor, size: 17,),
                      SizedBox(width: 5,),
                      Text(
                        name,
                        style:  TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.bold,
                            color: ColorConstants.darkGreyColor
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 4),
                  Row(
                    children: [
                      Icon(Icons.location_on_outlined, color: ColorConstants.darkGreyColor, size: 14,),
                      SizedBox(width: 5,),
                      Text(
                        address,
                        style:  TextStyle(
                          fontSize: 14,
                          color: ColorConstants.darkGreyColor,
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(height: 4),
                  Row(
                    children: [
                      Padding(
                        padding: EdgeInsets.only(left: 2),
                        child: Icon(Icons.phone, color: ColorConstants.darkGreyColor, size: 14,),
                      ),
                      SizedBox(width: 5,),
                      Text(
                        '+91 $phoneNumber',
                        style: const TextStyle(
                          fontSize: 14,
                          color: Colors.grey,
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),

            // Call icon
            IconButton(
              icon: const Icon(Icons.phone, color: Colors.green),
              onPressed: () {
                // TODO: Add phone call logic
                Utility.openPhoneCallApp(phoneNumber);
              },
            ),
          ],
        ),
      ),
    );
  }
}
