import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:generp/screens/order/tpcAgentIssueListDetails.dart';
import 'package:provider/provider.dart';

import '../../Notifiers/ordersProvider/tpcAgentsProvider.dart';
import '../../Utils/app_colors.dart';
import '../../Utils/commonWidgets.dart';
import 'ordersDetailsByModes.dart';

class Tpcagentissuelist extends StatefulWidget {
  final pageTitleName;
  final mode;
  const Tpcagentissuelist({super.key,required this.pageTitleName,required this.mode});

  @override
  State<Tpcagentissuelist> createState() => _TpcagentissuelistState();
}

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

      provider.TPCAgentsIssueListAPIFunction(context);
    });
  }


  @override
  Widget build(BuildContext context) {
    return Consumer<Tpcagentsprovider>(
      builder: (context, provider, child) {
        final tpcAgentsIssueList = provider.tpcAgentsIssueList;
        return WillPopScope(
          onWillPop: () {
            provider.resetAll();
            return onBackPressed(context);
          },
          child: SafeArea(
            top: false,
            bottom: Platform.isIOS?false:true,
            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)
              ))
                  : tpcAgentsIssueList.isNotEmpty ? SingleChildScrollView(
                child: Column(
                  children: [
                    ListView.builder(
                      itemCount: tpcAgentsIssueList.length,
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      itemBuilder: (context, index) {
                        if (tpcAgentsIssueList.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(
                                            tpcAgentsIssueList[index].name!,
                                            style: TextStyle(
                                              fontFamily:
                                              "JakartaMedium",
                                              fontSize: 14,
                                              color:
                                              AppColors.semi_black,
                                            ),
                                          ),
                                          Text(

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

                                ],
                              ),
                              Divider(
                                thickness: 0.5,
                                color: Color(0xFFD7D7D7),
                              ),
                              ...List.generate(4, (j) {
                                final headings = [
                                  "Sales Person",
                                  "Order Number",
                                  "Order Amount",
                                  "Order Status",


                                ];

                                final subHeadings = [
                                  tpcAgentsIssueList[index].ename,
                                  tpcAgentsIssueList[index].orderNumber,
                                  tpcAgentsIssueList[index].totalAmount,
                                  tpcAgentsIssueList[index].status,
                                ];
                                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,
                                          ),
                                        ),
                                      ),
                                      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) =>
                                      Tpcagentissuelistdetails(
                                  pageTitleName: "CRM Order Details",
                                  mode: widget.mode,
                                  orderId: tpcAgentsIssueList[index].orderId,
                                  ),
                                  ),
                                  );
                                },
                                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),
            ),
          ),
        );
      },
    );
  }
}
