import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:generp/Notifiers/VisitDetailsProvider.dart';
import 'package:generp/Utils/app_colors.dart';
import 'package:generp/Utils/commonWidgets.dart';
import 'package:provider/provider.dart';

import 'Followupdetails.dart';

class Visitdetails extends StatefulWidget {
  final complaintID;

  const Visitdetails({super.key, required this.complaintID});

  @override
  State<Visitdetails> createState() => _VisitdetailsState();
}

class _VisitdetailsState extends State<Visitdetails> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      var visitdetails = Provider.of<Visitdetailsprovider>(
        context,
        listen: false,
      );
      visitdetails.LoadVisitDetailsAPI(context, widget.complaintID);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Visitdetailsprovider>(
      builder: (context, provider, child) {
        var customerDetails = provider.customerDetails;
        var generatorDetails = provider.generatorDetails;
        var complaintDetails = provider.complaintDetailsNew;
        return WillPopScope(
          onWillPop: () => onBackPressed(context),
          child: SafeArea(
            top: false,
            bottom: Platform.isIOS?false:true,
            child: Scaffold(
              resizeToAvoidBottomInset: true,
              appBar: appbar(context, "Visit Details"),
              backgroundColor: AppColors.scaffold_bg_color,
              body: SizedBox(
                child: SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 8),
                        child: Text("Customer Details",style: TextStyle(
                                            fontSize: 14,
                                            color: AppColors.grey_thick,
                                          ),),
                      ),
                      Container(
                        margin: EdgeInsets.symmetric(
                          horizontal: 10,
                          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(4, (j) {
                            final textheads = [
                              "${customerDetails.cname}",
                              "Mobile Number",
                              "Contact Person Number",
                              "Mail ID",
                            ];
                            final textSubheads = [
                              customerDetails.aname ?? "-",
                              customerDetails.mob1 ?? "-",
                              customerDetails.mob2 ?? "-",
                              customerDetails.mail ?? "-",
                            ];
                            return Container(
                              padding: EdgeInsets.symmetric(
                                vertical: 6,
                                horizontal: 0,
                              ),
                              child: Column(
                                children: [
                                  Row(
                                    mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    children: [
                                      if (j == 0) ...[
                                        SvgPicture.asset(
                                          "assets/svg/se_block_head.svg",
                                        ),
                                        SizedBox(width: 5),
                                        Expanded(
                                          child: SizedBox(
                                            child: Column(
                                              crossAxisAlignment:
                                              CrossAxisAlignment.start,
                                              children: [
                                                Text(
                                                  "${customerDetails.cname}",
                                                  maxLines: 2,
                                                  overflow: TextOverflow.ellipsis,
                                                ),
                                                SizedBox(
                                                  // height:45,
                                                  width:
                                                  MediaQuery.of(
                                                    context,
                                                  ).size.width *
                                                      0.75,
                                                  child: Text(
                                                    textSubheads[j] == ""
                                                        ? "-"
                                                        : customerDetails.aname ?? "-",
                                                    maxLines: 2,
                                                    overflow: TextOverflow.ellipsis,
                                                    style: TextStyle(
                                                      color: Color(0xFF818181),
                                                    ),
                                                  ),
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),
                                      ]else...[
                                        Expanded(
                                          child: Text(textheads[j].toString()),
                                        ),
                                        Expanded(
                                          child: SizedBox(
                                            child: Text(
                                              textSubheads[j] == ""
                                                  ? "-"
                                                  : textSubheads[j],
                                              maxLines: 2,
                                              overflow: TextOverflow.ellipsis,
                                              style: TextStyle(
                                                color: Color(0xFF818181),
                                              ),
                                            ),
                                          ),
                                        ),
                                      ],

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

                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 8),
                        child: Text("Generator Details",style: TextStyle(
                          fontSize: 14,
                          color: AppColors.grey_thick,
                        ),),
                      ),
                      Container(
                        margin: EdgeInsets.symmetric(
                          horizontal: 10,
                          vertical: 5,
                        ),
                        padding: EdgeInsets.symmetric(
                          horizontal: 15,
                          vertical: 10,
                        ),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(20),
                        ),
                        child: Column(
                          children: [
                            ...List.generate(6, (j) {
                              final textheads = [
                                "Gen ID",
                                "Engine Number",
                                "Address",
                                "Product Name",
                                "Engine Model",
                                "Date Of Supply",
                              ];
                              final textSubheads = [
                                "${generatorDetails.genHashId}",
                                "${generatorDetails.engineNo}",
                                "${generatorDetails.address}",
                                "${generatorDetails.spname}",
                                "${generatorDetails.engineModel}",
                                "${complaintDetails.dateOfSupply}",
                              ];
                              return Container(
                                padding: EdgeInsets.symmetric(
                                  vertical: 6,
                                  horizontal: 0,
                                ),
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Expanded(
                                      child: Text(textheads[j].toString()),
                                    ),
                                    Expanded(
                                      child: SizedBox(
                                        child: Text(
                                          textSubheads[j] == ""
                                              ? "-"
                                              : textSubheads[j],
                                          maxLines: 2,
                                          overflow: TextOverflow.ellipsis,
                                          style: TextStyle(
                                            color: Color(0xFF818181),
                                          ),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              );
                            }),
                          ],
                        ),
                      ),

                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 8),
                        child: Text(
                          "Complaint Details",
                          style: TextStyle(
                            fontSize: 14,
                            color: AppColors.grey_thick,
                          ),
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.symmetric(
                          horizontal: 10,
                          vertical: 5,
                        ),
                        padding: EdgeInsets.symmetric(
                          horizontal: 15,
                          vertical: 10,
                        ),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(20),
                        ),
                        child: Column(
                          children: [
                            ...List.generate(4, (j) {
                              final textheads = [
                                "Complaint ID",
                                "Complaint Description",
                                "Opened Date",
                                "Complaint Type",
                              ];
                              final textSubheads = [
                                "${complaintDetails.complaintId}",
                                "${complaintDetails.complaintDesc}",
                                "${complaintDetails.openedDate}",
                                "${complaintDetails.complaintType}",
                              ];
                              return Container(
                                padding: EdgeInsets.symmetric(vertical: 6),
                                child: Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Expanded(
                                      child: Text(textheads[j].toString()),
                                    ),
                                    Expanded(
                                      child: SizedBox(
                                        // height:45,
                                        // width:
                                        // MediaQuery.of(
                                        //   context,
                                        // ).size.width *
                                        //     0.8,
                                        child: Text(
                                          textSubheads[j] == ""
                                              ? "-"
                                              : textSubheads[j],
                                          maxLines: 2,
                                          overflow: TextOverflow.ellipsis,
                                          style: TextStyle(
                                            color: Color(0xFF818181),
                                          ),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              );
                            }),
                          ],
                        ),
                      ),

                      SizedBox(height: 75),
                    ],
                  ),
                ),
              ),
              floatingActionButton: Align(
                alignment: Alignment.bottomCenter,
                child: InkWell(
                  onTap: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder:
                            (context) =>
                                Followupdetails(complaintID: widget.complaintID),
                      ),
                    );
                  },
                  child: Container(
                    alignment: Alignment.bottomCenter,
                    height: 45,
                    width: MediaQuery.of(context).size.width,
                    margin: EdgeInsets.symmetric(horizontal: 10),
                    decoration: BoxDecoration(
                      color: AppColors.app_blue,
                      borderRadius: BorderRadius.circular(14.0),
                    ),
                    child: Center(
                      child: Text(
                        "Followup Details",
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          color: Colors.white,
                          fontFamily: "JakartaMedium",
                        ),
                      ),
                    ),
                  ),
                ),
              ),
              floatingActionButtonLocation:
                  FloatingActionButtonLocation.centerFloat,
            ),
          ),
        );
      },
    );
  }
}
