import 'dart:convert';
import 'dart:core';
import 'dart:io';
import 'package:connectivity/connectivity.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';
import 'package:intl/intl.dart' show DateFormat;
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:carousel_slider/carousel_slider.dart';
import '../Models/ReviewList.dart';
import '../Models/UsersList.dart';
import '../Services/RemoteData.dart';
import 'Constants.dart';
import 'ImageZoomin.dart';

class Review extends StatefulWidget {
  //const Review({super.key});
  final String message;
  final String selecteddate;

  const Review({super.key, required this.message, required this.selecteddate});

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

class _Review extends State<Review> {
  String? selectedDropdownItem;
  var isMeeters = '';
  var selecteddate = '';
  //DateTime selecteddate = DateTime.now();
  DateTime _selectedDate = DateTime.now();
  final TextEditingController _dateTime = TextEditingController();
  List<UsersList>? userslist;
  final _controller = TextEditingController();
  int _currentIndex = 1;
  bool isvisible = true;
  //final items = ['Android', 'iOS', 'Desktop', 'Web'];
  final images = [
    'assets/images/dummy.jpeg',
    'assets/images/review.png',
    'assets/images/dummy.jpeg',
    'assets/images/review.png'
  ];
  List<String>? storesList;
  ReviewList? reviewList;
  List<Alerts>? filteredAlerts = [];
  late String imp;
  Key carouselKey = UniqueKey();
  bool pressed = false;
  bool pressed1 = false;
  Color black = Colors.black;
  Color black1 = Colors.black;
  Color black2 = Colors.black;
  Color black3 = Colors.black;
  Color black4 = Colors.black;
  Color black5 = Colors.black;
  Color black6 = Colors.black;

  //var ImageUrl = '';
  //List<Alerts> ? alertsList;

  @override
  void initState() {
    super.initState();
    _selectedDate = DateTime.now();
    String dateString = widget.selecteddate;
    DateFormat format = DateFormat("yyyy/MM/dd");
    _selectedDate = format.parse(dateString);
    _dateTime.text = DateFormat('yyyy/MM/dd').format(_selectedDate);
    isMeeters = widget.message;
    //print(message);
    _onButton7Pressed();
    //print(isMeeters);
    loadData();
    //getImage();
    //_checkInternetConnection();
  }

  Future<void> checkInternetConnection() async {
    final connectivityResult = await Connectivity().checkConnectivity();
    if (connectivityResult == ConnectivityResult.none) {
      setState(() {
        Constants('Review', 'Server Not Responding')
            .showAlertDialog(context, false);
      });
    } else {}
  }

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

  void getData(String name) async {
    bool isInternetConnected = await Constants.checkInternetConnectivity();
    if (isInternetConnected) {
      showDialog(
          context: context,
          builder: (context) {
            return const Center(
                child: CircularProgressIndicator(
              color: Color.fromARGB(255, 173, 57, 230),
            ));
          });

      var data = await RemoteData().getReview(
        selectedDropdownItem!,
        '${_selectedDate.year}/${_selectedDate.month}/${_selectedDate.day}',
      );
      if (data!.statusCode != 200) {
        Navigator.of(context).pop();
        Constants('Review', data.statusCode.toString())
            .showAlertDialog(context, false);
        return;
      }
      //print('Data:$data');
      Map<String, dynamic> responseMap = jsonDecode(data.body);
      if (kDebugMode) {
        print('response Hit :$responseMap');
        print(responseMap['status'].length);
      }
      if (responseMap['status'] == 'success') {
        reviewList = ReviewList.fromJson(responseMap);
        if (name == 'all') {
          filteredAlerts = reviewList!.alerts;
          _currentIndex = 1;
        } else {
          filteredAlerts = reviewList!.alerts
              ?.where((alert) => alert.nOTIFICATIONCAT == name)
              .toList();
          if (kDebugMode) {
            print(filteredAlerts!.length);
          }
          _currentIndex = 1;
        }
        setState(() {
          if (filteredAlerts!.isNotEmpty) {
            _controller.text = '${1}/${filteredAlerts?.length ?? 0}';
          } else {
            _controller.text = '${0}/${filteredAlerts?.length ?? 0}';
          }
        });
        Navigator.of(context).pop();
      } else if (responseMap['status'] == 'Error') {
        Navigator.of(context).pop();
        Constants('Review', 'No Data Found').showAlertDialog(context, false);
        setState(() {
          filteredAlerts?.clear();
          if (filteredAlerts!.isNotEmpty) {
            _controller.text = '${1}/${filteredAlerts?.length ?? 0}';
          } else {
            _controller.text = '${0}/${filteredAlerts?.length ?? 0}';
          }
        });
      } else {
        Navigator.of(context).pop();
        Constants('Review', data.statusCode.toString())
            .showAlertDialog(context, false);
      }
    } else {}
  }

  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), // <-- SEE HERE
                onPrimary: Colors.white, // <-- SEE HERE
                onSurface: Colors.black, // <-- SEE HERE
              ),
              textButtonTheme: TextButtonThemeData(
                style: TextButton.styleFrom(
                  foregroundColor: const Color.fromARGB(
                      255, 125, 50, 253), // button text color
                ),
              ),
            ),
            child: child!,
          );
        });
    if (picked != null && picked != _selectedDate) {
      setState(() {
        _selectedDate = picked;
        _dateTime.text =
            '${_selectedDate.year}/${_selectedDate.month}/${_selectedDate.day}';
        //getData(imp);
        _onButton7Pressed();
        //Constants.showAlertDialog1(context);
      });
    }
  }

  //ebe5f6
  //f772d1
  //c972f2

  Color _buttonColor1 = const Color.fromARGB(255, 235, 229, 246);
  Color _buttonColor2 = const Color.fromARGB(255, 235, 229, 246);
  Color _buttonColor3 = const Color.fromARGB(255, 235, 229, 246);
  Color _buttonColor4 = const Color.fromARGB(255, 235, 229, 246);
  Color _buttonColor5 = const Color.fromARGB(255, 235, 229, 246);
  Color _buttonColor6 = const Color.fromARGB(255, 235, 229, 246);
  Color _buttonColor7 = const Color.fromARGB(255, 235, 229, 246);

  void _onButton1Pressed() {
    imp = 'CUSTOMER MONITORING';
    setState(() {
      _currentIndex = 0;
      carouselKey = UniqueKey();
    });
    getData(imp);
    //Constants.showAlertDialog1(context);
    setState(() {
      black = Colors.white;
      black1 = Colors.black;
      black2 = Colors.black;
      black3 = Colors.black;
      black4 = Colors.black;
      black5 = Colors.black;
      black6 = Colors.black;
      _buttonColor1 = const Color.fromARGB(255, 219, 114, 229);
      _buttonColor2 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor3 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor4 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor5 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor6 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor7 = const Color.fromARGB(255, 235, 229, 246);
    });
  }

  void _onButton2Pressed() {
    imp = 'STORE MAINTENANCE';
    setState(() {
      _currentIndex = 0;
      carouselKey = UniqueKey();
    });
    getData(imp);
    //carouselKey = UniqueKey();
    //Constants.showAlertDialog1(context);
    setState(() {
      black = Colors.black;
      black1 = Colors.white;
      black2 = Colors.black;
      black3 = Colors.black;
      black4 = Colors.black;
      black5 = Colors.black;
      black6 = Colors.black;
      _buttonColor1 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor2 = const Color.fromARGB(255, 219, 114, 229);
      _buttonColor3 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor4 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor5 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor6 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor7 = const Color.fromARGB(255, 235, 229, 246);
    });
  }

  void _onButton3Pressed() {
    imp = 'EMPLOYEE MONITORING';
    setState(() {
      _currentIndex = 0;
      carouselKey = UniqueKey();
    });
    getData(imp);
    setState(() {
      black = Colors.black;
      black1 = Colors.black;
      black2 = Colors.white;
      black3 = Colors.black;
      black4 = Colors.black;
      black5 = Colors.black;
      black6 = Colors.black;
      _buttonColor1 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor2 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor3 = const Color.fromARGB(255, 219, 114, 229);
      _buttonColor4 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor5 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor6 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor7 = const Color.fromARGB(255, 235, 229, 246);
    });
  }

  void _onButton4Pressed() {
    imp = 'POSITIONAL AVAILABILITY';
    setState(() {
      _currentIndex = 0;
      carouselKey = UniqueKey();
    });
    getData(imp);
    setState(() {
      black = Colors.black;
      black1 = Colors.black;
      black2 = Colors.black;
      black3 = Colors.white;
      black4 = Colors.black;
      black5 = Colors.black;
      black6 = Colors.black;
      _buttonColor1 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor2 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor3 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor4 = const Color.fromARGB(255, 219, 114, 229);
      _buttonColor5 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor6 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor7 = const Color.fromARGB(255, 235, 229, 246);
    });
  }

  void _onButton5Pressed() {
    imp = 'COUNTER MONITORING';
    setState(() {
      _currentIndex = 0;
      carouselKey = UniqueKey();
    });
    getData(imp);
    setState(() {
      black = Colors.black;
      black1 = Colors.black;
      black2 = Colors.black;
      black3 = Colors.black;
      black4 = Colors.white;
      black5 = Colors.black;
      black6 = Colors.black;
      _buttonColor1 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor2 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor3 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor4 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor5 = const Color.fromARGB(255, 219, 114, 229);
      _buttonColor6 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor7 = const Color.fromARGB(255, 235, 229, 246);
    });
  }

  void _onButton6Pressed() {
    imp = 'OTHERS';
    setState(() {
      _currentIndex = 0;
      carouselKey = UniqueKey();
    });
    getData(imp);
    setState(() {
      black = Colors.black;
      black1 = Colors.black;
      black2 = Colors.black;
      black3 = Colors.black;
      black4 = Colors.black;
      black5 = Colors.white;
      black6 = Colors.black;
      _buttonColor1 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor2 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor3 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor4 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor5 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor6 = const Color.fromARGB(255, 219, 114, 229);
      _buttonColor7 = const Color.fromARGB(255, 235, 229, 246);
    });
  }

  void _onButton7Pressed() {
    imp = 'all';
    setState(() {
      _currentIndex = 0;
      carouselKey = UniqueKey();
    });
    getData(imp);
    setState(() {
      black = Colors.black;
      black1 = Colors.black;
      black2 = Colors.black;
      black3 = Colors.black;
      black4 = Colors.black;
      black5 = Colors.black;
      black6 = Colors.white;
      _buttonColor1 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor2 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor3 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor4 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor5 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor6 = const Color.fromARGB(255, 235, 229, 246);
      _buttonColor7 = const Color.fromARGB(255, 219, 114, 229);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(
          'REVIEW',
          style: Constants.fontStyle,
        ),
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: Constants.getReturn(),
          ),
        ),
      ),
      body: SingleChildScrollView(
        child: Container(
          margin: const EdgeInsets.all(15.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: selectedDropdownItem,
                    onChanged: (newValue) {
                      setState(() {
                        selectedDropdownItem = newValue;
                        _onButton7Pressed();
                      });
                    },
                    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: 15,
                width: 1,
              ),
              TextFormField(
                controller: _dateTime,
                //onTap: () => print('Date Picker'),
                decoration: InputDecoration(
                  contentPadding:
                      const EdgeInsets.symmetric(vertical: 15, horizontal: 10),
                  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(width: 1, height: 15),
              Container(
                decoration: BoxDecoration(
                  gradient: const LinearGradient(
                    colors: [Color(0xFF119887), Color(0xFF2bca83)],
                    begin: Alignment.topLeft,
                    end: Alignment.bottomRight,
                  ),
                  borderRadius: BorderRadius.circular(25),
                ),
                child: ElevatedButton(
                  onPressed: () {
                    _onButton7Pressed();
                  },
                  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(
                        'SUBMIT',
                        style: Constants.buttonsfontStyle,
                      ))),
                ),
              ),
              const SizedBox(width: 1, height: 15),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Expanded(
                    child: SizedBox(
                      width: double.infinity,
                      height: 50,
                      child: ElevatedButton(
                        onPressed: () {
                          _onButton1Pressed();
                          setState(() {
                            pressed = !pressed;
                            //pressed1 = !pressed1;
                          });
                        },
                        style: ElevatedButton.styleFrom(
                            backgroundColor: _buttonColor1),
                        child: Text(
                          'CUSTOMER',
                          style: reviewbuttonsfontStyle(black),
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(width: 10, height: 10),
                  Expanded(
                    child: SizedBox(
                      width: double.infinity,
                      height: 50,
                      child: ElevatedButton(
                        onPressed: () {
                          _onButton2Pressed();
                          setState(() {
                            pressed1 = !pressed1;
                            //pressed = !pressed;
                          });
                        },
                        style: ElevatedButton.styleFrom(
                            backgroundColor: _buttonColor2),
                        child: Text(
                          'STORE',
                          style: reviewbuttonsfontStyle(black1),
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(width: 10, height: 10),
                  Expanded(
                    child: SizedBox(
                      width: double.infinity,
                      height: 50,
                      child: ElevatedButton(
                        onPressed: _onButton3Pressed,
                        style: ElevatedButton.styleFrom(
                            backgroundColor: _buttonColor3),
                        child: Text(
                          'EMPLOYEE',
                          style: reviewbuttonsfontStyle(black2),
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              const SizedBox(
                width: 1,
                height: 10,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Expanded(
                    child: SizedBox(
                      width: double.infinity,
                      height: 50,
                      child: ElevatedButton(
                        onPressed: _onButton4Pressed,
                        style: ElevatedButton.styleFrom(
                            backgroundColor: _buttonColor4),
                        child: Text(
                          'POSITIONAL',
                          style: reviewbuttonsfontStyle(black3),
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(
                    width: 10,
                    height: 10,
                  ),
                  Expanded(
                    child: SizedBox(
                      width: double.infinity,
                      height: 50,
                      child: ElevatedButton(
                        onPressed: _onButton5Pressed,
                        style: ElevatedButton.styleFrom(
                            backgroundColor: _buttonColor5),
                        child: Text(
                          'COUNTER',
                          style: reviewbuttonsfontStyle(black4),
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(
                    width: 10,
                    height: 10,
                  ),
                  Expanded(
                    child: SizedBox(
                      width: double.infinity,
                      height: 50,
                      child: ElevatedButton(
                        onPressed: _onButton6Pressed,
                        style: ElevatedButton.styleFrom(
                            backgroundColor: _buttonColor6),
                        child: Text(
                          'OTHERS',
                          style: reviewbuttonsfontStyle(black5),
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              const SizedBox(
                width: 1,
                height: 10,
              ),
              SizedBox(
                width: double.infinity,
                height: 50,
                child: ElevatedButton(
                  onPressed: _onButton7Pressed,
                  style:
                      ElevatedButton.styleFrom(backgroundColor: _buttonColor7),
                  child: Text(
                    'ALL SCENARIOS',
                    style: reviewbuttonsfontStyle(black6),
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ),
              const SizedBox(
                width: 1,
                height: 10,
              ),
              ElevatedButton(
                  onPressed: () {},
                  style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all<Color>(
                        const Color.fromARGB(255, 35, 22, 66)),
                  ),
                  child: TextFormField(
                    controller: _controller,
                    decoration: const InputDecoration(border: InputBorder.none),
                    style: const TextStyle(fontSize: 20, color: Colors.white),
                    textAlign: TextAlign.center,
                    onEditingComplete: () {
                      FocusScope.of(context)
                          .unfocus(); // remove the focus from the TextFormField
                    },
                  )),
              const SizedBox(
                width: 1,
                height: 1,
              ),
              StatefulBuilder(
                builder: (BuildContext context, StateSetter setState) {
                  return CarouselSlider.builder(
                      key: carouselKey,
                      options: CarouselOptions(
                        viewportFraction: 1.1,
                        height: MediaQuery.of(context).size.height * 0.80,
                        autoPlay: false,
                        enlargeCenterPage: false,
                        initialPage: _currentIndex,
                        onPageChanged: (index, reason) {
                          if (reason == CarouselPageChangedReason.manual) {
                            setState(() {
                              if (kDebugMode) {
                                print('Real Index:$index');
                              }
                              _currentIndex = index;
                              int count = _currentIndex + 1;
                              _controller.text =
                                  '$count/${filteredAlerts?.length ?? 0}';
                              if (kDebugMode) {
                                print('position: $_currentIndex');
                              }
                            });
                          }
                        },
                      ),
                      itemCount: filteredAlerts?.length ?? 0,
                      itemBuilder: (context, itemIndex, realIndex) {
                        if (kDebugMode) {
                          print('inside index:$itemIndex');
                        }
                        if (filteredAlerts != null &&
                            filteredAlerts!.isNotEmpty) {
                          if (kDebugMode) {
                            print('Allowed');
                          }
                          return FutureBuilder<String>(
                              future: loadImage(filteredAlerts![itemIndex].path,
                                  filteredAlerts![itemIndex].alertReason),
                              builder: (BuildContext context,
                                  AsyncSnapshot<String> snapshot) {
                                if (snapshot.connectionState ==
                                    ConnectionState.waiting) {
                                  return const Center(
                                    child: CircularProgressIndicator(),
                                  );
                                }
                                if (snapshot.hasError) {
                                  return Text('Error: ${snapshot.error}');
                                }
                                if (snapshot.hasData) {
                                  String imageUrl = snapshot.data!;
                                  if (kDebugMode) {
                                    print(filteredAlerts![itemIndex].feedback);
                                  }
                                  if (filteredAlerts![itemIndex].feedback ==
                                          'yes' ||
                                      filteredAlerts![itemIndex].feedback ==
                                          'no' ||
                                      filteredAlerts![itemIndex].feedback ==
                                          'audit') {
                                    isvisible = false;
                                  } else {
                                    isvisible = true;
                                  }

                                  return Padding(
                                    padding: const EdgeInsets.all(15.0),
                                    child: Card(
                                      shape: RoundedRectangleBorder(
                                        borderRadius:
                                            BorderRadius.circular(5.0),
                                      ),
                                      child: Column(
                                        children: [
                                          Stack(
                                            children: [
                                              FadeInImage(
                                                placeholder:
                                                    AssetImage(images[0]),
                                                image: NetworkImage(
                                                  imageUrl,
                                                ),
                                                fit: BoxFit.fitWidth,
                                              ),
                                              Positioned(
                                                bottom: 16,
                                                right: 16,
                                                child: Row(
                                                  children: [
                                                    IconButton(
                                                        onPressed: () {
                                                          shareImageWithText(
                                                              imageUrl,
                                                              'Reason: ${filteredAlerts![itemIndex].alertReason}\nCamera:${filteredAlerts![itemIndex].cameraName}\nTime:${filteredAlerts![itemIndex].alertTime}\nID:${filteredAlerts![itemIndex].notificationID}');
                                                        },
                                                        icon: const Icon(
                                                          Icons.share,
                                                          color: Colors.white,
                                                        )),
                                                    const SizedBox(width: 10),
                                                    IconButton(
                                                        onPressed: () {
                                                          Navigator.push(
                                                            context,
                                                            MaterialPageRoute(
                                                                builder: (context) =>
                                                                    ImageZoomin(
                                                                        message:
                                                                            imageUrl)),
                                                          );
                                                        },
                                                        icon: const Icon(
                                                          Icons.zoom_out_map,
                                                          color: Colors.white,
                                                        )),
                                                  ],
                                                ),
                                              ),
                                            ],
                                          ),
                                          Container(
                                            margin: const EdgeInsets.all(5.0),
                                            child: Column(
                                              children: [
                                                Row(
                                                  mainAxisAlignment:
                                                      MainAxisAlignment.start,
                                                  children: [
                                                    const Text(
                                                        '  Reason         :',
                                                        style: Constants
                                                            .listHeaderfontStyle,
                                                        overflow: TextOverflow
                                                            .ellipsis),
                                                    const SizedBox(
                                                      width: 10,
                                                      height: 1,
                                                    ),
                                                    Expanded(
                                                      child: Text(
                                                        Constants.capitalizeFirstLetter(
                                                            filteredAlerts![
                                                                    itemIndex]
                                                                .alertReason
                                                                .toString()
                                                                .toLowerCase()),
                                                        softWrap: true,
                                                        textAlign:
                                                            TextAlign.left,
                                                        style: Constants
                                                            .listHeaderoppfontStyle,
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                                const SizedBox(height: 10.0),
                                                Row(
                                                  mainAxisAlignment:
                                                      MainAxisAlignment.start,
                                                  children: [
                                                    const Text(
                                                        '  Camera       :',
                                                        style: Constants
                                                            .listHeaderfontStyle,
                                                        overflow: TextOverflow
                                                            .ellipsis),
                                                    const SizedBox(
                                                      width: 10,
                                                      height: 1,
                                                    ),
                                                    Expanded(
                                                      child: Text(
                                                        Constants.capitalizeFirstLetter(
                                                            filteredAlerts![
                                                                    itemIndex]
                                                                .cameraName
                                                                .toString()
                                                                .toLowerCase()),
                                                        softWrap: true,
                                                        textAlign:
                                                            TextAlign.left,
                                                        style: Constants
                                                            .listHeaderoppfontStyle,
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                                const SizedBox(height: 10.0),
                                                Row(
                                                  mainAxisAlignment:
                                                      MainAxisAlignment.start,
                                                  children: [
                                                    const Text(
                                                        '  Time               :',
                                                        style: Constants
                                                            .listHeaderfontStyle,
                                                        overflow: TextOverflow
                                                            .ellipsis),
                                                    const SizedBox(
                                                      width: 10,
                                                      height: 1,
                                                    ),
                                                    Text(
                                                        filteredAlerts![
                                                                itemIndex]
                                                            .alertTime
                                                            .toString()
                                                            .toLowerCase(),
                                                        textAlign:
                                                            TextAlign.right,
                                                        style: Constants
                                                            .listHeaderoppfontStyle,
                                                        overflow: TextOverflow
                                                            .ellipsis),
                                                  ],
                                                ),
                                                const SizedBox(height: 10.0),
                                                Row(
                                                  mainAxisAlignment:
                                                      MainAxisAlignment.start,
                                                  children: [
                                                    const Text(
                                                        '  Status           :',
                                                        style: Constants
                                                            .listHeaderfontStyle,
                                                        overflow: TextOverflow
                                                            .ellipsis),
                                                    const SizedBox(
                                                      width: 10,
                                                      height: 1,
                                                    ),
                                                    if (filteredAlerts![
                                                                itemIndex]
                                                            .feedback ==
                                                        '')
                                                      const Text('Unresolved',
                                                          style: Constants
                                                              .listHeadercolorfontStyle,
                                                          overflow: TextOverflow
                                                              .ellipsis),
                                                    if (filteredAlerts![
                                                                    itemIndex]
                                                                .feedback ==
                                                            'yes' ||
                                                        filteredAlerts![
                                                                    itemIndex]
                                                                .feedback ==
                                                            'no' ||
                                                        filteredAlerts![
                                                                    itemIndex]
                                                                .feedback ==
                                                            'audit')
                                                      //isvisible =false;
                                                      const Text('Resolved',
                                                          style: Constants
                                                              .listHeadercolor1fontStyle,
                                                          overflow: TextOverflow
                                                              .ellipsis),
                                                  ],
                                                ),
                                                const SizedBox(height: 10.0),
                                                Row(
                                                  mainAxisAlignment:
                                                      MainAxisAlignment.start,
                                                  children: [
                                                    const Text(
                                                        '  Priority         :',
                                                        style: Constants
                                                            .listHeaderfontStyle,
                                                        overflow: TextOverflow
                                                            .ellipsis),
                                                    const SizedBox(
                                                      width: 10,
                                                      height: 1,
                                                    ),
                                                    if (filteredAlerts![
                                                                itemIndex]
                                                            .show ==
                                                        'yes')
                                                      const Text('High',
                                                          style: Constants
                                                              .listHeaderoppfontStyle,
                                                          overflow: TextOverflow
                                                              .ellipsis),
                                                    if (filteredAlerts![
                                                                itemIndex]
                                                            .show ==
                                                        'no')
                                                      const Text('Low',
                                                          style: Constants
                                                              .listHeaderoppfontStyle,
                                                          overflow: TextOverflow
                                                              .ellipsis),
                                                  ],
                                                ),
                                                const SizedBox(height: 10.0),
                                                Row(
                                                  mainAxisAlignment:
                                                      MainAxisAlignment.start,
                                                  children: [
                                                    const Text(
                                                        '  ID                      :',
                                                        style: Constants
                                                            .listHeaderfontStyle,
                                                        overflow: TextOverflow
                                                            .ellipsis),
                                                    const SizedBox(
                                                        width: 10, height: 1),
                                                    Text(
                                                        filteredAlerts![
                                                                itemIndex]
                                                            .notificationID
                                                            .toString()
                                                            .toLowerCase(),
                                                        style: Constants
                                                            .listHeaderoppfontStyle,
                                                        overflow: TextOverflow
                                                            .ellipsis),
                                                  ],
                                                ),
                                                const SizedBox(height: 10.0),
                                                // Row(
                                                //   mainAxisAlignment:
                                                //       MainAxisAlignment.start,
                                                //   children: [
                                                //     const Text('Comments:',
                                                //         style: Constants
                                                //             .listHeaderfontStyle,
                                                //         overflow: TextOverflow
                                                //             .ellipsis),
                                                //     const SizedBox(
                                                //         width: 10, height: 1),
                                                //     Text(
                                                //         filteredAlerts![
                                                //                 itemIndex]
                                                //             .notificationID
                                                //             .toString()
                                                //             .toLowerCase(),
                                                //         style: Constants
                                                //             .listHeaderoppfontStyle,
                                                //         overflow: TextOverflow
                                                //             .ellipsis),
                                                //   ],
                                                // ),
                                                // const SizedBox(height: 20.0),
                                                if (isvisible)
                                                  Row(
                                                    mainAxisAlignment:
                                                        MainAxisAlignment
                                                            .spaceAround,
                                                    children: [
                                                      Container(
                                                        decoration:
                                                            BoxDecoration(
                                                          gradient:
                                                              const LinearGradient(
                                                            colors: [
                                                              Color(0xFFff0f0f),
                                                              Color(0xFFff734d)
                                                            ],
                                                            begin: Alignment
                                                                .topLeft,
                                                            end: Alignment
                                                                .bottomRight,
                                                          ),
                                                          borderRadius:
                                                              BorderRadius
                                                                  .circular(25),
                                                        ),
                                                        child: ElevatedButton(
                                                          onPressed: () {
                                                            getShow(itemIndex,
                                                                'no');
                                                          },
                                                          style: ElevatedButton
                                                              .styleFrom(
                                                            backgroundColor: Colors
                                                                .transparent, // make the button transparent
                                                            elevation:
                                                                0, // remove the button's shadow
                                                            shape:
                                                                RoundedRectangleBorder(
                                                              borderRadius:
                                                                  BorderRadius
                                                                      .circular(
                                                                          10),
                                                            ),
                                                          ),
                                                          child: SizedBox(
                                                              height: 50,
                                                              width: MediaQuery.of(
                                                                          context)
                                                                      .size
                                                                      .width /
                                                                  3,
                                                              child:
                                                                  const Center(
                                                                      child:
                                                                          Text(
                                                                'No',
                                                                style: Constants
                                                                    .buttonsfontStyle,
                                                              ))),
                                                        ),
                                                      ),
                                                      const SizedBox(
                                                        width: 5,
                                                        height: 0,
                                                      ),
                                                      Container(
                                                        decoration:
                                                            BoxDecoration(
                                                          gradient:
                                                              const LinearGradient(
                                                            colors: [
                                                              Color(0xFF119887),
                                                              Color(0xFF2bca83)
                                                            ],
                                                            begin: Alignment
                                                                .topLeft,
                                                            end: Alignment
                                                                .bottomRight,
                                                          ),
                                                          borderRadius:
                                                              BorderRadius
                                                                  .circular(25),
                                                        ),
                                                        child: ElevatedButton(
                                                          onPressed: () {
                                                            // getAction(itemIndex,
                                                            //     'yes', '');
                                                            getShow(itemIndex,
                                                                'yes');
                                                          },
                                                          style: ElevatedButton
                                                              .styleFrom(
                                                            backgroundColor: Colors
                                                                .transparent, // make the button transparent
                                                            elevation:
                                                                0, // remove the button's shadow
                                                            shape:
                                                                RoundedRectangleBorder(
                                                              borderRadius:
                                                                  BorderRadius
                                                                      .circular(
                                                                          10),
                                                            ),
                                                          ),
                                                          child: SizedBox(
                                                              height: 50,
                                                              width: MediaQuery.of(
                                                                          context)
                                                                      .size
                                                                      .width /
                                                                  3,
                                                              child:
                                                                  const Center(
                                                                      child:
                                                                          Text(
                                                                'Yes',
                                                                style: Constants
                                                                    .buttonsfontStyle,
                                                              ))),
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                const SizedBox(height: 20.0),
                                                if (isvisible)
                                                  Row(
                                                    mainAxisAlignment:
                                                        MainAxisAlignment
                                                            .center,
                                                    children: [
                                                      Container(
                                                        decoration:
                                                            BoxDecoration(
                                                          gradient:
                                                              const LinearGradient(
                                                            colors: [
                                                              Color.fromARGB(
                                                                  255,
                                                                  61,
                                                                  43,
                                                                  75),
                                                              Color.fromARGB(
                                                                  255,
                                                                  61,
                                                                  43,
                                                                  75)
                                                            ],
                                                            begin: Alignment
                                                                .topLeft,
                                                            end: Alignment
                                                                .bottomRight,
                                                          ),
                                                          borderRadius:
                                                              BorderRadius
                                                                  .circular(25),
                                                        ),
                                                        child: ElevatedButton(
                                                          onPressed: () {
                                                            getAction(itemIndex,
                                                                'audit', '');
                                                          },
                                                          style: ElevatedButton
                                                              .styleFrom(
                                                            backgroundColor: Colors
                                                                .transparent, // make the button transparent
                                                            elevation:
                                                                0, // remove the button's shadow
                                                            shape:
                                                                RoundedRectangleBorder(
                                                              borderRadius:
                                                                  BorderRadius
                                                                      .circular(
                                                                          10),
                                                            ),
                                                          ),
                                                          child: SizedBox(
                                                              height: 50,
                                                              width: MediaQuery.of(
                                                                          context)
                                                                      .size
                                                                      .width /
                                                                  1.3,
                                                              child:
                                                                  const Center(
                                                                      child:
                                                                          Text(
                                                                'Audit',
                                                                style: Constants
                                                                    .buttonsfontStyle,
                                                              ))),
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                const Column(
                                                  mainAxisAlignment:
                                                      MainAxisAlignment.start,
                                                  children: [
                                                    Text('',
                                                        style: Constants
                                                            .listHeaderfontStyle,
                                                        overflow: TextOverflow
                                                            .ellipsis),
                                                    SizedBox(
                                                        width: 10, height: 1),
                                                  ],
                                                ),
                                              ],
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  );
                                } else {
                                  if (kDebugMode) {
                                    print(filteredAlerts?.length);
                                  }
                                }
                                return const SizedBox(
                                  width: 1,
                                  height: 10,
                                );
                              });
                        } else {
                          return const Center(
                            child: Text(''),
                          );
                        }
                      });
                },
              ),
            ],
          ),
        ),
      ),
    );
  }

  getAction(int index, String feed, String comments) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String? userId = prefs.getString('empId');
    final res = await RemoteData().getFeedbackStatus(
        filteredAlerts![index].notificationID.toString(),
        userId!,
        feed,
        comments);
    // print('GetAction: $res');
    if (res == 200) {
      setState(() {
        if (feed == 'yes' || feed == 'no' || feed == 'audit') {
          isvisible = false;
          filteredAlerts![index].feedback = 'yes';
        }
        Constants('Review', 'Sucess').showAlertDialog(context, false);
      });
    }
  }

  Future<void> shareImageWithText(String url, String textToShare) async {
    final box = context.findRenderObject() as RenderBox?;
    //print('inside share function');
    final response = await get(Uri.parse(url));
    //print('got response:$response');
    //final bytes = response.bodyBytes;
    final Directory temp = await getTemporaryDirectory();
    final File imageFile = File('${temp.path}/ShareImage.jpg');
    imageFile.writeAsBytesSync(response.bodyBytes);
    if (Platform.isAndroid) {
      await Share.shareXFiles([XFile('${temp.path}/ShareImage.jpg')],
          text: textToShare,
          //subject: 'Reason',
          sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
    } else if (Platform.isIOS) {
      await Share.shareXFiles([XFile('${temp.path}/ShareImage.jpg')],
          //text: textToShare,
          //subject: 'Reason',
          sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
    }
  }

  loadImage(String? path, String? reason) {
    final s3url = RemoteData().getImageUrl(path!, reason!);
    //print(s3url);
    return s3url;
  }

  getShow(int index, String text) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        String textFieldValue = '';
        return AlertDialog(
          title: const Text('Write a Comment'),
          content: TextFormField(
            maxLines: 3,
            onChanged: (value) {
              textFieldValue = value;
            },
            decoration: InputDecoration(
              hintText: 'Enter comment here',
              contentPadding:
                  const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
              //isDense: true,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(2.0),
              ),
            ),
          ),
          actions: [
            ElevatedButton(
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all<Color>(
                  const Color.fromARGB(255, 124, 76, 211),
                ),
              ),
              onPressed: () {
                getAction(index, text, textFieldValue);
                Navigator.of(context).pop(); // Close the dialog
              },
              child: const Padding(
                padding: EdgeInsets.only(right: 130),
                child: Text('Submit'),
              ),
              //child: const Text('Submit'),
            ),
          ],
        );
      },
    );
  }

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