import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:generp/screens/crm/ProspectDetailsByMode.dart';
import 'package:provider/provider.dart';

import '../../Notifiers/crmProvider/crmDashboardProvider.dart';
import '../../Utils/app_colors.dart';
import '../../Utils/commonWidgets.dart';

class Universalsearchscreen extends StatefulWidget {
  final text;

  const Universalsearchscreen({super.key, required this.text});

  @override
  State<Universalsearchscreen> createState() => _UniversalsearchscreenState();
}

class _UniversalsearchscreenState extends State<Universalsearchscreen> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      var prov = Provider.of<Crmdashboardprovider>(context, listen: false);
      prov.crmUniversalSearchFunction(context);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Crmdashboardprovider>(
      builder: (context, provider, child) {
        return WillPopScope(
          onWillPop: () => onBackPressed(context),
          child: Scaffold(
            resizeToAvoidBottomInset: true,
            backgroundColor: AppColors.scaffold_bg_color,
            appBar: appbar(context, "Search"),
            body: SafeArea(
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    if (provider.accountsList.length > 0) ...[
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Padding(
                          padding: EdgeInsets.symmetric(horizontal: 6),
                          child: Text(
                            "Accounts",
                            style: TextStyle(
                              fontSize: 16,
                              color: AppColors.grey_semi,
                            ),
                          ),
                        ),
                      ),
                      ListView.builder(
                        physics: NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        padding: EdgeInsets.symmetric(
                          vertical: 10,
                          horizontal: 10,
                        ),
                        itemCount: provider.accountsList.length,
                        itemBuilder: (context, index) {
                          return InkResponse(
                            onTap: () {
                              Navigator.pushAndRemoveUntil(
                                context,
                                MaterialPageRoute(
                                  builder: (context) => ProspectDetailsByMode(
                                    pageTitleName: "Account Details",
                                    mode: "Executive",
                                    leadId: provider.accountsList[index].aid,
                                  ),
                                  settings: RouteSettings(name: 'ProspectDetails'),
                                ),
                                    (Route<dynamic> route) => route.settings.name == 'CrmdashboardScreen',
                              );

                            },
                            child: Container(
                              decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(14),
                              ),

                              margin: EdgeInsets.only(
                                bottom: 5,
                                left: 5,
                                right: 5,
                                top: 5,
                              ),
                              padding: EdgeInsets.only(
                                bottom: MediaQuery.of(context).viewInsets.bottom,
                              ),
                              child: ListTile(
                                title: Text(
                                  provider.accountsList[index].aname!,
                                  style: TextStyle(
                                    fontFamily: "JakartaMedium",
                                    fontSize: 14,
                                  ),
                                ),
                                trailing: SvgPicture.asset(
                                  "assets/svg/arrow_right_new.svg",
                                ),
                              ),
                            ),
                          );
                        },
                      ),
                    ],

                    if (provider.leadsList.length > 0) ...[
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Padding(
                          padding: EdgeInsets.symmetric(horizontal: 6),
                          child: Text(
                            "Leads",
                            style: TextStyle(
                              fontSize: 16,
                              color: AppColors.grey_semi,
                            ),
                          ),
                        ),
                      ),
                      ListView.builder(
                        physics: NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        padding: EdgeInsets.symmetric(
                          vertical: 10,
                          horizontal: 10,
                        ),
                        itemCount: provider.leadsList.length,
                        itemBuilder: (context, index) {
                          return InkResponse(
                            onTap: () {
                              Navigator.pushAndRemoveUntil(
                                context,
                                MaterialPageRoute(
                                  builder: (context) => ProspectDetailsByMode(
                                    pageTitleName: "Lead Details",
                                    mode: "Executive",
                                    leadId: provider.leadsList[index].lid,
                                  ),
                                  settings: RouteSettings(name: 'ProspectDetails'),
                                ),
                                    (Route<dynamic> route) => route.settings.name == 'CrmdashboardScreen',
                              );
                            },
                            child: Container(
                              decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(14),
                              ),

                              margin: EdgeInsets.only(
                                bottom: 5,
                                left: 5,
                                right: 5,
                                top: 5,
                              ),
                              padding: EdgeInsets.only(
                                bottom: MediaQuery.of(context).viewInsets.bottom,
                              ),
                              child: ListTile(
                                title: Text(
                                  provider.leadsList[index].aname!,
                                  style: TextStyle(
                                    fontFamily: "JakartaMedium",
                                    fontSize: 14,
                                  ),
                                ),
                                trailing: SvgPicture.asset(
                                  "assets/svg/arrow_right_new.svg",
                                ),
                              ),
                            ),
                          );
                        },
                      ),
                    ],
                    if (provider.enquiresList.length > 0) ...[
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Padding(
                          padding: EdgeInsets.symmetric(horizontal: 6),
                          child: Text(
                            "Enquiries",
                            style: TextStyle(
                              fontSize: 16,
                              color: AppColors.grey_semi,
                            ),
                          ),
                        ),
                      ),
                      ListView.builder(
                        physics: NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        padding: EdgeInsets.symmetric(
                          vertical: 10,
                          horizontal: 10,
                        ),
                        itemCount: provider.enquiresList.length,
                        itemBuilder: (context, index) {
                          return Container(
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(14),
                            ),

                            margin: EdgeInsets.only(
                              bottom: 5,
                              left: 5,
                              right: 5,
                              top: 5,
                            ),
                            padding: EdgeInsets.only(
                              bottom: MediaQuery.of(context).viewInsets.bottom,
                            ),
                            child: ListTile(
                              title: Text(
                                provider.enquiresList[index].companyName == ""
                                    ? provider.enquiresList[index].name!
                                    : provider.enquiresList[index].companyName!,
                                style: TextStyle(
                                  fontFamily: "JakartaMedium",
                                  fontSize: 14,
                                ),
                              ),
                              trailing: SvgPicture.asset(
                                "assets/svg/arrow_right_new.svg",
                              ),
                            ),
                          );
                        },
                      ),
                    ],
                  ],
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
