import 'package:flutter/material.dart';
import 'package:generp/Utils/app_colors.dart';
import 'package:generp/Utils/commonWidgets.dart';
import 'package:provider/provider.dart';

import '../../Notifiers/financeProvider/paymentRequisitionPaymentsListProvider.dart';

class Transactiondetails extends StatefulWidget {
  final paymentID;
  final type;

  const Transactiondetails({super.key, this.paymentID, this.type});

  @override
  State<Transactiondetails> createState() => _TransactiondetailsState();
}

class _TransactiondetailsState extends State<Transactiondetails> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      var provider = Provider.of<Paymentrequisitionpaymentslistprovider>(
        context,
        listen: false,
      );
      provider.paymentsListDetailsAPI(context, widget.paymentID);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Paymentrequisitionpaymentslistprovider>(
      builder: (context, provider, child) {
        var details = provider.paymentDetails;
        return Scaffold(
          resizeToAvoidBottomInset: true,
          appBar: appbar(context, "Details"),
          backgroundColor: AppColors.scaffold_bg_color,
          body: SafeArea(
            child: Container(
              margin: EdgeInsets.symmetric(vertical: 10,horizontal: 10),
              child: SingleChildScrollView(
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                  margin: EdgeInsets.symmetric(vertical: 5),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(16),
                  ),
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Expanded(
                            flex: 1,
                            child: SizedBox(
                              child:
                                  widget.type == "Credit"
                                      ? Image.asset(
                                        "assets/images/trans_debit.png",
                                        height: 45,
                                        width: 45,
                                        fit: BoxFit.contain,
                                      )
                                      : Image.asset(
                                        "assets/images/trans_credit.png",
                                        height: 45,
                                        width: 45,
                                        fit: BoxFit.contain,
                                      ),
                            ),
                          ),
                          SizedBox(width: 10),
                          Expanded(
                            flex: 5,
                            child: SizedBox(
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(
                                    widget.type == "Credit"
                                        ? "Debited Amount"
                                        : "Credited Amount",
                                    maxLines: 2,
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(
                                      fontFamily: "JakartaMedium",
                                      fontSize: 12,
                                    ),
                                  ),
                                  RichText(
                                    textAlign: TextAlign.right,
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                    text: TextSpan(
                                      children: [
                                        TextSpan(
                                          text:
                                              widget.type == "Credit" ? "-" : "+",
                                          style: TextStyle(
                                            color: Color(0xFF2D2D2D),
                                            fontSize: 13,
                                            fontFamily: "JakartaRegular",
                                          ),
                                        ),
                                        TextSpan(
                                          text: "₹${details.amount}",
                                          style: TextStyle(
                                            color: Color(0xFF2D2D2D),
                                            fontSize: 13,
                                            fontFamily: "JakartaMedium",
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ],
                      ),
                      Divider(thickness: 0.5, color: Color(0xFFd7d7d7)),
                      ...List.generate(5, (j) {
                        final headings = [
                          "ID",
                          "Date",
                          "Account",
                          "Ref Type",
                          "Description",
                        ];
            
                        final subHeadings = [
                          details.id ?? "-",
                          details.createdDatetime ?? "-",
                          details.accountName ?? "-",
                          details.refType ?? "-",
                          details.description ?? "-",
                        ];
                        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,
                                  ),
                                ),
                              ),
                              SizedBox(width: 15),
                              Expanded(
                                child: Text(
                                  subHeadings[j] ?? "-",
                                  style: TextStyle(
                                    fontFamily: "JakartaMedium",
                                    fontSize: 14,
                                    color: AppColors.grey_thick,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        );
                      }),
                    ],
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
