import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:generp/Notifiers/PaymentCollectionProvider.dart';
import 'package:generp/screens/WebERPScreen.dart';
import 'package:provider/provider.dart';

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

class Monthlycollection extends StatefulWidget {
  const Monthlycollection({super.key});

  @override
  State<Monthlycollection> createState() => _MonthlycollectionState();
}

class _MonthlycollectionState extends State<Monthlycollection> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      var provider = Provider.of<Paymentcollectionprovider>(
        context,
        listen: false,
      );
      provider.PaymentCollectionAPI(context);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Paymentcollectionprovider>(
      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(
                    "Payment Collection",
                    style: TextStyle(
                      fontSize: 16,
                      height: 1.1,
                      color: AppColors.semi_black,
                    ),
                  ),
                  Spacer(),
                  InkResponse(
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) => Accountsuggestions(),
                        ),
                      );
                    },
                    child: 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.paymenCollectionList.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.paymenCollectionList[index].accountName}",
                              ),
                              // Text("Gen Pvt. Ltd"),
                            ],
                          ),
                          Spacer(),
                          Expanded(
                            flex: 2,
                            child: Text(
                              "₹${provider.paymenCollectionList[index].amount}",
                              style: TextStyle(color: AppColors.app_blue),
                            ),
                          ),
                          // Expanded(
                          //   flex: 1,
                          //   child: InkResponse(
                          //     onTap: () {
                          //       _showOptionsSheet(context);
                          //     },
                          //     child: Icon(Icons.more_vert),
                          //   ),
                          // ),
                        ],
                      ),
                      SizedBox(height: 10),
                      ...List.generate(2, (j) {
                        final textheads = ["ID", "Generator Type"];
                        final textSubheads = [
                          provider.paymenCollectionList[index].paymentRefNo,
                          provider.paymenCollectionList[index].paymentMode,
                        ];
                        return Container(
                          padding: EdgeInsets.symmetric(vertical: 3),
                          child: Column(
                            children: [
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Text(textheads[j].toString()),
                                      Text(textSubheads[j].toString()),
                                    ],
                                  ),
                                  if (j == 1)
                                    Container(
                                      height: 30,
                                      padding: EdgeInsets.symmetric(
                                        horizontal: 10,
                                      ),
                                      decoration: BoxDecoration(
                                        color: Color(0xFFE3FFE0),
                                        borderRadius: BorderRadius.circular(8),
                                      ),
                                      child: Center(
                                        child: Text(
                                          "${provider.paymenCollectionList[index].approvalStatus}",
                                          style: TextStyle(
                                            color: Color(0xFF0D9C00),
                                          ),
                                        ),
                                      ),
                                    ),
                                ],
                              ),
                              if (j == 0)
                                Divider(
                                  thickness: 0.5,
                                  color: Color(0xFFD7D7D7),
                                ),
                            ],
                          ),
                        );
                      }),
                      SizedBox(height: 10),
                      InkResponse(
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder:
                                  (context) => WebErpScreen(
                                    erp_url:
                                        provider
                                            .paymenCollectionList[index]
                                            .paymentProofFilePath ??
                                        "",
                                  ),
                            ),
                          );
                        },
                        child: SizedBox(
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Text(
                                "View Attachment",
                                style: TextStyle(color: AppColors.app_blue),
                              ),
                              SizedBox(width: 5),
                              SvgPicture.asset("assets/svg/next_button.svg"),
                            ],
                          ),
                        ),
                      ),
                    ],
                  ),
                );
              },
            ),
          ),
        );
      },
    );
  }
}
