import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:generp/Notifiers/crmProvider/crmLeadDetailsProvider.dart';
import 'package:generp/screens/crm/AppointmentDetails.dart';
import 'package:generp/screens/crm/FollowUpDetails.dart';
import 'package:generp/screens/crm/QuotationDetails.dart';
import 'package:generp/screens/crm/contactDetails.dart';
import 'package:generp/screens/crm/productDetails.dart';
import 'package:provider/provider.dart';

import '../../Utils/app_colors.dart';
import '../../Utils/commonWidgets.dart';
import '../finance/FileViewer.dart';

class LeadDetailsByMode extends StatefulWidget {
  final mode;
  final pageTitleName;
  final leadId;

  const LeadDetailsByMode({
    super.key,
    this.mode,
    this.pageTitleName,
    this.leadId,
  });

  @override
  State<LeadDetailsByMode> createState() => _LeadDetailsByModeState();
}

class _LeadDetailsByModeState extends State<LeadDetailsByMode> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      final provider = Provider.of<crmLeadDetailsProvider>(
        context,
        listen: false,
      );
      provider.crmLeadDetailsAPIFunction(context, widget.leadId, widget.mode);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<crmLeadDetailsProvider>(
      builder: (context, provider, child) {
        var leadDetails = provider.leadDetails;

        return WillPopScope(
          child: Scaffold(
            resizeToAvoidBottomInset: true,
            appBar: appbar2(
              context,
              widget.pageTitleName,
              provider.resetAll,
              InkResponse(
                onTap: () {
                  _showOptionsSheet(
                      context
                  );
                },
                child: SvgPicture.asset("assets/svg/ic_more.svg", height: 30),
              ),
            ),
            backgroundColor: AppColors.scaffold_bg_color,
            body: Container(
              child: SingleChildScrollView(
                child: Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(16),
                  ),
                  margin: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                  padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                  child: Column(
                    children: [
                      Row(
                        children: [
                          Expanded(
                            flex: 1,
                            child: Container(
                              height: 50,
                              width: 35,
                              padding: EdgeInsets.all(8.0),
                              decoration: BoxDecoration(
                                color: Color(0xFFE6F6FF),
                                borderRadius: BorderRadius.circular(8),
                              ),
                              child: SvgPicture.asset(
                                "assets/svg/se_block_head.svg",
                              ),
                            ),
                          ),
                          SizedBox(width: 10),
                          Expanded(
                            flex: 4,
                            child: SizedBox(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    leadDetails.name ?? "-",
                                    style: TextStyle(
                                      fontFamily: "JakartaMedium",
                                      fontSize: 14,
                                      color: AppColors.semi_black,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),

                        ],
                      ),
                      Divider(thickness: 0.5, color: Color(0xFFD7D7D7)),
                      ...List.generate(provider.subHeadings.length, (j) {
                        return Container(
                          padding: EdgeInsets.symmetric(vertical: 7),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Expanded(
                                child: Text(
                                  provider.Headings[j],
                                  style: TextStyle(
                                    fontFamily: "JakartaMedium",
                                    fontSize: 14,
                                    color: AppColors.semi_black,
                                  ),
                                ),
                              ),
                              Expanded(
                                child: InkResponse(
                                  child:provider.Headings[j]=="Lead Age"?  Tooltip(
                                    triggerMode: TooltipTriggerMode.tap,

                                     message: "${provider.leadDetails.createdDatetime}",
                                    child:Text(
                                      provider.subHeadings[j] == ""
                                          ? "-"
                                          : provider.subHeadings[j],
                                      style: TextStyle(
                                        fontSize: 14,
                                        color: Color(0xFF818181),
                                        decorationColor: AppColors.grey_semi,
                                        decoration: TextDecoration.underline
                                      ),
                                    ),
                                  ):Text(
                                    provider.subHeadings[j] == ""
                                        ? "-"
                                        : provider.subHeadings[j],
                                    style: TextStyle(
                                        fontSize: 14,
                                        color: Color(0xFF818181),
                                    ),
                                  ),
                                ),
                              ),
                            ],
                          ),
                        );
                      }),
                    ],
                  ),
                ),
              ),
            ),
          ),

          onWillPop: () {
            return onBackPressed(context);
          },
        );
      },
    );
  }

  Future<void> _showOptionsSheet(BuildContext context) {
    return showModalBottomSheet(
      useSafeArea: true,
      isDismissible: true,
      isScrollControlled: true,
      showDragHandle: true,
      backgroundColor: Colors.white,
      enableDrag: true,
      context: context,
      builder: (context) {
        return StatefulBuilder(
          builder: (context, setState) {
            return SafeArea(
              child: Consumer<crmLeadDetailsProvider>(
                builder: (context, provider, child) {
                  return Container(
                    margin: EdgeInsets.only(
                      bottom: 15,
                      left: 15,
                      right: 15,
                      top: 10,
                    ),

                    child: SingleChildScrollView(
                      child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          SizedBox(height: 15),

                          ...List.generate(5, (index) {
                            final assetnames = [
                              "crm_appoint_details",
                              "crm_cont_details",
                              "crm_follow_up_details",
                              "crm_prod_details",
                              "crm_quotation_details",
                            ];
                            final Headingnames = [
                              "Product Details",
                              "Contact Details",
                              "Followup Details",
                              "Appointment Details",
                              "Quotation Details",
                            ];
                            return ListTile(
                              onTap: () {
                                switch(index){
                                  case 0:
                                    Navigator.push(context, MaterialPageRoute(builder: (context) => Productdetails(),));
                                    break;
                                  case 1:
                                    Navigator.push(context, MaterialPageRoute(builder: (context) => Contactdetails(
                                      accID:provider.leadDetails.accId
                                    ),));
                                    break;
                                  case 2:
                                    Navigator.push(context, MaterialPageRoute(builder: (context) => FollowUpDetails(),));
                                    break;
                                  case 3:
                                    Navigator.push(context, MaterialPageRoute(builder: (context) => AppointmentDetails(leadID:provider.leadDetails.id ,),));
                                    break;
                                  case 4:
                                    Navigator.push(context, MaterialPageRoute(builder: (context) => QuotationDetails(),));
                                  default:
                                    print("object");
                                }
                              },
                              leading: SvgPicture.asset(
                                "assets/svg/${assetnames[index]}.svg",
                              ),
                              title: Text(
                                Headingnames[index],
                                style: TextStyle(fontFamily: "JakartaMedium"),
                              ),
                              trailing: SvgPicture.asset(
                                "assets/svg/arrow_right_new.svg",
                              ),
                            );
                          }),
                        ],
                      ),
                    ),
                  );
                },
              ),
            );
          },
        );
      },
    );
  }
}
