import 'package:flutter/material.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 {
  final genHashID;

  const Complainthistory({super.key, required this.genHashID});

  @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,
        widget.genHashID,
      );
    });
  }

  @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: SafeArea(
              child: Container(
                child: SingleChildScrollView(
                  child: Column(
                    children: [
                      if (provider.complaintList.isNotEmpty) ...[
                        ListView.builder(
                          shrinkWrap: true,
                          physics: NeverScrollableScrollPhysics(),
                          itemCount: provider.complaintList.length,
                          itemBuilder: (context, l) {
                            return Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Padding(
                                  padding: const EdgeInsets.only(left: 8.0,top: 5.0),
                                  child: Text(
                                    "${provider.complaintList[l].compRegdate}",
                                    style: TextStyle(
                                        fontSize:14,
                                        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, (jndex) {
                                        final headings = [
                                          "Technician Name",
                                          "Complaint Type",
                                          "Complaint Status",
                                          "Complaint Created By",
                                        ];

                                        final values = [
                                          provider.complaintList[l].techName,
                                          provider.complaintList[l].compType,
                                          provider.complaintList[l].compStatus,
                                          provider.complaintList[l].createdBy,
                                        ];
                                        return Container(
                                          padding: EdgeInsets.symmetric(
                                            horizontal: 15,
                                            vertical: 6,
                                          ),
                                          child: Row(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: [
                                              Expanded(
                                                child: SizedBox(
                                                  child: Text(
                                                    "${headings[jndex]}",
                                                    style: TextStyle(
                                                      fontSize: 14
                                                    ),
                                                  ),
                                                ),
                                              ),
                                              Expanded(
                                                child: SizedBox(
                                                  child: Text(
                                                    "${values[jndex]}",
                                                    style: TextStyle(
                                                      fontSize:14,
                                                      color: Color(

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