import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:generp/Notifiers/ordersProvider/tpcAgentsProvider.dart';
import 'package:generp/screens/finance/FileViewer.dart';
import 'package:generp/screens/order/tpcAgentDetailsByMode.dart';
import 'package:provider/provider.dart';

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

class Tpcagentlistbymode extends StatefulWidget {
  final pageTitleName;
  final mode;

  const Tpcagentlistbymode(
      {super.key, required this.pageTitleName, required this.mode});

  @override
  State<Tpcagentlistbymode> createState() => _TpcagentlistbymodeState();
}

class _TpcagentlistbymodeState extends State<Tpcagentlistbymode> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      var provider = Provider.of<Tpcagentsprovider>(
        context,
        listen: false,
      );

      provider.TPCAgentsListAPIFunction(context, widget.mode);
    });
  }


  @override
  Widget build(BuildContext context) {
    return Consumer<Tpcagentsprovider>(
      builder: (context, provider, child) {
        final tpcAgentsLists = provider.tpcAgentsList;
        return WillPopScope(
          onWillPop: () {
            provider.resetAll();
            return onBackPressed(context);
          },
          child: Scaffold(
            resizeToAvoidBottomInset: true,
            appBar: appbar2(
              context,
              widget.pageTitleName,
              provider.resetAll,
              Row(
                children: [
                  // InkResponse(
                  //   onTap: () {
                  //     // _showOptionsSheet(context);
                  //   },
                  //   child: SvgPicture.asset("assets/svg/ic_download.svg",),
                  // ),


                ],
              ),
            ),
            backgroundColor: AppColors.scaffold_bg_color,
            body:
            provider.isLoading
                ? Center(child: CircularProgressIndicator.adaptive(
                valueColor: AlwaysStoppedAnimation<Color>(
                    AppColors.app_blue)
            ))
                : tpcAgentsLists.isNotEmpty ? SingleChildScrollView(
              child: Column(
                children: [
                  ListView.builder(
                    itemCount: tpcAgentsLists.length,
                    shrinkWrap: true,
                    physics: NeverScrollableScrollPhysics(),
                    itemBuilder: (context, index) {
                      if (tpcAgentsLists.isEmpty) {
                        return SizedBox(
                          child: Center(
                            child: Text("No Data Available"),
                          ),
                        );
                      }
                      return Container(
                        padding: EdgeInsets.symmetric(
                          horizontal: 10,
                          vertical: 10,
                        ),
                        margin: EdgeInsets.symmetric(
                          horizontal: 10,
                          vertical: 10,
                        ),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(16),
                        ),
                        child: Column(
                          children: [
                            Row(
                              children: [
                                Expanded(
                                  flex: 1,
                                  child: Container(
                                    height: 50,
                                    width: 35,

                                    decoration: BoxDecoration(
                                      color: Color(0xFFEAF7FF),
                                      borderRadius:
                                      BorderRadius.circular(8),
                                    ),
                                    child: SvgPicture.asset(
                                      "assets/svg/tpc_ic.svg",
                                    ),
                                  ),
                                ),
                                SizedBox(width: 10),
                                Expanded(
                                  flex: 4,
                                  child: SizedBox(
                                    child: Column(
                                      crossAxisAlignment:
                                      CrossAxisAlignment.start,
                                      children: [
                                        Text(
                                          tpcAgentsLists[index].name!,
                                          style: TextStyle(
                                            fontFamily:
                                            "JakartaMedium",
                                            fontSize: 14,
                                            color:
                                            AppColors.semi_black,
                                          ),
                                        ),
                                        Text(

                                          "${tpcAgentsLists[index]
                                              .mobileNumber}",
                                          style: TextStyle(
                                            fontFamily:
                                            "JakartaMedium",
                                            fontSize: 14,
                                            color: AppColors.app_blue,
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ),

                              ],
                            ),
                            Divider(
                              thickness: 0.5,
                              color: Color(0xFFD7D7D7),
                            ),
                            ...List.generate(4, (j) {
                              final headings = [
                                "Entered Employee",
                                "Issued Amount",
                                "ID Proof",
                                "Description",
                              ];

                              final subHeadings = [
                                tpcAgentsLists[index].enteredEmpName,
                                tpcAgentsLists[index].issuedAmount,
                                "View",
                                tpcAgentsLists[index].note,
                              ];
                              return Container(
                                padding: EdgeInsets.symmetric(
                                  vertical: 5,
                                ),
                                child: Row(
                                  crossAxisAlignment:
                                  CrossAxisAlignment.start,
                                  children: [
                                    Expanded(
                                      child: Text(
                                        headings[j],
                                        style: TextStyle(
                                          fontFamily: "JakartaMedium",
                                          fontSize: 14,
                                          color: AppColors.semi_black,
                                        ),
                                      ),
                                    ),
                                    if(subHeadings[j] == "View")...[
                                      Expanded(
                                        child: InkResponse(
                                          onTap: () {
                                            Navigator.push(context,
                                                MaterialPageRoute(
                                                  builder: (context) =>
                                                      Fileviewer(
                                                          fileName: tpcAgentsLists[index]
                                                              .idProofViewFileName ??
                                                              "-",
                                                          fileUrl: tpcAgentsLists[index]
                                                              .idProofDirFilePath ??
                                                              "-"),));
                                          },
                                          child: Text(
                                            "${subHeadings[j]}",
                                            style: TextStyle(
                                              fontSize: 14,
                                              color:AppColors.app_blue,
                                              decoration: TextDecoration
                                                  .underline,
                                              decorationColor: AppColors.app_blue,
                                            ),
                                          ),
                                        ),
                                      ),
                                    ] else
                                      ...[
                                        Expanded(
                                          child: InkResponse(

                                            child: Text(
                                              "${subHeadings[j]}",
                                              style: TextStyle(
                                                fontSize: 14,
                                                color: Color(
                                                  0xFF818181,
                                                ),
                                                decoration: TextDecoration
                                                    .none,
                                                decorationColor: AppColors
                                                    .white,
                                              ),
                                            ),
                                          ),
                                        ),
                                      ]

                                  ],
                                ),
                              );
                            }),
                            InkResponse(
                              onTap: () async {
                                Navigator.push(context, MaterialPageRoute(
                                  builder: (context) =>
                                      Tpcagentdetailsbymode(mode: widget.mode,
                                        pageTitleName: widget.pageTitleName,
                                        tpcAgentId: tpcAgentsLists[index]
                                            .tpcAgentId,
                                      ),));
                              },
                              child: Container(
                                padding: EdgeInsets.symmetric(
                                  vertical: 5,
                                ),
                                child: Row(
                                  crossAxisAlignment:
                                  CrossAxisAlignment.center,
                                  mainAxisAlignment:
                                  MainAxisAlignment.start,
                                  children: [
                                    Text(
                                      "View Details",
                                      style: TextStyle(
                                        fontFamily: "JakartaMedium",
                                        fontSize: 14,
                                        color: AppColors.app_blue,
                                      ),
                                    ),
                                    SizedBox(width: 5),
                                    SvgPicture.asset(
                                      "assets/svg/next_button.svg",
                                    ),
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      );
                    },
                  ),
                ],
              ),
            ) : Emptywidget(context),
          ),
        );
      },
    );
  }
}
