import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:storemonitor/Models/EmployeesGraph.dart';
import 'package:storemonitor/Screens/DepartmentSearch.dart';
import '../Services/RemoteData.dart';
import 'Constants.dart';

class EmployeeStatus extends StatefulWidget {
  final String message;
  const EmployeeStatus({super.key, required this.message});

  //const Home({super.key});

  @override
  employeeStatus createState() => employeeStatus();
}

class employeeStatus extends State<EmployeeStatus> {
  Future<EmployeeGraph?>? _futureFoot;
  String? selectedDropdownItem;
  var isMeeters = '';
  List<String>? storesList;
  //final Color _buttonColor1 = const Color.fromARGB(255, 235, 229, 246);
  //final Color _buttonColor  = const Color.fromARGB(255, 235, 229, 246);
  Color color   = Colors.black;
  Color color1  = Colors.black;

  @override
  void initState() {
    super.initState();
    isMeeters = widget.message;
    loadData();
    //getData(selectedDropdownItem!);
  }

  // void _onButtonPressed1() {
  //   _futureFoot = getData(selectedDropdownItem!, 'TODAY');
  //   double screenWidth = MediaQuery.of(context).size.width;
  //   if (kDebugMode) {
  //     print('Screen Width : $screenWidth');
  //   }
  //   setState(() {
  //     _buttonColor1 = const Color.fromARGB(255, 38, 20, 70);
  //     _buttonColor = const Color.fromARGB(255, 235, 229, 246);
  //     color = Colors.black;
  //     color1 = Colors.white;
  //   });
  // }

  // void _onButtonPressed() {
  //   _futureFoot = getData(selectedDropdownItem!, 'YESTERDAY');
  //   setState(() {
  //     _buttonColor1 = const Color.fromARGB(255, 235, 229, 246);
  //     _buttonColor = const Color.fromARGB(255, 38, 20, 70);
  //     color = Colors.white;
  //     color1 = Colors.black;
  //   });
  // }

  Future<void> loadData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? userId = prefs.getString("stores_list");
    storesList = (json.decode(userId!) as List<dynamic>)
        .map((map) => map['name'] as String)
        .toList();
    int index = storesList!.indexWhere((item) => item.contains(isMeeters));
    setState(() {
      selectedDropdownItem = storesList![index];
      _futureFoot = getData(selectedDropdownItem!);
    });
  }

  Future<EmployeeGraph?> getData(String store) async {
    var response = await RemoteData().getEmployeesStatus(
      store,
    );
    Map<String, dynamic> responseMap = jsonDecode(response!.body);
    final result = EmployeeGraph.fromJson(responseMap);
    if (kDebugMode) {
      print(result.departmentlist);
    }
    return result;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          "EmployeesStatus",
          style: Constants.fontStyle,
        ),
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: Constants.getReturn(),
          ),
        ),
      ),
      body: FutureBuilder<EmployeeGraph?>(
        future: _futureFoot,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const Center(
                child: CircularProgressIndicator(
                    color: Color.fromARGB(255, 173, 57, 230)));
          } else if (snapshot.hasData) {
            final foot = snapshot.data!;
            return SingleChildScrollView(
              child: Container(
                  margin: const EdgeInsets.fromLTRB(15, 0, 15, 0),
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      const SizedBox(
                        width: 10,
                        height: 10,
                      ),
                      Container(
                        padding: const EdgeInsets.symmetric(horizontal: 10.0),
                        decoration: BoxDecoration(
                          border: Border.all(color: Colors.grey),
                          borderRadius: BorderRadius.circular(5.0),
                        ),
                        child: DropdownButtonHideUnderline(
                          child: DropdownButton<String>(
                            menuMaxHeight:
                                Constants.getDropdownItemHeight(context),
                            value: selectedDropdownItem,
                            onChanged: (newValue) {
                              setState(() {
                                selectedDropdownItem = newValue;
                                _futureFoot = getData(selectedDropdownItem!);
                              });
                            },
                            items: storesList?.map((item) {
                              return DropdownMenuItem<String>(
                                value: item,
                                child: Text(item),
                              );
                            }).toList(),
                            icon: const Icon(Icons.arrow_drop_down),
                            isExpanded: true,
                          ),
                        ),
                      ),
                      const SizedBox(
                        height: 10,
                        width: 1,
                      ),
                      // Row(
                      //   mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                      //   children: [
                      //     SizedBox(
                      //       width: MediaQuery.of(context).size.width / 2.3,
                      //       height: 50,
                      //       child: ElevatedButton(
                      //         onPressed: _onButtonPressed,
                      //         style: ElevatedButton.styleFrom(
                      //             backgroundColor: _buttonColor),
                      //         child: Text(
                      //           'YESTERDAY',
                      //           style: reviewbuttonsfontStyle(color),
                      //           overflow: TextOverflow.ellipsis,
                      //         ),
                      //       ),
                      //     ),
                      //     const SizedBox(
                      //       height: 10,
                      //       width: 5,
                      //     ),
                      //     SizedBox(
                      //       width: MediaQuery.of(context).size.width / 2.25,
                      //       height: 50,
                      //       child: ElevatedButton(
                      //         onPressed: _onButtonPressed1,
                      //         style: ElevatedButton.styleFrom(
                      //             backgroundColor: _buttonColor1),
                      //         child: Text(
                      //           'TODAY',
                      //           style: reviewbuttonsfontStyle(color1),
                      //           overflow: TextOverflow.ellipsis,
                      //         ),
                      //       ),
                      //     ),
                      //   ],
                      // ),
                      const SizedBox(
                        height: 15,
                        width: 1,
                      ),
                      const Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          Text('Employees InStore Status',
                              textAlign: TextAlign.left,
                              style: Constants.headerfontStyle),
                        ],
                      ),
                      const SizedBox(
                        width: 0,
                        height: 10,
                      ),
                      Center(
                        child: SizedBox(
                          width: MediaQuery.of(context).size.width,
                          height: 450,
                          child: InAppWebView(
                            initialData: InAppWebViewInitialData(
                                data: foot.eMPLOYEEGRAPH ?? ""),
                            initialOptions: InAppWebViewGroupOptions(
                              crossPlatform: InAppWebViewOptions(
                                  // enable other options if needed
                                  //supportZoom: false,
                                  ),
                            ),
                          ),
                        ),
                      ),

                      const SizedBox(
                        width: 1,
                        height: 10,
                      ),

                      Container(
                        decoration: BoxDecoration(
                          gradient: const LinearGradient(
                            colors: [Color(0xFF119887), Color(0xFF2bca83)],
                            begin: Alignment.topLeft,
                            end: Alignment.bottomRight,
                          ),
                          borderRadius: BorderRadius.circular(5),
                        ),
                        child: ElevatedButton(
                          onPressed: () {
                            // Navigator.push(
                            //   context,
                            //   MaterialPageRoute(builder: (context) => Home()),
                            // );
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => DepartmentSearch(
                                      message: selectedDropdownItem.toString(),
                                      list: foot.departmentlist)),
                            );
                            //print(foot.departmentlist);
                          },
                          style: ElevatedButton.styleFrom(
                            backgroundColor: Colors
                                .transparent, // make the button transparent
                            elevation: 0, // remove the button's shadow
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(10),
                            ),
                          ),
                          child: const SizedBox(
                            height: 50,
                            child: Center(
                              child: Text(
                                'Department Search',
                                style: Constants.buttonsfontStyle,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  )),
            );
          } else if (snapshot.hasError) {
            return Center(child: Text("Error: ${snapshot.error}"));
          } else {
            return const Center(child: Text("No data"));
          }
        },
      ),
    );
  }

  TextStyle reviewbuttonsfontStyle(Color color) {
    return TextStyle(
      fontFamily: 'Poppins',
      fontSize: 14,
      fontWeight: FontWeight.normal,
      color: color,
    );
  }
}
