import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:storemonitor/Models/chartdata.dart';
import 'package:storemonitor/Screens/EmployeeStatus.dart';
import 'package:storemonitor/Screens/IssueVideos.dart';
import 'package:storemonitor/Screens/MyHomePage.dart';
import 'package:storemonitor/Screens/NavBar.dart';
import 'package:intl/intl.dart';
import 'package:storemonitor/Services/RemoteData.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:toggle_switch/toggle_switch.dart';
import '../Models/Store.dart';
import 'Constants.dart';
import 'FootFall.dart';
import 'NotificationReport.dart';
import 'Review.dart';
import 'UserRegistration.dart';

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

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

class _Home extends State<Home> {
  String? selectedRegion;
  String? selectedDropdownItem = '';
  String? empId;
  List<String>? namesList;
  List<String> storeNames = [];
  List<Store> stores      = [];
  List<String> regions    = [];
  late List<GDPData> _chartData;
  DateTime _selectedDate = DateTime.now();
  final TextEditingController _dateTime = TextEditingController();
  bool isAuthenticated = false;
  bool iswarehouserelated = false;
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  final _controller = TextEditingController();
  List<Insight>? insightslist = [];
  bool isSwitched = false;

  @override
  void initState() {
    super.initState();
    loadData();
    _chartData = getChartData(0.0, 0.0);
    _selectedDate = DateTime.now();
    _dateTime.text = DateFormat('yyyy/MM/dd').format(_selectedDate);
  }

  loadData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? userId = prefs.getString("stores_list");
    empId = prefs.getString('empId');
    if (kDebugMode) {
      print(empId);
    }

    final List<dynamic> jsonResponse = json.decode(userId!);
    if (kDebugMode) {
      print(jsonResponse);
    }
    stores = jsonResponse.map((data) => Store.fromJson(data)).toList();
    if (kDebugMode) {
      print(stores);
    }
    // Get unique regions
    regions = stores.map((store) => store.region).toSet().toList();
    selectedRegion = regions[0];
    updateStoreNames(selectedRegion!);
    setState(() {});

    //namesList = (json.decode(userId!) as List<dynamic>)
    //.map((map) => map['name'] as String)
    //.toList();

    selectedDropdownItem = storeNames[0];
    getupdateData(selectedDropdownItem);
    if (prefs.getString('access') == 'ADMIN' ||
        prefs.getString('access') == 'MANAGER') {
      setState(() {
        isAuthenticated = true;
      });
    } else {
      setState(() {
        isAuthenticated = false;
      });
    }
    if (selectedDropdownItem == 'WH KISMATPUR' ||
        selectedDropdownItem == 'WH BANGALORE' ||
        selectedDropdownItem == 'WH VIJAYAWADA' ||
        selectedDropdownItem == 'WH KANCHI') {
      iswarehouserelated = false;
    } else {
      iswarehouserelated = false;
    }
  }

  Future<void> _selectDate(BuildContext context) async {
    final DateTime? picked = await showDatePicker(
        context: context,
        initialDate: _selectedDate,
        firstDate: DateTime(1900),
        lastDate: DateTime.now(),
        builder: (context, child) {
          return Theme(
            data: Theme.of(context).copyWith(
              colorScheme: const ColorScheme.light(
                primary: Color.fromARGB(255, 125, 50, 253),
                onPrimary: Colors.white,
                onSurface: Colors.black,
              ),
              textButtonTheme: TextButtonThemeData(
                style: TextButton.styleFrom(
                  foregroundColor: const Color.fromARGB(255, 125, 50, 253),
                ),
              ),
            ),
            child: child!,
          );
        });
    setState(() {
      _selectedDate = picked!;
      _dateTime.text =
          '${_selectedDate.year}/${_selectedDate.month}/${_selectedDate.day}';
      getupdateData(selectedDropdownItem);
    });
  }

  void updateStoreNames(String region) {
    storeNames = stores
        .where((store) => store.region == region)
        .map((store) => store.storename)
        .toList();
    selectedDropdownItem = storeNames[0]; // Reset the selected store

    setState(() {
      getupdateData(selectedDropdownItem);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      drawer: NavBar(
        message: selectedDropdownItem.toString(),
        storeNames: storeNames.toString(),
      ),
      appBar: AppBar(
        leading: IconButton(
          icon: Image.asset(
            "assets/images/menu.png",
            width: 24,
            height: 24,
            color: Colors.white,
          ),
          onPressed: () => _scaffoldKey.currentState?.openDrawer(),
        ),
        title: const Text(
          'STORE MONITOR',
          style: Constants.fontStyle,
        ),
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: Constants.getReturn(),
          ),
        ),
      ),
      body: SingleChildScrollView(
        child: GestureDetector(
          onForcePressUpdate: (details) {
            if (kDebugMode) {
              print('force press update');
            }
          },
          onHorizontalDragStart: (details) {
            if (kDebugMode) {
              print('force press start');
            }
          },
          onHorizontalDragDown: (details) {
            if (kDebugMode) {
              print('force press down');
            }
            //Navigator.pop(context);
          },
          child: Container(
            margin: const EdgeInsets.all(12.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                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: selectedRegion,
                      onChanged: (newValue) {
                        setState(() {
                          selectedRegion = newValue;
                          updateStoreNames(newValue!);
                        });
                      },
                      items: regions.map((region) {
                        return DropdownMenuItem<String>(
                          value: region,
                          child: Text(region),
                        );
                      }).toList(),
                      icon: const Icon(Icons.arrow_drop_down),
                      isExpanded: true,
                    ),
                  ),
                ),
                const SizedBox(
                  height: 15,
                  width: 1,
                ),
                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;
                          getupdateData(selectedDropdownItem);
                          // if (selectedDropdownItem == 'WH KISMATPUR' ||
                          //     selectedDropdownItem == 'WH BANGALORE' ||
                          //     selectedDropdownItem == 'WH VIJAYAWADA' ||
                          //     selectedDropdownItem == 'WH KANCHI') {
                          //   iswarehouserelated = true;
                          // } else {
                          //   iswarehouserelated = false;
                          // }
                        });
                      },
                      items: storeNames.map((item) {
                        return DropdownMenuItem<String>(
                          value: item,
                          child: Text(item),
                        );
                      }).toList(),
                      icon: const Icon(Icons.arrow_drop_down),
                      isExpanded: true,
                    ),
                  ),
                ),
                const SizedBox(
                  height: 15,
                  width: 1,
                ),
                TextFormField(
                  controller: _dateTime,
                  readOnly: true,
                  onTap: () {},
                  decoration: InputDecoration(
                    contentPadding: const EdgeInsets.symmetric(
                        vertical: 15, horizontal: 10),
                    isDense: true,
                    hintText: 'Select date',
                    border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(5.0),
                    ),
                    suffixIcon: IconButton(
                      icon: Image.asset(
                        'assets/images/date_icon.png',
                        width: 24,
                        height: 24,
                      ),
                      onPressed: () => _selectDate(context),
                    ),
                  ),
                ),
                const SizedBox(height: 10),
                const Text('AI Powered Insights',
                    textAlign: TextAlign.right,
                    style: Constants.headerfontStyle),
                const SizedBox(
                  width: 0,
                  height: 5,
                ),
                Container(
                  height: 2,
                  width: 60,
                  color: const Color.fromARGB(255, 170, 57, 243),
                ),
                const SizedBox(
                  height: 15,
                  width: 1,
                ),

                SizedBox(
                  height: MediaQuery.of(context).size.height * 0.15,
                  child: ListView.builder(
                    itemCount: insightslist?.length,
                    itemBuilder: (context, index) {
                      return Padding(
                        padding: const EdgeInsets.all(2.0),
                        child: Container(
                          height: 60,
                          color: index % 2 == 0
                              ? const Color.fromARGB(255, 245, 242, 251)
                              : Colors.white,
                          child: GestureDetector(
                            onTap: () {
                              showDialog(
                                context: context,
                                builder: (BuildContext context) {
                                  return AlertDialog(
                                    title: const Text('View Insights'),
                                    content: Text(
                                        Constants.capitalizeFirstLetter(
                                            insightslist![index].content)),
                                    actions: <Widget>[
                                      ToggleSwitch(
                                        minWidth: 90.0,
                                        minHeight: 50.0,
                                        cornerRadius: 20.0,
                                        activeBgColors: const [
                                          [Colors.green],
                                          [Colors.redAccent]
                                        ],
                                        activeFgColor: Colors.white,
                                        inactiveBgColor: Colors.grey,
                                        inactiveFgColor: Colors.white,
                                        initialLabelIndex: 0,
                                        totalSwitches: 2,
                                        labels: const ['YES', 'NO'],
                                        onToggle: (index) {
                                          if (kDebugMode) {
                                            print('Switched to: $index');
                                          }
                                        },
                                      ),
                                    ],
                                  );
                                },
                              );
                            },
                            child: Column(
                              children: [
                                const SizedBox(
                                  width: 5,
                                  height: 2,
                                ),
                                Padding(
                                  padding: const EdgeInsets.symmetric(
                                      vertical: 2.0, horizontal: 2.0),
                                  child: Row(children: [
                                    SizedBox(
                                      width: MediaQuery.of(context).size.width *
                                          0.68,
                                      child: Text(
                                        Constants.capitalizeFirstLetter(
                                            insightslist![index].heading),
                                        style: Constants.headerlistitems1,
                                        maxLines: 2,
                                        overflow: TextOverflow.ellipsis,
                                        textAlign: TextAlign.left,
                                      ),
                                    ),
                                    SizedBox(
                                      width: MediaQuery.of(context).size.width *
                                          0.22,
                                      child: IconButton(
                                        icon: const Icon(
                                          false
                                              ? Icons.visibility
                                              : Icons.visibility_off,
                                          // You can customize the color, size, and other properties of the icon as needed
                                        ),
                                        onPressed: () {},
                                      ),
                                    ),
                                  ]),
                                ),
                                // const VerticalDivider(
                                //   width: 1.0,
                                //   color: Colors.white,
                                // ),
                                // SizedBox(
                                //   width:
                                //       MediaQuery.of(context).size.width / 0.8,
                                //   height: 100,
                                //   child: Text(
                                //     insightslist![index].ins2.last,
                                //     style: Constants.headerlistitems,
                                //     overflow: TextOverflow.ellipsis,
                                //     textAlign: TextAlign.left,
                                //     //softWrap: true,
                                //   ),
                                // ),
                                const VerticalDivider(
                                  width: 1.0,
                                  color: Colors.white,
                                ),
                              ],
                            ),
                          ),
                        ),
                      );
                    },
                  ),
                ),
                const SizedBox(height: 10),
                const Text('AI Alert Status',
                    textAlign: TextAlign.right,
                    style: Constants.headerfontStyle),
                const SizedBox(
                  width: 0,
                  height: 5,
                ),
                Container(
                  height: 2,
                  width: 60,
                  color: const Color.fromARGB(255, 170, 57, 243),
                ),
                const SizedBox(
                  height: 15,
                  width: 1,
                ),
                Container(
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(10.0),
                    boxShadow: [
                      BoxShadow(
                        color: Colors.grey.withOpacity(0.5),
                        spreadRadius: 2,
                        blurRadius: 5,
                        offset: const Offset(0, 2),
                      ),
                    ],
                  ),

                  child: SizedBox(
                    height: 150,
                    child: SfCartesianChart(
                      primaryXAxis: CategoryAxis(
                        majorGridLines: const MajorGridLines(
                            width: 0), // Remove major grid lines
                        minorGridLines: const MinorGridLines(
                            width: 0), // Remove minor grid lines
                        axisLine: const AxisLine(width: 0), // Remove axis line
                        labelStyle:
                            const TextStyle(fontSize: 0), // Hide axis labels
                        majorTickLines: const MajorTickLines(
                            size: 0), // Hide major tick lines
                        minorTickLines: const MinorTickLines(
                            size: 0), // Hide minor tick lines
                      ),
                      primaryYAxis: NumericAxis(
                        majorGridLines: const MajorGridLines(
                            width: 0), // Remove major grid lines
                        minorGridLines: const MinorGridLines(
                            width: 0), // Remove minor grid lines
                        axisLine: const AxisLine(width: 0), // Remove axis line
                        labelStyle:
                            const TextStyle(fontSize: 0), // Hide axis labels
                        majorTickLines: const MajorTickLines(
                            size: 0), // Hide major tick lines
                        minorTickLines: const MinorTickLines(
                            size: 0), // Hide minor tick lines
                      ),
                      plotAreaBorderWidth: 0, // Remove plot area border
                      title: ChartTitle(text: ''), // Empty title
                      legend: Legend(
                        isVisible: true,
                        position: LegendPosition.bottom,
                      ),
                      // tooltipBehavior: TooltipBehavior(
                      //   textStyle:
                      //       const TextStyle(fontSize: 12, color: Colors.white),
                      // ),
                      series: <ChartSeries>[
                        StackedBarSeries<GDPData, String>(
                          dataSource: _chartData,
                          xValueMapper: (GDPData data, _) => data.continent,
                          yValueMapper: (GDPData data, _) => data.resolved,
                          name: 'Resolved',
                          legendIconType: LegendIconType.circle,
                          color: Colors.green,
                          dataLabelSettings: const DataLabelSettings(
                              isVisible: true,
                              textStyle:
                                  Constants.fontStyle), // Show data labels
                          pointColorMapper: (GDPData data, _) => Colors.green,
                        ),
                        StackedBarSeries<GDPData, String>(
                          dataSource: _chartData,
                          xValueMapper: (GDPData data, _) => data.continent,
                          yValueMapper: (GDPData data, _) => data.unresolved,
                          name: 'Unresolved',
                          legendIconType: LegendIconType.circle,
                          color: Colors.red,
                          dataLabelSettings: const DataLabelSettings(
                              isVisible: true,
                              textStyle:
                                  Constants.fontStyle), // Show data labels
                          pointColorMapper: (GDPData data, _) => Colors.red,
                        ),
                      ],
                    ),
                  ),

                  // child: SfCartesianChart(
                  //   primaryXAxis: NumericAxis(),
                  //   primaryYAxis: CategoryAxis(),
                  //   title: ChartTitle(text: 'Sales analysis'),
                  //   legend: Legend(isVisible: true),
                  //   tooltipBehavior: TooltipBehavior(enable: true),
                  //   palette: const <Color>[
                  //     Color.fromARGB(255, 4, 145, 74),
                  //     Color.fromARGB(255, 241, 92, 34)
                  //   ],
                  //   series: <ChartSeries<GDPData, String>>[
                  //     StackedBarSeries<GDPData, String>(
                  //       dataSource: _chartData,
                  //       xValueMapper: (GDPData sales, _) => sales.continent,
                  //       yValueMapper: (GDPData sales, _) => 0,
                  //       name: 'Alerts Graph',
                  //       dataLabelSettings: DataLabelSettings(isVisible: true),
                  //     ),
                  //   ],
                  // ),

                  // child: SfCartesianChart(
                  //   primaryXAxis: CategoryAxis(),
                  //   title: ChartTitle(text: 'Sales analysis'),
                  //   legend: Legend(isVisible: true),
                  //   tooltipBehavior: TooltipBehavior(enable: true),
                  //   palette: const <Color>[
                  //     Color.fromARGB(255, 4, 145, 74),
                  //     Color.fromARGB(255, 241, 92, 34)
                  //   ],
                  //   series: <ChartSeries<GDPData, String>>[
                  //     LineSeries<GDPData, String>(
                  //       dataSource: _chartData,
                  //       xValueMapper: (GDPData sales, _) => sales.continent,
                  //       yValueMapper: (GDPData sales, _) => sales.gdp,
                  //       name: 'Sales',
                  //       dataLabelSettings: DataLabelSettings(isVisible: true),
                  //     )
                  //   ],
                  //   onDataLabelRender: (DataLabelRenderArgs args) {
                  //     args.textStyle = args.textStyle.copyWith(
                  //       fontSize: 18, // Set the desired font size
                  //       fontFamily: 'Poppins',
                  //       fontWeight: FontWeight.bold,
                  //     ); // Set the desired font size
                  //   },
                  // ),

                  // child: SfCircularChart(
                  //   //backgroundColor: Colors.white,
                  //   margin: const EdgeInsets.all(10),
                  //   legend: Legend(
                  //     isVisible: true,
                  //     position: LegendPosition.top,
                  //   ),
                  //   tooltipBehavior: TooltipBehavior(
                  //     enable: true,
                  //     textStyle:
                  //         const TextStyle(fontSize: 22.0, color: Colors.white),
                  //   ),

                  //   palette: const <Color>[
                  //     Color.fromARGB(255, 4, 145, 74),
                  //     Color.fromARGB(255, 241, 92, 34)
                  //   ],
                  //   series: <CircularSeries>[
                  //     PieSeries<GDPData, String>(
                  //       dataSource: _chartData,
                  //       xValueMapper: (GDPData data, _) => data.continent,
                  //       yValueMapper: (GDPData data, _) => data.gdp,
                  //       dataLabelSettings: const DataLabelSettings(
                  //         isVisible: true,
                  //         textStyle:
                  //             TextStyle(fontSize: 32.0, color: Colors.white),
                  //       ),
                  //       enableTooltip: true,
                  //       radius: '100%',
                  //       explode: true,
                  //     ),
                  //   ],
                  //   onDataLabelRender: (DataLabelRenderArgs args) {
                  //     args.textStyle = args.textStyle.copyWith(
                  //       fontSize: 18, // Set the desired font size
                  //       fontFamily: 'Poppins',
                  //       fontWeight: FontWeight.bold,
                  //     ); // Set the desired font size
                  //   },
                  // ),
                ),
                const SizedBox(
                  height: 15,
                  width: 1,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Expanded(
                      child: Container(
                        margin: const EdgeInsets.only(
                            left: 0, top: 0, right: 0, bottom: 0),
                        width: MediaQuery.of(context).size.width * 0.44,
                        //visible: isAuthenticated,

                        child: Container(
                            //height: MediaQuery.of(context).size.width * 0.12,
                            padding: const EdgeInsets.all(1),
                            decoration: BoxDecoration(
                              gradient: const LinearGradient(
                                begin: Alignment.bottomLeft,
                                end: Alignment.bottomCenter,
                                colors: [
                                  Color.fromARGB(255, 170, 133, 220),
                                  //Color(0xFFC2185B),
                                  Color.fromARGB(255, 201, 175, 236),
                                ],
                              ),
                              borderRadius: BorderRadius.circular(7),
                            ),
                            child: TextFormField(
                              controller: _controller,
                              readOnly: true,
                              enabled: false,
                              decoration: const InputDecoration(
                                  border: InputBorder.none),
                              style: Constants.whitefontStyle,
                              textAlign: TextAlign.left,
                              onEditingComplete: () {
                                FocusScope.of(context)
                                    .unfocus(); // remove the focus from the TextFormField
                              },
                            )),
                      ),
                    ),
                  ],
                ),
                // const SizedBox(
                //   height: 10,
                //   width: 1,
                // ),
                // Row(
                //   mainAxisAlignment: MainAxisAlignment.start,
                //   children: [
                //     Expanded(
                //       child: Container(
                //         margin: const EdgeInsets.only(
                //             left: 0, top: 0, right: 0, bottom: 0),
                //         width: MediaQuery.of(context).size.width * 0.44,
                //         //visible: isAuthenticated,
                //         child: Container(
                //           padding: const EdgeInsets.all(1),
                //           decoration: BoxDecoration(
                //             gradient: const LinearGradient(
                //               begin: Alignment.bottomLeft,
                //               end: Alignment.bottomCenter,
                //               colors: [
                //                 Color.fromARGB(255, 249, 68, 2),
                //                 Color.fromARGB(255, 250, 59, 37),
                //               ],
                //             ),
                //             borderRadius: BorderRadius.circular(7),
                //           ),
                //           child: TextFormField(
                //             controller: _controller1,
                //             readOnly: true,
                //             enabled: false,
                //             decoration: const InputDecoration(
                //               border: InputBorder.none,
                //             ),
                //             style: Constants.whitefontStyle,
                //             textAlign: TextAlign.left,
                //             onEditingComplete: () {
                //               FocusScope.of(context).unfocus();
                //             },
                //           ),
                //         ),
                //       ),
                //     ),
                //   ],
                // ),
                const SizedBox(
                  height: 10,
                  width: 1,
                ),
                Row(mainAxisAlignment: MainAxisAlignment.start, children: [
                  Expanded(
                    child: SizedBox(
                      width: MediaQuery.of(context).size.width * 0.448,
                      child: GestureDetector(
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => Review(
                                      message: selectedDropdownItem.toString(),
                                      selecteddate:
                                          '${_selectedDate.year}/${_selectedDate.month}/${_selectedDate.day}',
                                    )),
                          );
                        },
                        child: Container(
                          margin: const EdgeInsets.only(
                              left: 0, top: 0, right: 2, bottom: 0),
                          width: MediaQuery.of(context).size.width * 0.44,
                          height: MediaQuery.of(context).size.width * 0.22,
                          padding: const EdgeInsets.all(5),
                          decoration: BoxDecoration(
                            gradient: const LinearGradient(
                              begin: Alignment.bottomLeft,
                              end: Alignment.bottomRight,
                              colors: [
                                Color(0xFFFF1412),
                                //Color(0xFFC2185B),
                                Color(0xFFFF714C),
                              ],
                            ),
                            borderRadius: BorderRadius.circular(7),
                          ),
                          child: Row(
                            children: [
                              Image.asset(
                                'assets/images/review.png',
                              ), // replace with your image
                            ],
                          ),
                        ),
                      ),
                    ),
                  ),
                  const VerticalDivider(width: 10.0),
                  Expanded(
                    child: Container(
                      margin: const EdgeInsets.only(
                          left: 0, top: 0, right: 0, bottom: 0),
                      width: MediaQuery.of(context).size.width * 0.44,
                      child: Visibility(
                        //visible: isAuthenticated,
                        child: GestureDetector(
                          onTap: () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => EmployeeStatus(
                                      message:
                                          selectedDropdownItem.toString())),
                            );
                          },
                          child: Container(
                            width: MediaQuery.of(context).size.width * 0.44,
                            height: MediaQuery.of(context).size.width * 0.22,
                            padding: const EdgeInsets.all(5),
                            decoration: BoxDecoration(
                              gradient: const LinearGradient(
                                begin: Alignment.bottomLeft,
                                end: Alignment.bottomCenter,
                                colors: [
                                  Color(0xFFc89181),
                                  Color(0xFFf5b97e),
                                  //Color(0xFFC2185B),
                                ],
                              ),
                              borderRadius: BorderRadius.circular(7),
                            ),
                            child: Row(
                              children: [
                                Image.asset(
                                  'assets/images/emp.png',
                                  // width: 145,
                                  // height: 160,
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ),
                  ),
                ]),
                const SizedBox(
                  height: 10,
                  width: 1,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Expanded(
                      child: Container(
                        margin: const EdgeInsets.only(
                            left: 0, top: 0, right: 2, bottom: 0),
                        width: MediaQuery.of(context).size.width * 0.44,
                        child: Visibility(
                          //visible: isAuthenticated,
                          child: GestureDetector(
                            onTap: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => NotificationReport(
                                          message:
                                              selectedDropdownItem.toString(),
                                          selecteddate:
                                              '${_selectedDate.year}/${_selectedDate.month}/${_selectedDate.day}',
                                        )),
                              );
                            },
                            child: Container(
                              width: MediaQuery.of(context).size.width * 0.44,
                              height: MediaQuery.of(context).size.width * 0.22,
                              padding: const EdgeInsets.all(5),
                              decoration: BoxDecoration(
                                gradient: const LinearGradient(
                                  begin: Alignment.bottomLeft,
                                  end: Alignment.bottomCenter,
                                  colors: [
                                    Color(0xFF6e87d6),
                                    //Color(0xFFC2185B),
                                    Color(0xFF48c5ef),
                                  ],
                                ),
                                borderRadius: BorderRadius.circular(7),
                              ),
                              child: Row(
                                children: [
                                  Image.asset(
                                    'assets/images/accuracy.png',
                                    // width: 145,
                                    // height: 160,
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                    const VerticalDivider(width: 10.0),
                    Expanded(
                      child: Container(
                        margin: const EdgeInsets.only(
                            left: 0, top: 0, right: 0, bottom: 0),
                        width: MediaQuery.of(context).size.width * 0.44,
                        child: Visibility(
                          //visible: isAuthenticated,
                          child: GestureDetector(
                            onTap: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => FootFall(
                                          message:
                                              selectedDropdownItem.toString(),
                                          selecteddate:
                                              '${_selectedDate.year}/${_selectedDate.month}/${_selectedDate.day}',
                                        )),
                              );
                            },
                            child: Container(
                              width: MediaQuery.of(context).size.width * 0.44,
                              height: MediaQuery.of(context).size.width * 0.22,
                              padding: const EdgeInsets.all(3),
                              decoration: BoxDecoration(
                                gradient: const LinearGradient(
                                  begin: Alignment.bottomLeft,
                                  end: Alignment.bottomCenter,
                                  colors: [
                                    Color(0xFF872b8e),
                                    //Color(0xFFC2185B),
                                    Color(0xFFcf2b72),
                                  ],
                                ),
                                borderRadius: BorderRadius.circular(7),
                              ),
                              child: Row(
                                children: [
                                  Image.asset(
                                    'assets/images/footfall.png',
                                    // width: 145,
                                    // height: 160,
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
                const SizedBox(
                  height: 10,
                  width: 1,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Expanded(
                      child: Container(
                        margin: const EdgeInsets.only(
                            left: 0, top: 0, right: 0, bottom: 0),
                        width: MediaQuery.of(context).size.width * 0.44,
                        child: GestureDetector(
                          onTap: () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => UserRegistration(
                                      message:
                                          selectedDropdownItem.toString())),
                            );
                          },
                          child: Container(
                            width: MediaQuery.of(context).size.width * 0.44,
                            height: MediaQuery.of(context).size.width * 0.22,
                            padding: const EdgeInsets.all(5),
                            decoration: BoxDecoration(
                              gradient: const LinearGradient(
                                begin: Alignment.bottomLeft,
                                end: Alignment.bottomRight,
                                colors: [
                                  Color(0xFF6a706f),
                                  //Color(0xFFC2185B),
                                  Color(0xFF91b3b1),
                                ],
                              ),
                              borderRadius: BorderRadius.circular(7),
                            ),
                            child: Row(
                              children: [
                                Image.asset(
                                  'assets/images/emp_registration.png',
                                  // width: 145,
                                  // height: 160,
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ),
                    const VerticalDivider(width: 10.0),
                    Expanded(
                      child: Container(
                        margin: const EdgeInsets.only(
                            left: 0, top: 0, right: 0, bottom: 0),
                        width: MediaQuery.of(context).size.width * 0.44,
                        child: Visibility(
                          //visible: iswarehouserelated,
                          child: GestureDetector(
                            onTap: () {
                              Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => IssueVideos(
                                          message:
                                              selectedDropdownItem.toString(),
                                        )),
                              );

                              // Navigator.push(
                              //   context,
                              //   MaterialPageRoute(
                              //       builder: (context) => CameraGridItems()),
                              // );
                            },
                            child: Container(
                              width: MediaQuery.of(context).size.width * 0.44,
                              height: MediaQuery.of(context).size.width * 0.22,
                              padding: const EdgeInsets.all(3),
                              decoration: BoxDecoration(
                                gradient: const LinearGradient(
                                  begin: Alignment.bottomLeft,
                                  end: Alignment.bottomRight,
                                  colors: [
                                    Color(0xFF525589),
                                    Color(0xFFaca8e0),
                                    //Color(0xFFC2185B),
                                  ],
                                ),
                                borderRadius: BorderRadius.circular(7),
                              ),
                              child: Row(
                                children: [
                                  Image.asset(
                                    'assets/images/videos.png',
                                    // width: 145,
                                    // height: 160,
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
                const SizedBox(
                  height: 10,
                  width: 1,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Expanded(
                      child: Container(
                        margin: const EdgeInsets.only(
                            left: 0, top: 0, right: 0, bottom: 0),
                        width: MediaQuery.of(context).size.width * 0.44,
                        child: GestureDetector(
                          onTap: () {
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (context) => MyHomePage(
                                          message:
                                              'https://vaman.plutokm.com/sa_mob_report/$empId',
                                        )));
                          },
                          child: Container(
                            width: MediaQuery.of(context).size.width * 0.44,
                            height: MediaQuery.of(context).size.width * 0.22,
                            padding: const EdgeInsets.all(5),
                            decoration: BoxDecoration(
                              gradient: const LinearGradient(
                                begin: Alignment.bottomLeft,
                                end: Alignment.bottomRight,
                                colors: [
                                  Color(0xFF2c3e50),
                                  //Color(0xFFC2185B),
                                  Color(0xFF5aacba),
                                ],
                              ),
                              borderRadius: BorderRadius.circular(7),
                            ),
                            child: Row(
                              children: [
                                Image.asset(
                                  'assets/images/weekly_report.png',
                                  // width: 145,
                                  // height: 160,
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ),
                    const VerticalDivider(width: 10.0),
                    Expanded(
                      child: Container(
                        margin: const EdgeInsets.only(
                            left: 0, top: 0, right: 0, bottom: 0),
                        width: MediaQuery.of(context).size.width * 0.44,
                        child: GestureDetector(
                          onTap: () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => UserRegistration(
                                      message:
                                          selectedDropdownItem.toString())),
                            );
                          },
                          child: Container(
                            width: MediaQuery.of(context).size.width * 0.44,
                            height: MediaQuery.of(context).size.width * 0.22,
                            padding: const EdgeInsets.all(5),
                          ),
                        ),
                      ),
                      // child: Container(
                      //   margin: const EdgeInsets.only(
                      //       left: 0, top: 0, right: 0, bottom: 0),
                      //   width: MediaQuery.of(context).size.width * 0.44,
                      //   child: GestureDetector(
                      //     onTap: () {
                      //       // Navigator.push(
                      //       //     context,
                      //       //     MaterialPageRoute(
                      //       //         builder: (context) =>
                      //       //             const SpeechSampleApp()));
                      //     },
                      //     child: Container(
                      //       width: MediaQuery.of(context).size.width * 0.44,
                      //       height: MediaQuery.of(context).size.width * 0.22,
                      //       padding: const EdgeInsets.all(5),
                      //       decoration: BoxDecoration(
                      //         gradient: const LinearGradient(
                      //           begin: Alignment.bottomLeft,
                      //           end: Alignment.bottomRight,
                      //           colors: [
                      //             Color(0xFF2c3e50),
                      //             //Color(0xFFC2185B),
                      //             Color(0xFF5aacba),
                      //           ],
                      //         ),
                      //         borderRadius: BorderRadius.circular(7),
                      //       ),
                      //       child: Row(
                      //         children: [
                      //           Image.asset(
                      //             'assets/images/weekly_report.png',
                      //             // width: 145,
                      //             // height: 160,
                      //           ),
                      //         ],
                      //       ),
                      //     ),
                      //   ),
                      // ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  // List<GDPData> getChartData(int res, int un) {
  //   final List<GDPData> chartData = [
  //     GDPData('Resolved', res),
  //     GDPData('UnResolved', un),
  //   ];
  //   return chartData;
  // }

  Future<void> getupdateData(String? selectedDropdownItem) async {
    showDialog(
        context: context,
        builder: (context) {
          return const Center(
              child: CircularProgressIndicator(
            color: Color.fromARGB(255, 173, 57, 230),
          ));
        });
    String selectedmonth = _selectedDate.month.toString();
    String selectedday = _selectedDate.day.toString();
    if (_selectedDate.month < 10) {
      selectedmonth = '0${_selectedDate.month}';
    }
    if (_selectedDate.day < 10) {
      selectedday = '0${_selectedDate.day}';
    }
    final String respon;
    final String res;
    //Response data;
    respon = await RemoteData().geAlertSummary(selectedDropdownItem!,
        '${_selectedDate.year}-$selectedmonth-$selectedday');

    res = await RemoteData().getpendingAlerts(
      selectedDropdownItem,
      '${_selectedDate.year}-$selectedmonth-$selectedday',
    );
    if (kDebugMode) {
      print(respon);
      print(res);
    }
    Navigator.of(context).pop();

    final chart = chartdataFromJson(respon);
    Map<String, dynamic> jsonMap = jsonDecode(res);
    var pending_alerts = jsonMap['no_of_pending_alerts'];
    var pending_video_alerts = jsonMap['no_of_pending_alert_videos'];

    setState(() {
      _chartData = getChartData(
          double.parse(chart.resolved), double.parse(chart.unresolved));
      _controller.text =
          '\t\t Pending \t: $pending_alerts \t Alerts | $pending_video_alerts \tvideos';
      //_controller1.text = '\t\t Pending Videos Alerts : $pending_video_alerts';
    });

    String insights = await RemoteData().getInsights(
      selectedRegion!,
      selectedDropdownItem,
      '${_selectedDate.year}-$selectedmonth-$selectedday',
    );
    print('Testing Data : $insights');

    List<dynamic> parsedJson = json.decode(insights);

    print(parsedJson);

    List<Insight> insightsList =
        parsedJson.map((json) => Insight.fromJson(json)).toList();
    setState(() {
      insightslist = insightsList;
    });
  }
}

class MyData {
  String? store;
}

// class GDPData {
//   GDPData(this.continent, this.gdp);
//   final String continent;
//   final int gdp;
// }

class GDPData {
  final String continent;
  final double resolved;
  final double unresolved;

  GDPData(this.continent, this.resolved, this.unresolved);
}

List<GDPData> getChartData(double res, double un) {
  final List<GDPData> chartData = [
    GDPData('', res, un), // Example data
    // Add more data as needed
  ];
  return chartData;
}

class Insight {
  int id;
  String content;
  String feedback;
  String heading;

  Insight(
      {required this.id,
      required this.content,
      required this.feedback,
      required this.heading});

  factory Insight.fromJson(Map<String, dynamic> json) {
    return Insight(
      id: json['IN_ID'],
      content: json['insight_content'],
      feedback: json['insight_feedback'],
      heading: json['insight_heading'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'IN_ID': id,
      'insight_content': content,
      'insight_feedback': feedback,
      'insight_heading': heading,
    };
  }
}
