import 'dart:io';

import 'package:flutter/material.dart';
import 'package:generp/Utils/app_colors.dart';
import 'package:generp/Utils/commonWidgets.dart';
import 'package:generp/screens/finance/FileViewer.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 SafeArea(
          top: false,
          bottom: Platform.isIOS?false:true,
          child: Scaffold(
            resizeToAvoidBottomInset: true,
            appBar: appbar(context, "Details"),
            backgroundColor: AppColors.scaffold_bg_color,
            body: 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(6, (j) {
                        final headings = [
                          "ID",
                          "Date",
                          "Account",
                          "Ref Type",
                          "Payment Remarks",
                          "Attachment"
                        ];

                        final subHeadings = [
                          details.id ?? "-",
                          details.createdDatetime ?? "-",
                          details.accountName ?? "-",
                          details.refType ?? "-",
                          details.paymentRemarks ?? "-",
                          "View",
                        ];
                        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),
                              if(subHeadings[j] == "View")...[
                                Expanded(
                                  child: InkResponse(
                                    onTap: () async {
                                      Navigator.push(context, MaterialPageRoute(
                                        builder: (context) =>
                                            Fileviewer(fileName: details
                                                .attachmentViewFileName ?? "",
                                                fileUrl: details
                                                    .attachmentDirFilePath ?? "")
                                        ,));
                                    },
                                    child: Text(
                                      "${subHeadings[j]}" == ""
                                          ? "-"
                                          : subHeadings[j] ?? "-",
                                      style: TextStyle(
                                          fontFamily: "JakartaMedium",
                                          fontSize: 14,
                                          color: AppColors.app_blue,
                                          decorationColor: AppColors.app_blue,
                                          decoration: TextDecoration.underline
                                      ),
                                    ),
                                  ),
                                ),
                              ] else
                                ...[
                                  Expanded(
                                    child: Text(
                                      "${subHeadings[j]}" == ""
                                          ? "-"
                                          : subHeadings[j] ?? "-",
                                      style: TextStyle(
                                        fontFamily: "JakartaMedium",
                                        fontSize: 14,
                                        color: AppColors.grey_thick,
                                      ),
                                    ),
                                  ),
                                ]

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