import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.dart';

import '../../Notifiers/GeneratorDetailsProvider.dart';
import '../../Notifiers/HomeScreenNotifier.dart';
import '../../Utils/app_colors.dart';
import '../../Utils/commonWidgets.dart';

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

  @override
  State<Complainthistory> createState() => _ComplainthistoryState();
}

class _ComplainthistoryState extends State<Complainthistory> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      var details = Provider.of<Generatordetailsprovider>(
        context,
        listen: false,
      );
      var homeProvider = Provider.of<HomescreenNotifier>(context,listen: false);
      details.LoadgeneratorComplaintHistoryApifunction(homeProvider,context, details.genID);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Generatordetailsprovider>(
      builder: (context, provider, child) {

        return WillPopScope(
          onWillPop: () => onBackPressed(context),
          child: Scaffold(
            resizeToAvoidBottomInset: true,
            appBar: appbar(context, "Complaint History"),
            backgroundColor: AppColors.scaffold_bg_color,
            body: Container(
              child: SingleChildScrollView(
                child: Column(

                  children: [
                    if(provider.complaintList.isNotEmpty)...[
                    ListView.builder(
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      itemCount: provider.complaintList.length,
                      itemBuilder: (context, index) {
                      return Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text("${ provider.complaintList[index].compRegdate}",style: TextStyle(
                            color: Color(0xFF818181)
                          ),),
                          Container(
                            margin: EdgeInsets.symmetric(horizontal: 10,vertical: 5),
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(16),

                            ),
                            child: Column(
                              children: [

                                ...List.generate(4, (index) {
                                  final headings = [
                                    "Technician Name",
                                    "ID",
                                    "Complaint Type",
                                    "Complaint Status",];

                                  final values = [
                                    provider.complaintList[index].techName,
                                    provider.complaintList[index].compId,
                                    provider.complaintList[index].compType,
                                    provider.complaintList[index].compStatus,

                                  ];
                                  return Container(

                                    padding: EdgeInsets.symmetric(horizontal: 15,vertical: 15),
                                    child: Row(
                                      children: [
                                        Expanded(
                                          child: SizedBox(
                                            child: Column(
                                              crossAxisAlignment: CrossAxisAlignment.start,
                                              mainAxisAlignment: MainAxisAlignment.start,
                                              children: [
                                                Text("${headings[index]}"),

                                              ],
                                            ),
                                          ),
                                        ),
                                        Expanded(
                                          child: SizedBox(
                                            child: Column(
                                              crossAxisAlignment: CrossAxisAlignment.start,
                                              mainAxisAlignment: MainAxisAlignment.start,
                                              children: [
                                                Text("${values[index]}",
                                                style: TextStyle(
                                                    color: Color(0xFF818181)
                                                ),),

                                              ],
                                            ),
                                          ),
                                        ),
                                      ],
                                    ),
                                  );
                                },),
                              ]
                            ),
                          ),
                        ],
                      );
                    },),
                    ]else...[
                      Emptywidget(context)
                    ]
                  ],
                ),
              ),
            ),

          ),
        );
      },
    );
  }
}
