import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:generp/Notifiers/ServiceEngineerDashboardProvider.dart';
import 'package:generp/Utils/app_colors.dart';
import 'package:generp/Utils/commonWidgets.dart';
import 'package:generp/screens/serviceEngineer/MonthlyCollection.dart';
import 'package:generp/screens/serviceEngineer/PCWallet.dart';
import 'package:generp/screens/serviceEngineer/PendingComplaints.dart';
import 'package:generp/screens/serviceEngineer/TodayMontlyVisists.dart';
import 'package:provider/provider.dart';

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

  @override
  State<Serviceengineerdashboard> createState() =>
      _ServiceengineerdashboardState();
}

class _ServiceengineerdashboardState extends State<Serviceengineerdashboard> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      var serviceProvider = Provider.of<Serviceengineerdashboardprovider>(
        context,
        listen: false,
      );
      serviceProvider.LoadTechnicianDashboard(context);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Serviceengineerdashboardprovider>(
      builder: (context, provider, child) {
        return Scaffold(
          backgroundColor: AppColors.scaffold_bg_color,
          appBar: AppBar(
            automaticallyImplyLeading: false,
            elevation: 2.0,
            title: SizedBox(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  InkResponse(
                    onTap: () => Navigator.pop(context, true),
                    child: SvgPicture.asset(
                      "assets/svg/app_bar_back.svg",
                      height: 25,
                    ),
                  ),
                  Text(
                    "Service Engineer",
                    style: TextStyle(
                      fontSize: 16,
                      height: 1.1,
                      color: AppColors.semi_black,
                    ),
                  ),
                  Spacer(),
                  Container(
                    height: 25,
                    width: 45,
                    decoration: BoxDecoration(
                      color: Color(0xFFFFF6E2),
                      borderRadius: BorderRadius.circular(20),
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        SvgPicture.asset(
                          'assets/svg/engineer_rating.svg',
                          height: 15,
                        ),
                        SizedBox(width: 5),
                        Text(
                          provider.avgRating.toString(),
                          style: TextStyle(fontSize: 14),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.vertical(
                bottom: Radius.circular(30), // Adjust the radius as needed
              ),
            ),
          ),
          body: Container(
            child: SingleChildScrollView(
              child: Column(
                children: [
                  Container(
                    margin: EdgeInsets.only(top: 0, bottom: 15),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.vertical(
                        bottom: Radius.circular(16),
                      ),
                    ),
                    child: Column(
                      children: [
                        InkResponse(
                          onTap: () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) => Pendingcomplaints(),
                              ),
                            );
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(
                              horizontal: 10,
                              vertical: 15,
                            ),
                            margin: EdgeInsets.symmetric(
                              horizontal: 10,
                              vertical: 15,
                            ),
                            decoration: BoxDecoration(
                              color: Color(0xFFFFEFEF),
                              borderRadius: BorderRadius.circular(16),
                            ),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Expanded(
                                  child: SizedBox(
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.center,
                                      children: [
                                        Text(
                                          "${provider.pendingComplaints}",
                                          style: TextStyle(
                                            fontSize: 30,
                                            color: Color(0xFFED3424),
                                          ),
                                        ),
                                        Text("Pending Complaints"),
                                      ],
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.only(left: 15),
                    alignment: Alignment.topLeft,
                    child: Text(
                      "Collections & Visits",
                      style: TextStyle(color: Color(0xFF818181), fontSize: 14),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.symmetric(horizontal: 10, vertical: 15),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(16),
                    ),

                    child: GridView.builder(
                      padding: EdgeInsets.symmetric(
                        vertical: 15,
                        horizontal: 10,
                      ),
                      itemCount: 4,
                      shrinkWrap: true,
                      physics: NeverScrollableScrollPhysics(),
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2,
                        crossAxisSpacing: 10,
                        mainAxisSpacing: 10,
                        childAspectRatio: 22 / 10,
                      ),
                      itemBuilder: (context, index) {
                        final numbers = [
                          provider.monthlyPaymentCollectionAmount,
                          provider.paymentCollectionWalletBalanceAmount,
                          provider.thisMonthsVisits,
                          provider.todayVisits,
                        ];
                        final names = [
                          "Month Collection",
                          "P.C. Wallet",
                          "Month Visits",
                          "Today Visits",
                        ];
                        final List<int> colorCodes = [
                          0xFFEEFCFF,
                          0xFFE7FFE5,
                          0xFFF3EDFF,
                          0xFFFFF6F0,
                        ];
                        final List<int> textColorCodes = [
                          0xFF1487C9,
                          0xFF0D9C00,
                          0xFF493272,
                          0xFF91481B,
                        ];
                        return InkResponse(
                          onTap: () {
                            var route;
                            switch (index) {
                              case 0:
                                route = Monthlycollection();
                                break;
                              case 1:
                                route = Pcwallet();
                                break;
                              case 2:
                                route = Todaymontlyvisists(visitType: "Month");
                                break;
                              case 3:
                                route = Todaymontlyvisists(visitType: "Today");
                                break;
                            }

                            Navigator.push(
                              context,
                              MaterialPageRoute(builder: (context) => route),
                            );
                          },
                          child: Container(
                            padding: EdgeInsets.only(
                              left: 15,
                              top: 5,
                              bottom: 5,
                            ),
                            decoration: BoxDecoration(
                              color: Color(colorCodes[index]),
                              borderRadius: BorderRadius.circular(20),
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text(
                                  numbers[index].toString(),
                                  style: TextStyle(
                                    fontSize: 30,
                                    color: Color(textColorCodes[index]),
                                  ),
                                ),
                                Text(names[index]),
                              ],
                            ),
                          ),
                        );
                      },
                    ),
                  ),
                  Container(
                    height: 60,
                    margin: EdgeInsets.symmetric(horizontal: 10, vertical: 0),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(20),
                    ),

                    child: Row(
                      children: [
                        Expanded(
                          flex: 1,
                          child: SvgPicture.asset(
                            "assets/svg/find_generator.svg",
                          ),
                        ),
                        Expanded(
                          flex: 4,
                          child: Text("Find Nearby Generators"),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
