import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:generp/screens/serviceEngineer/PaymentDetails.dart';
import 'package:generp/screens/serviceEngineer/VisitDetails.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../Notifiers/TodayMontlyVisitsProvider.dart';
import '../../Utils/app_colors.dart';

class Todaymontlyvisists extends StatefulWidget {
  final String visitType;

  const Todaymontlyvisists({super.key, required this.visitType});

  @override
  State<Todaymontlyvisists> createState() => _TodaymontlyvisistsState();
}

class _TodaymontlyvisistsState extends State<Todaymontlyvisists> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      var visitsProvider = Provider.of<Todaymontlyvisitsprovider>(
        context,
        listen: false,
      );

      switch (widget.visitType) {
        case "Month":
          visitsProvider.title = "Monthly Visits";
          visitsProvider.LoadMonthVisitsListAPI(context);
          break;
        case "Today":
          visitsProvider.title = "Today Visits";
          visitsProvider.LoadTodayVisitsListAPI(context);
          break;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Todaymontlyvisitsprovider>(
      builder: (context, provider, child) {
        return Scaffold(
          backgroundColor: AppColors.scaffold_bg_color,
          appBar: AppBar(
            automaticallyImplyLeading: false,
            elevation: 2.0,
            title: SizedBox(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  InkResponse(
                    onTap: () => Navigator.pop(context, true),
                    child: SvgPicture.asset(
                      "assets/svg/app_bar_back.svg",
                      height: 25,
                    ),
                  ),
                  Text(
                    provider.title,
                    style: TextStyle(
                      fontSize: 16,
                      height: 1.1,
                      color: AppColors.semi_black,
                    ),
                  ),
                  Spacer(),
                  SvgPicture.asset("assets/svg/scanner.svg"),
                ],
              ),
            ),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.vertical(
                bottom: Radius.circular(30), // Adjust the radius as needed
              ),
            ),
          ),
          body: Container(
            child: ListView.builder(
              scrollDirection: Axis.vertical,
              shrinkWrap: true,
              itemCount: provider.visitsList.length,
              padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),

              itemBuilder: (context, index) {
                return Container(
                  padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                  margin: EdgeInsets.symmetric(vertical: 5),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(20),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Expanded(
                            flex: 1,
                            child: SvgPicture.asset(
                              "assets/svg/se_block_head.svg",
                            ),
                          ),
                          SizedBox(width: 5),

                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(
                                "${provider.visitsList[index]
                                    .companyName}",
                              ),
                              Text("Gen Pvt. Ltd"),
                            ],
                          ),
                          Spacer(),
                          Expanded(
                            flex: 1,
                            child: InkResponse(
                              onTap: () {
                                launch(
                                  'tel://${provider
                                      .visitsList[index]
                                      .mobileNo}',
                                );
                              },
                              child: SvgPicture.asset(
                                "assets/svg/se_call_customer.svg",
                              ),
                            ),
                          ),
                          // Expanded(
                          //   flex: 1,
                          //   child: InkResponse(
                          //     onTap: () {
                          //       _showOptionsSheet(context);
                          //     },
                          //     child: Icon(Icons.more_vert),
                          //   ),
                          // ),
                        ],
                      ),
                      Divider(thickness: 0.5, color: Color(0xFFD7D7D7)),
                      ...List.generate(3, (j) {
                        final textheads = ["ID", "Generator Type", "Address"];
                        final textSubheads = [
                          provider.visitsList[index].genId,
                          provider.visitsList[index].productName,
                          provider.visitsList[index].address,
                        ];
                        return Container(
                          padding: EdgeInsets.symmetric(vertical: 3),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(textheads[j].toString()),
                              Text(textSubheads[j].toString()),
                            ],
                          ),
                        );
                      }),
                      SizedBox(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [

                            InkResponse(
                              onTap: () {
                                Navigator.push(context, MaterialPageRoute(
                                  builder: (context) =>
                                      Paymentdetails(accountName: "Generator",
                                          name: provider
                                              .visitsList[index].companyName,
                                          genId: provider
                                              .visitsList[index].genId,
                                          referenceID: provider
                                              .visitsList[index].complaintId),));
                              },
                              child: SvgPicture.asset(
                                "assets/svg/se_payment_details.svg",
                              ),
                            ),
                            Spacer(),
                            InkResponse(
                              onTap: () {
                                Navigator.push(context, MaterialPageRoute(
                                  builder: (context) =>
                                      Visitdetails(complaintID: provider
                                          .visitsList[index].complaintId),));
                              },
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.start,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Text("View Details", style: TextStyle(
                                      color: AppColors.app_blue
                                  ),),
                                  SizedBox(width: 5),
                                  SvgPicture.asset(
                                    "assets/svg/next_button.svg",
                                  ),
                                ],
                              ),
                            ),
                          ],
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
        );
      },
    );
  }

  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<Todaymontlyvisitsprovider>(
                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),

                          ListTile(
                            onTap: () async {
                              // _showUpdateComplaintSheet(context,provider.visitsList[index].complaintId);
                            },
                            leading: SvgPicture.asset(
                              "assets/svg/se_payment_details.svg",
                            ),
                            title: Text("Payment Details"),
                            trailing: Icon(
                              Icons.arrow_forward_ios_sharp, size: 16,),
                          )
                        ],
                      ),
                    ),
                  );
                },
              ),
            );
          },
        );
      },
    );
  }
}
