import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:generp/Notifiers/ordersProvider/tpcAgentsProvider.dart';
import 'package:provider/provider.dart';

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

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:generp/Utils/commonServices.dart';
class Tpcagentdetailsbymode extends StatefulWidget {
  final tpcAgentId;
  final pageTitleName;
  final mode;
  const Tpcagentdetailsbymode({super.key, this.tpcAgentId, this.pageTitleName, this.mode});

  @override
  State<Tpcagentdetailsbymode> createState() => _TpcagentdetailsbymodeState();
}

class _TpcagentdetailsbymodeState extends State<Tpcagentdetailsbymode> {
  Map _source = {ConnectivityResult.mobile: true};
  final MyConnectivity _connectivity = MyConnectivity.instance;


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _connectivity.initialise();
    _connectivity.myStream.listen((source) {
      setState(() => _source = source);
    });
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      var provider = Provider.of<Tpcagentsprovider>(
        context,
        listen: false,
      );
      provider.TPCAgentsDetailsAPIFunction(
          context, widget.tpcAgentId);
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _connectivity.disposeStream();
  }

  @override
  Widget build(BuildContext context) {
    switch (_source.keys.toList()[0]) {
      case ConnectivityResult.mobile:
        connection = 'Online';
        break;
      case ConnectivityResult.wifi:
        connection = 'Online';
        break;
      case ConnectivityResult.none:
      default:
        connection = 'Offline';
    }
    return connection=="Online"?
    Consumer<Tpcagentsprovider>(
      builder: (context, provider, child) {
        var tpcAgentDetails = provider.TPCAgentsDetails;
        var tpcReqAmt = provider.TPCRequestedAmounts;
        return WillPopScope(
          child: SafeArea(
            top: false,
            bottom: Platform.isIOS?false:true,
            child: Scaffold(
              resizeToAvoidBottomInset: true,
              appBar: appbar2(context, widget.pageTitleName, provider.resetAll,
                SizedBox(width: 0,),),
              backgroundColor: AppColors.scaffold_bg_color,
              body: Container(
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      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(0xFFFFF3CE),
                                      borderRadius: BorderRadius.circular(8),
                                    ),
                                    child: SvgPicture.asset("assets/svg/fin_ic.svg"),
                                  ),
                                ),
                                SizedBox(width: 10),
                                Expanded(
                                  flex: 4,
                                  child: SizedBox(
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Text(
                                          tpcAgentDetails.name ?? "-",
                                          style: TextStyle(
                                            fontFamily: "JakartaMedium",
                                            fontSize: 14,
                                            color: AppColors.semi_black,
                                          ),
                                        ),
                                        Text(
                                          "${tpcAgentDetails.mobileNumber}",
                                          style: TextStyle(
                                            fontFamily: "JakartaMedium",
                                            fontSize: 14,
                                            color: AppColors.app_blue,
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ),

                              ],
                            ),
                            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(
                                        onTap: () {
                                          if (provider.Headings[j] ==
                                              "ID Proof") {
                                            Navigator.push(
                                              context,
                                              MaterialPageRoute(
                                                builder:
                                                    (context) =>
                                                    Fileviewer(
                                                      fileName:
                                                      tpcAgentDetails
                                                          .idProofViewFileName!,
                                                      fileUrl:
                                                      tpcAgentDetails
                                                          .idProofDirFilePath!,
                                                    ),
                                              ),
                                            );
                                          }
                                        },
                                        child: Text(
                                          "${provider.subHeadings[j]}",
                                          style: TextStyle(
                                            fontSize: 14,
                                            color:
                                            provider.Headings[j] == "ID Proof"
                                                ? AppColors.app_blue
                                                : Color(0xFF818181),
                                            decoration:
                                            provider.Headings[j] == "ID Proof"
                                                ? TextDecoration.underline
                                                : TextDecoration.none,
                                            decorationColor:
                                            provider.Headings[j] == "ID Proof"
                                                ? AppColors.app_blue
                                                : AppColors.white,
                                          ),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              );
                            }),
                          ],
                        ),
                      ),

                      Container(
                          alignment: Alignment.topLeft,
                          padding: EdgeInsets.symmetric(vertical: 5,horizontal: 10),
                          child: Text("TPC Amounts Requested",
                            style: TextStyle(
                                fontSize: 14,
                                color: AppColors.grey_thick
                            ),)),
                      ListView.builder(
                          scrollDirection: Axis.vertical,
                          shrinkWrap: true,
                          physics: NeverScrollableScrollPhysics(),
                          itemCount: tpcReqAmt.length,
                          padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),

                          itemBuilder: (context, index) {
                            return Container(
                              margin: EdgeInsets.symmetric(vertical: 5),
                              padding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
                              decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(20),
                              ),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: List.generate(6, (j) {
                                  final textheads = [
                                    "Customer Name",
                                    "Order Number",
                                    "Order Amount",
                                    "Level 2 Approved Amount",
                                    "Status",
                                    "Action",
                                  ];
                                  final textSubheads = [
                                    "${tpcReqAmt[index].customerName}",
                                    "${tpcReqAmt[index].orderNumber}",
                                    "${tpcReqAmt[index].totalAmount}",
                                    "${tpcReqAmt[index].level2TpcApprovedAmount}",
                                    "${tpcReqAmt[index].tpcStatus}",
                                    "View",
                                  ];
                                  return Container(

                                    padding: EdgeInsets.symmetric(
                                      vertical: 6,
                                      horizontal: 0,
                                    ),
                                    child: Row(
                                      mainAxisAlignment:
                                      MainAxisAlignment.start,
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [

                                        Expanded(
                                          child: Text(textheads[j].toString(),
                                            maxLines: 2,
                                            overflow: TextOverflow.ellipsis,),
                                        ),
                                        Expanded(
                                          child: InkResponse(
                                            onTap: () {
                                              if (textheads[j] == "Action") {
                                                Navigator.push(
                                                  context,
                                                  MaterialPageRoute(
                                                    builder:
                                                        (context) =>
                                                        Ordersdetailsbymodes(
                                                          pageTitleName: "CRM Order Details",
                                                          mode: widget.mode,
                                                          orderId: tpcReqAmt[index].orderId,
                                                        ),
                                                  ),
                                                );
                                              }
                                            },
                                            child: SizedBox(
                                              // height:45,
                                              width:
                                              MediaQuery.of(
                                                context,
                                              ).size.width *
                                                  0.75,
                                              child: Text(
                                                textSubheads[j],
                                                maxLines: 2,
                                                overflow:
                                                TextOverflow.ellipsis,
                                                style: TextStyle(
                                                  color:textheads[j] == "Action"? AppColors.app_blue:
                                                  Color(0xFF818181),
                                                  decoration:
                                                  textheads[j] == "Action"
                                                      ? TextDecoration.underline
                                                      : TextDecoration.none,
                                                  decorationColor:
                                                  textheads[j] == "Action"
                                                      ? AppColors.app_blue
                                                      : AppColors.white,
                                                ),
                                              ),
                                            ),
                                          ),
                                        ),

                                      ],
                                    ),
                                  );
                                }),
                              ),
                            );
                          }
                      ),
                    ],
                  ),
                ),
              ),

            ),
          ),

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




}
