import 'dart:convert';
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';
import 'package:flutter/material.dart';
import 'package:doctor/common/settings.dart';
import 'package:doctor/common/zoom_image.dart';

class AllRecordsOnClick extends StatefulWidget {
  var recordDetails;
  var initialIndex;
  var customerId;

  AllRecordsOnClick({this.recordDetails,this.initialIndex,this.customerId});

  @override
  State<AllRecordsOnClick> createState() => _AllRecordsOnClickState();
}

class _AllRecordsOnClickState extends State<AllRecordsOnClick>
    with TickerProviderStateMixin {
  //late TabController _controller;
  /*final List<Tab> topTabs = <Tab>[
    Tab(
        child: Text(
          'Findings',
          style: TextStyle(fontSize: 12),
        )),
    Tab(
        child: Text(
          'Reports',
          style: TextStyle(fontSize: 12),
        )),
    Tab(
        child: Text(
          'Prescriptions',
          style: TextStyle(fontSize: 12),
        )),
  ];*/
  final ImagePicker imagePicker = ImagePicker();
  List imageFileList = [];
  final ImagePicker _picker = ImagePicker();

  @override
  void initState() {
    //_controller = TabController(vsync: this, initialIndex: widget.initialIndex, length: topTabs.length);
    super.initState();
  }

  Future pickImageFromGalleryForUpdateFindings() async {
    imageFileList = [];
    final List<XFile>? selectedImages = await imagePicker.pickMultiImage();
    AppSettings.preLoaderDialog(context);
    if (selectedImages!.isNotEmpty) {
      imageFileList.addAll(selectedImages);
    }

    var res = await AppSettings.updateFindingsGallery(
        imageFileList, widget.recordDetails.recordId,widget.customerId);
    print(jsonDecode(res));
    Navigator.of(context, rootNavigator: true).pop();
    setState(() {
      widget.recordDetails.findingsImages = jsonDecode(res)['findings'];
    });
  }

  Future takeImageFromCameraForUpdateFindings() async {
    try {
      final image = await _picker.pickImage(source: ImageSource.camera);
      if (image == null) return;
      final imageTemp = File(image.path);
      AppSettings.preLoaderDialog(context);
      var res = await AppSettings.updateFindingsCamera(
          image, widget.recordDetails.recordId,widget.customerId);
      print(jsonDecode(res));
      Navigator.of(context, rootNavigator: true).pop();
      setState(() {
        widget.recordDetails.findingsImages = jsonDecode(res)['findings'];
      });
    } on PlatformException catch (e) {
      print('Failed to pick image: $e');
    }
  }

  Future pickImageFromGalleryForUpdateReports() async {
    imageFileList = [];
    final List<XFile>? selectedImages = await imagePicker.pickMultiImage();
    AppSettings.preLoaderDialog(context);
    if (selectedImages!.isNotEmpty) {
      imageFileList.addAll(selectedImages);
    }

    var res = await AppSettings.updateReportsGallery(
        imageFileList, widget.recordDetails.recordId,widget.customerId);
    print(jsonDecode(res));
    Navigator.of(context, rootNavigator: true).pop();
    setState(() {
      widget.recordDetails.reportImages = jsonDecode(res)['reports'];
    });
  }

  Future takeImageFromCameraForUpdateReports() async {
    try {
      final image = await _picker.pickImage(source: ImageSource.camera);
      if (image == null) return;
      final imageTemp = File(image.path);
      AppSettings.preLoaderDialog(context);
      var res = await AppSettings.updateReportsCamera(
          image, widget.recordDetails.recordId,widget.customerId);
      print(jsonDecode(res));
      Navigator.of(context, rootNavigator: true).pop();
      setState(() {
        widget.recordDetails.reportImages = jsonDecode(res)['reports'];
      });
    } on PlatformException catch (e) {
      print('Failed to pick image: $e');
    }
  }

  Future pickImageFromGalleryForUpdatePrescriptions() async {
    imageFileList = [];
    final List<XFile>? selectedImages = await imagePicker.pickMultiImage();
    AppSettings.preLoaderDialog(context);
    if (selectedImages!.isNotEmpty) {
      imageFileList.addAll(selectedImages);
    }

    var res = await AppSettings.updatePrescriptionsGallery(
        imageFileList, widget.recordDetails.recordId,widget);
    print(jsonDecode(res));
    Navigator.of(context, rootNavigator: true).pop();
    setState(() {
      widget.recordDetails.prescriptionImages = jsonDecode(res)['prescription'];
    });
  }

  Future takeImageFromCameraForUpdatePrescriptions() async {
    try {
      final image = await _picker.pickImage(source: ImageSource.camera);
      if (image == null) return;
      final imageTemp = File(image.path);
      AppSettings.preLoaderDialog(context);
      var res = await AppSettings.updatePrescriptionsCamera(
          image, widget.recordDetails.recordId,widget.customerId);
      print(jsonDecode(res));
      Navigator.of(context, rootNavigator: true).pop();
      setState(() {
        widget.recordDetails.prescriptionImages =
        jsonDecode(res)['prescription'];
      });
    } on PlatformException catch (e) {
      print('Failed to pick image: $e');
    }
  }


  Widget renderUiForFindings() {
    if (widget.recordDetails.findingsImages.length != 0) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Expanded(
              child: GridView.builder(
                itemCount: widget.recordDetails.findingsImages.length,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  crossAxisSpacing: 2.0,
                  mainAxisSpacing: 2.0,
                ),
                itemBuilder: (BuildContext context, int index) {
                  return GestureDetector(
                    onTap: () {
                      Navigator.push(
                          context,
                          new MaterialPageRoute(
                              builder: (__) => new ImageZoomPage(
                                  imageName: 'Findings',
                                  imageDetails: widget.recordDetails
                                      .findingsImages[index]['url'])));
                      /*gridOntap(index);*/
                    },
                    child: Container(
                      width: MediaQuery.of(context).size.width * .30,
                      height: MediaQuery.of(context).size.height * .15,
                      decoration: BoxDecoration(
                          shape: BoxShape.rectangle,
                          image: DecorationImage(
                              image: NetworkImage(
                                  widget.recordDetails.findingsImages[index]
                                  ['url']) as ImageProvider, // picked file
                              fit: BoxFit.fill)),
                      child: Stack(children: [
                        Positioned(
                          right: 0,
                          child: Container(
                            child: IconButton(
                              iconSize: 30,
                              icon: const Icon(
                                Icons.delete,
                                color: Colors.red,
                              ),
                              onPressed: () async {
                                showDialog(
                                  barrierDismissible: false,
                                  context: context,
                                  builder: (BuildContext context) => AlertDialog(
                                    title:
                                    const Text('Do you want to delete image?',
                                        style: TextStyle(
                                          color: primaryColor,
                                          fontSize: 20,
                                        )),
                                    actionsAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    actions: [
                                      TextButton(
                                        onPressed: () async {
                                          AppSettings.preLoaderDialog(context);

                                          String fileName = widget.recordDetails
                                              .findingsImages[index]['url']
                                              .split('/')
                                              .last;

                                          var payload = new Map<String, dynamic>();
                                          payload["urlType"] = 'findings';
                                          payload["url"] = widget.recordDetails
                                              .findingsImages[index]['url'];

                                          // bool deleteStatus = await AppSettings.deleteRecordsNew(payload,widget.recordId);
                                          try {
                                            var res =
                                            await AppSettings.deleteRecordsNew(
                                                payload,
                                                widget.recordDetails.recordId,widget.customerId);
                                            print(jsonDecode(res));
                                            Navigator.of(context,
                                                rootNavigator: true)
                                                .pop();
                                            Navigator.of(context).pop(true);
                                            AppSettings.longSuccessToast(
                                                "Image deleted Successfully");
                                            setState(() {
                                              widget.recordDetails.findingsImages =
                                              jsonDecode(res)['remainingUrls'];
                                            });
                                          } catch (e) {
                                            print(e);
                                            Navigator.of(context,
                                                rootNavigator: true)
                                                .pop();
                                            Navigator.of(context).pop(true);
                                            AppSettings.longFailedToast(
                                                "Image deletion failed");
                                          }
                                        },
                                        child: const Text('Yes',
                                            style: TextStyle(
                                              color: primaryColor,
                                              fontSize: 20,
                                            )),
                                      ),
                                      TextButton(
                                        onPressed: () {
                                          Navigator.of(context).pop(true);
                                        },
                                        child: const Text('No',
                                            style: TextStyle(
                                              color: primaryColor,
                                              fontSize: 20,
                                            )),
                                      ),
                                    ],
                                  ),
                                );
                              },
                            ),
                            /* color: Colors.pinkAccent,
                      width: 35,
                      height: 35,*/
                          ),
                        )
                      ]),
                    ),

                    //Image.network(widget.imageDetails[index]['url']),
                  );
                },
              )),
          Padding(
            padding: EdgeInsets.fromLTRB(8, 8, 8, 8),
            child: CircleAvatar(
              backgroundColor: primaryColor,
              radius: 40,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  IconButton(
                    iconSize: 40,
                    icon: const Icon(
                      Icons.add,
                      color: Colors.white,
                    ),
                    onPressed: () async {
                      showModalBottomSheet<void>(
                          context: context,
                          builder: (BuildContext context) {
                            return SizedBox(
                              height: 200,
                              child: Center(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    GestureDetector(
                                      child: Icon(
                                        Icons.camera_alt_outlined,
                                        size: 100,
                                        color: primaryColor,
                                      ),
                                      onTap: () async {
                                        await takeImageFromCameraForUpdateFindings();
                                        Navigator.pop(context);
                                      },
                                    ),
                                    SizedBox(
                                      width: MediaQuery.of(context).size.width *
                                          .20,
                                    ),
                                    GestureDetector(
                                      child: Icon(
                                        Icons.photo,
                                        size: 100,
                                        color: primaryColor,
                                      ),
                                      onTap: () async {
                                        await pickImageFromGalleryForUpdateFindings();
                                        Navigator.pop(context);
                                      },
                                    ),
                                  ],
                                ),
                              ),
                            );
                          });
                    },
                  ),
                ],
              ),
            ),
          ),
        ],
      );
    } else {
      return Center(
          child: Padding(
            padding: EdgeInsets.fromLTRB(0, 40, 0, 0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(
                  height: MediaQuery.of(context).size.height * .25,
                ),
                Text('Click below icon to add new findings'),
                SizedBox(
                  height: 20,
                ),
                CircleAvatar(
                  backgroundColor: primaryColor,
                  radius: 40,
                  child: IconButton(
                    iconSize: 40,
                    icon: const Icon(
                      Icons.add,
                      color: Colors.white,
                    ),
                    onPressed: () async {
                      showModalBottomSheet<void>(
                          context: context,
                          builder: (BuildContext context) {
                            return SizedBox(
                              height: 200,
                              child: Center(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    GestureDetector(
                                      child: Icon(
                                        Icons.camera_alt_outlined,
                                        size: 100,
                                        color: primaryColor,
                                      ),
                                      onTap: () async {
                                        await takeImageFromCameraForUpdateFindings();
                                        Navigator.pop(context);
                                      },
                                    ),
                                    SizedBox(
                                      width:
                                      MediaQuery.of(context).size.width * .20,
                                    ),
                                    GestureDetector(
                                      child: Icon(
                                        Icons.photo,
                                        size: 100,
                                        color: primaryColor,
                                      ),
                                      onTap: () async {
                                        await pickImageFromGalleryForUpdateFindings();
                                        Navigator.pop(context);
                                      },
                                    ),
                                  ],
                                ),
                              ),
                            );
                          });
                    },
                  ),
                )
              ],
            ),
          ));
    }
  }

  Widget renderUiForReports() {
    if (widget.recordDetails.reportImages.length != 0) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Expanded(
              child: GridView.builder(
                itemCount: widget.recordDetails.reportImages.length,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 3,
                  crossAxisSpacing: 2.0,
                  mainAxisSpacing: 2.0,
                ),
                itemBuilder: (BuildContext context, int index) {
                  return GestureDetector(
                    onTap: () {
                      Navigator.push(
                          context,
                          new MaterialPageRoute(
                              builder: (__) => new ImageZoomPage(
                                  imageName: 'Findings',
                                  imageDetails: widget
                                      .recordDetails.reportImages[index]['url'])));
                      /*gridOntap(index);*/
                    },
                    child: Container(
                      width: MediaQuery.of(context).size.width * .30,
                      height: MediaQuery.of(context).size.height * .15,
                      decoration: BoxDecoration(
                          shape: BoxShape.rectangle,
                          image: DecorationImage(
                              image: NetworkImage(
                                  widget.recordDetails.reportImages[index]
                                  ['url']) as ImageProvider, // picked file
                              fit: BoxFit.fill)),
                      child: Stack(children: [
                        Positioned(
                          right: 0,
                          child: Container(
                            child: IconButton(
                              iconSize: 30,
                              icon: const Icon(
                                Icons.delete,
                                color: Colors.red,
                              ),
                              onPressed: () async {
                                showDialog(
                                  barrierDismissible: false,
                                  context: context,
                                  builder: (BuildContext context) => AlertDialog(
                                    title:
                                    const Text('Do you want to delete image?',
                                        style: TextStyle(
                                          color: primaryColor,
                                          fontSize: 20,
                                        )),
                                    actionsAlignment:
                                    MainAxisAlignment.spaceBetween,
                                    actions: [
                                      TextButton(
                                        onPressed: () async {
                                          AppSettings.preLoaderDialog(context);

                                          String fileName = widget.recordDetails
                                              .reportImages[index]['url']
                                              .split('/')
                                              .last;

                                          var payload = new Map<String, dynamic>();
                                          payload["urlType"] = 'reports';
                                          payload["url"] = widget.recordDetails
                                              .reportImages[index]['url'];

                                          try {
                                            var res =
                                            await AppSettings.deleteRecordsNew(
                                                payload,
                                                widget.recordDetails.recordId,widget.customerId);
                                            print(jsonDecode(res));
                                            Navigator.of(context,
                                                rootNavigator: true)
                                                .pop();
                                            Navigator.of(context).pop(true);
                                            AppSettings.longSuccessToast(
                                                "Image deleted Successfully");
                                            setState(() {
                                              widget.recordDetails.reportImages =
                                              jsonDecode(res)['remainingUrls'];
                                            });
                                          } catch (e) {
                                            print(e);
                                            Navigator.of(context,
                                                rootNavigator: true)
                                                .pop();
                                            Navigator.of(context).pop(true);
                                            AppSettings.longFailedToast(
                                                "Image deletion failed");
                                          }
                                        },
                                        child: const Text('Yes',
                                            style: TextStyle(
                                              color: primaryColor,
                                              fontSize: 20,
                                            )),
                                      ),
                                      TextButton(
                                        onPressed: () {
                                          Navigator.of(context).pop(true);
                                        },
                                        child: const Text('No',
                                            style: TextStyle(
                                              color: primaryColor,
                                              fontSize: 20,
                                            )),
                                      ),
                                    ],
                                  ),
                                );
                              },
                            ),
                            /* color: Colors.pinkAccent,
                      width: 35,
                      height: 35,*/
                          ),
                        )
                      ]),
                    ),

                    //Image.network(widget.imageDetails[index]['url']),
                  );
                },
              )),
          Padding(
            padding: EdgeInsets.fromLTRB(8, 8, 8, 8),
            child: CircleAvatar(
              backgroundColor: primaryColor,
              radius: 40,
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  IconButton(
                    iconSize: 40,
                    icon: const Icon(
                      Icons.add,
                      color: Colors.white,
                    ),
                    onPressed: () async {
                      showModalBottomSheet<void>(
                          context: context,
                          builder: (BuildContext context) {
                            return SizedBox(
                              height: 200,
                              child: Center(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    GestureDetector(
                                      child: Icon(
                                        Icons.camera_alt_outlined,
                                        size: 100,
                                        color: primaryColor,
                                      ),
                                      onTap: () async {
                                        await takeImageFromCameraForUpdateReports();
                                        Navigator.pop(context);
                                      },
                                    ),
                                    SizedBox(
                                      width: MediaQuery.of(context).size.width *
                                          .20,
                                    ),
                                    GestureDetector(
                                      child: Icon(
                                        Icons.photo,
                                        size: 100,
                                        color: primaryColor,
                                      ),
                                      onTap: () async {
                                        await pickImageFromGalleryForUpdateReports();
                                        Navigator.pop(context);
                                      },
                                    ),
                                  ],
                                ),
                              ),
                            );
                          });
                    },
                  ),
                ],
              ),
            ),
          ),
        ],
      );
    } else {
      return Center(
          child: Padding(
            padding: EdgeInsets.fromLTRB(0, 40, 0, 0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(
                  height: MediaQuery.of(context).size.height * .25,
                ),
                Text('Click below icon to add new Report'),
                SizedBox(
                  height: 20,
                ),
                CircleAvatar(
                  backgroundColor: primaryColor,
                  radius: 40,
                  child: IconButton(
                    iconSize: 40,
                    icon: const Icon(
                      Icons.add,
                      color: Colors.white,
                    ),
                    onPressed: () async {
                      showModalBottomSheet<void>(
                          context: context,
                          builder: (BuildContext context) {
                            return SizedBox(
                              height: 200,
                              child: Center(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    GestureDetector(
                                      child: Icon(
                                        Icons.camera_alt_outlined,
                                        size: 100,
                                        color: primaryColor,
                                      ),
                                      onTap: () async {
                                        await takeImageFromCameraForUpdateReports();
                                        Navigator.pop(context);
                                      },
                                    ),
                                    SizedBox(
                                      width:
                                      MediaQuery.of(context).size.width * .20,
                                    ),
                                    GestureDetector(
                                      child: Icon(
                                        Icons.photo,
                                        size: 100,
                                        color: primaryColor,
                                      ),
                                      onTap: () async {
                                        await pickImageFromGalleryForUpdateReports();
                                        Navigator.pop(context);
                                      },
                                    ),
                                  ],
                                ),
                              ),
                            );
                          });
                    },
                  ),
                )
              ],
            ),
          ));
    }
  }

  Widget renderUiForPrescriptions() {
    if (widget.recordDetails.prescriptionImages.length != 0) {
      return Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Expanded(
            child: GridView.builder(
              itemCount: widget.recordDetails.prescriptionImages.length,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                crossAxisSpacing: 2.0,
                mainAxisSpacing: 2.0,
              ),
              itemBuilder: (BuildContext context, int index) {
                return GestureDetector(
                  onTap: () {
                    Navigator.push(
                        context,
                        new MaterialPageRoute(
                            builder: (__) => new ImageZoomPage(
                                imageName: 'Prescripitons',
                                imageDetails: widget.recordDetails
                                    .prescriptionImages[index]['url'])));
                    /*gridOntap(index);*/
                  },
                  child: Container(
                    width: MediaQuery.of(context).size.width * .30,
                    height: MediaQuery.of(context).size.height * .15,
                    decoration: BoxDecoration(
                        shape: BoxShape.rectangle,
                        image: DecorationImage(
                            image: NetworkImage(
                                widget.recordDetails.prescriptionImages[index]
                                ['url']) as ImageProvider, // picked file
                            fit: BoxFit.fill)),
                    child: Stack(children: [
                      Positioned(
                        right: 0,
                        child: Container(
                          child: IconButton(
                            iconSize: 30,
                            icon: const Icon(
                              Icons.delete,
                              color: Colors.red,
                            ),
                            onPressed: () async {
                              showDialog(
                                barrierDismissible: false,
                                context: context,
                                builder: (BuildContext context) => AlertDialog(
                                  title:
                                  const Text('Do you want to delete image?',
                                      style: TextStyle(
                                        color: primaryColor,
                                        fontSize: 20,
                                      )),
                                  actionsAlignment:
                                  MainAxisAlignment.spaceBetween,
                                  actions: [
                                    TextButton(
                                      onPressed: () async {
                                        AppSettings.preLoaderDialog(context);

                                        String fileName = widget.recordDetails
                                            .prescriptionImages[index]['url']
                                            .split('/')
                                            .last;

                                        var payload =
                                        new Map<String, dynamic>();
                                        payload["urlType"] = 'prescription';
                                        payload["url"] = widget.recordDetails
                                            .prescriptionImages[index]['url'];

                                        try {
                                          var res = await AppSettings
                                              .deleteRecordsNew(
                                              payload,
                                              widget
                                                  .recordDetails.recordId,widget.customerId);
                                          print(jsonDecode(res));
                                          Navigator.of(context,
                                              rootNavigator: true)
                                              .pop();
                                          Navigator.of(context).pop(true);
                                          AppSettings.longSuccessToast(
                                              "Image deleted Successfully");
                                          setState(() {
                                            widget.recordDetails
                                                .prescriptionImages =
                                            jsonDecode(
                                                res)['remainingUrls'];
                                          });
                                        } catch (e) {
                                          print(e);
                                          Navigator.of(context,
                                              rootNavigator: true)
                                              .pop();
                                          Navigator.of(context).pop(true);
                                          AppSettings.longFailedToast(
                                              "Image deletion failed");
                                        }
                                      },
                                      child: const Text('Yes',
                                          style: TextStyle(
                                            color: primaryColor,
                                            fontSize: 20,
                                          )),
                                    ),
                                    TextButton(
                                      onPressed: () {
                                        Navigator.of(context).pop(true);
                                      },
                                      child: const Text('No',
                                          style: TextStyle(
                                            color: primaryColor,
                                            fontSize: 20,
                                          )),
                                    ),
                                  ],
                                ),
                              );
                            },
                          ),
                        ),
                      )
                    ]),
                  ),

                  //Image.network(widget.imageDetails[index]['url']),
                );
              },
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Padding(
                padding: EdgeInsets.fromLTRB(8, 8, 8, 8),
                child: CircleAvatar(
                  backgroundColor: primaryColor,
                  radius: 40,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      IconButton(
                        iconSize: 40,
                        icon: const Icon(
                          Icons.shopping_cart,
                          color: Colors.white,
                        ),
                        onPressed: () {
                          /*Navigator.push(
                              context,
                              new MaterialPageRoute(
                                  builder: (__) => new OrderMedicines(
                                      prescriptionDetails: widget.recordDetails)));*/
                          //signup screen
                        },
                      ),
                    ],
                  ),
                ),
              ),
              Padding(
                padding: EdgeInsets.fromLTRB(8, 8, 8, 8),
                child: CircleAvatar(
                  backgroundColor: primaryColor,
                  radius: 40,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      IconButton(
                        iconSize: 40,
                        icon: const Icon(
                          Icons.add,
                          color: Colors.white,
                        ),
                        onPressed: () async {
                          showModalBottomSheet<void>(
                              context: context,
                              builder: (BuildContext context) {
                                return SizedBox(
                                  height: 200,
                                  child: Center(
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.center,
                                      children: <Widget>[
                                        GestureDetector(
                                          child: Icon(
                                            Icons.camera_alt_outlined,
                                            size: 100,
                                            color: primaryColor,
                                          ),
                                          onTap: () async {
                                            await takeImageFromCameraForUpdatePrescriptions();
                                            Navigator.pop(context);
                                          },
                                        ),
                                        SizedBox(
                                          width: MediaQuery.of(context).size.width *
                                              .20,
                                        ),
                                        GestureDetector(
                                          child: Icon(
                                            Icons.photo,
                                            size: 100,
                                            color: primaryColor,
                                          ),
                                          onTap: () async {
                                            await pickImageFromGalleryForUpdatePrescriptions();
                                            Navigator.pop(context);
                                          },
                                        ),
                                      ],
                                    ),
                                  ),
                                );
                              });
                        },
                      ),
                    ],
                  ),
                ),
              ),
            ],
          )
        ],
      );
    } else {
      return Center(
          child: Padding(
            padding: EdgeInsets.fromLTRB(0, 40, 0, 0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(
                  height: MediaQuery.of(context).size.height * .25,
                ),
                Text('Click below icon to add new Prescriptions'),
                SizedBox(
                  height: 20,
                ),
                CircleAvatar(
                  backgroundColor: primaryColor,
                  radius: 40,
                  child: IconButton(
                    iconSize: 40,
                    icon: const Icon(
                      Icons.add,
                      color: Colors.white,
                    ),
                    onPressed: () async {
                      showModalBottomSheet<void>(
                          context: context,
                          builder: (BuildContext context) {
                            return SizedBox(
                              height: 200,
                              child: Center(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    GestureDetector(
                                      child: Icon(
                                        Icons.camera_alt_outlined,
                                        size: 100,
                                        color: primaryColor,
                                      ),
                                      onTap: () async {
                                        await takeImageFromCameraForUpdatePrescriptions();
                                        Navigator.pop(context);
                                      },
                                    ),
                                    SizedBox(
                                      width:
                                      MediaQuery.of(context).size.width * .20,
                                    ),
                                    GestureDetector(
                                      child: Icon(
                                        Icons.photo,
                                        size: 100,
                                        color: primaryColor,
                                      ),
                                      onTap: () async {
                                        await pickImageFromGalleryForUpdatePrescriptions();
                                        Navigator.pop(context);
                                      },
                                    ),
                                  ],
                                ),
                              ),
                            );
                          });
                    },
                  ),
                )
              ],
            ),
          ));
    }
  }

  Widget prescriptions(var obj){
    if(obj.prescriptionImages.length!=0){
      return Container(
        //color: secondaryColor,
          width: double.infinity,
          height: MediaQuery.of(context).size.height * .20,
          child: Row(
            children: [
              Expanded(child:  ListView.builder(
                  scrollDirection: Axis.horizontal,
                  itemCount: obj.prescriptionImages.length,
                  itemBuilder: (context, index) {
                    return Row(
                      children: [
                        Card(
                          child: GestureDetector(
                            onTap: (){
                              //showPicDialog(obj.prescriptionImages[index]['url']);
                              Navigator.push(
                                  context,
                                  new MaterialPageRoute(
                                      builder: (__) => new ImageZoomPage(imageName:'Prescriptions',imageDetails:obj.prescriptionImages[index]['url'])));},
                            child: Container(
                              width: MediaQuery.of(context).size.width * .30,
                              height: MediaQuery.of(context).size.height * .15,
                              decoration: BoxDecoration(
                                  shape: BoxShape.rectangle,
                                  image: DecorationImage(
                                      image: NetworkImage(
                                          obj.prescriptionImages[index]['url'])
                                      as ImageProvider, // picked file
                                      fit: BoxFit.fill)),
                              child: Stack(children: [
                                Positioned(
                                  right: 0,
                                  child: Container(
                                    child: IconButton(
                                      iconSize: 30,
                                      icon: const Icon(
                                        Icons.delete,
                                        color: Colors.red,
                                      ),
                                      onPressed: () async {
                                        showDialog(
                                          barrierDismissible: false,
                                          context: context,
                                          builder: (BuildContext context) => AlertDialog(
                                            title:
                                            const Text('Do you want to delete image?',
                                                style: TextStyle(
                                                  color: primaryColor,
                                                  fontSize: 20,
                                                )),
                                            actionsAlignment:
                                            MainAxisAlignment.spaceBetween,
                                            actions: [
                                              TextButton(
                                                onPressed: () async {
                                                  AppSettings.preLoaderDialog(context);

                                                  String fileName = obj.prescriptionImages[index]['url']
                                                      .split('/')
                                                      .last;

                                                  var payload =
                                                  new Map<String, dynamic>();
                                                  payload["urlType"] = 'prescription';
                                                  payload["url"] = obj.prescriptionImages[index]['url'];

                                                  try {
                                                    var res = await AppSettings.deleteRecordsNew(payload, obj.recordId,widget.customerId);
                                                    print(jsonDecode(res));
                                                    Navigator.of(context, rootNavigator: true).pop();
                                                    Navigator.of(context).pop(true);
                                                    AppSettings.longSuccessToast("Image deleted Successfully");
                                                    setState(() {
                                                      obj.prescriptionImages =
                                                      jsonDecode(
                                                          res)['remainingUrls'];
                                                    });
                                                  } catch (e) {
                                                    print(e);
                                                    Navigator.of(context,
                                                        rootNavigator: true)
                                                        .pop();
                                                    Navigator.of(context).pop(true);
                                                    AppSettings.longFailedToast(
                                                        "Image deletion failed");
                                                  }
                                                },
                                                child: const Text('Yes',
                                                    style: TextStyle(
                                                      color: primaryColor,
                                                      fontSize: 20,
                                                    )),
                                              ),
                                              TextButton(
                                                onPressed: () {
                                                  Navigator.of(context).pop(true);
                                                },
                                                child: const Text('No',
                                                    style: TextStyle(
                                                      color: primaryColor,
                                                      fontSize: 20,
                                                    )),
                                              ),
                                            ],
                                          ),
                                        );
                                      },
                                    ),
                                  ),
                                )
                              ]),
                            ),
                          ),
                        )

                      ],
                    );
                  }),),
              Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  IconButton(
                    iconSize: 40,
                    icon: const Icon(
                      Icons.add,
                      color: Colors.green,
                    ),
                    onPressed: () async {
                      showModalBottomSheet<void>(
                          context: context,
                          builder: (BuildContext context) {
                            return SizedBox(
                              height: 200,
                              child: Center(
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    GestureDetector(
                                      child: Icon(
                                        Icons.camera_alt_outlined,
                                        size: 100,
                                        color: primaryColor,
                                      ),
                                      onTap: () async {
                                        await takeImageFromCameraForUpdatePrescriptions();
                                        Navigator.pop(context);
                                      },
                                    ),
                                    SizedBox(
                                      width: MediaQuery.of(context).size.width *
                                          .20,
                                    ),
                                    GestureDetector(
                                      child: Icon(
                                        Icons.photo,
                                        size: 100,
                                        color: primaryColor,
                                      ),
                                      onTap: () async {
                                        await pickImageFromGalleryForUpdatePrescriptions();
                                        Navigator.pop(context);
                                      },
                                    ),
                                  ],
                                ),
                              ),
                            );
                          });



                    },
                  ),
                  IconButton(
                    iconSize: 40,
                    icon: const Icon(
                      Icons.shopping_cart,
                      color: primaryColor,
                    ),
                    onPressed: () {
                      /*Navigator.push(
                          context,
                          new MaterialPageRoute(
                              builder: (__) => new OrderMedicines(
                                  prescriptionDetails: widget.recordDetails)));*/
                      //signup screen
                    },
                  ),
                ],
              )

            ],
          )
      );
    }
    else{
      return GestureDetector(
        onTap: (){
          showModalBottomSheet<void>(
              context: context,
              builder: (BuildContext context) {
                return SizedBox(
                  height: 200,
                  child: Center(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        GestureDetector(
                          child: Icon(
                            Icons.camera_alt_outlined,
                            size: 100,
                            color: primaryColor,
                          ),
                          onTap: () async {
                            await takeImageFromCameraForUpdatePrescriptions();
                            Navigator.pop(context);
                          },
                        ),
                        SizedBox(
                          width: MediaQuery.of(context).size.width *
                              .20,
                        ),
                        GestureDetector(
                          child: Icon(
                            Icons.photo,
                            size: 100,
                            color: primaryColor,
                          ),
                          onTap: () async {
                            await pickImageFromGalleryForUpdatePrescriptions();
                            Navigator.pop(context);
                          },
                        ),
                      ],
                    ),
                  ),
                );
              });
        },
        child: Row(
          children: [
            Icon(
              Icons.add,
              color: Colors.green,
              size: 40,
            ),
            Text('Add Prescriptions',style: textButtonStyle(),)
          ],
        ),
      );
    }



  }

  Widget findings(var obj){
    if(obj.findingsImages.length!=0){
      return Container(
        //color: secondaryColor,
          width: double.infinity,
          height: MediaQuery.of(context).size.height * .20,
          child: Row(
            children: [
              Expanded(child:  ListView.builder(
                  scrollDirection: Axis.horizontal,
                  itemCount: obj.findingsImages.length,
                  itemBuilder: (context, index) {
                    return Row(
                      children: [
                        Card(
                          child: GestureDetector(
                            onTap: (){
                              //showPicDialog(obj.prescriptionImages[index]['url']);
                              Navigator.push(
                                  context,
                                  new MaterialPageRoute(
                                      builder: (__) => new ImageZoomPage(imageName:'Findings',imageDetails:obj.findingsImages[index]['url'])));},
                            child: Container(
                              width: MediaQuery.of(context).size.width * .30,
                              height: MediaQuery.of(context).size.height * .15,
                              decoration: BoxDecoration(
                                  shape: BoxShape.rectangle,
                                  image: DecorationImage(
                                      image: NetworkImage(
                                          obj.findingsImages[index]['url'])
                                      as ImageProvider, // picked file
                                      fit: BoxFit.fill)),
                              child: Stack(children: [
                                Positioned(
                                  right: 0,
                                  child: Container(
                                    child: IconButton(
                                      iconSize: 30,
                                      icon: const Icon(
                                        Icons.delete,
                                        color: Colors.red,
                                      ),
                                      onPressed: () async {
                                        showDialog(
                                          barrierDismissible: false,
                                          context: context,
                                          builder: (BuildContext context) => AlertDialog(
                                            title:
                                            const Text('Do you want to delete image?',
                                                style: TextStyle(
                                                  color: primaryColor,
                                                  fontSize: 20,
                                                )),
                                            actionsAlignment:
                                            MainAxisAlignment.spaceBetween,
                                            actions: [
                                              TextButton(
                                                onPressed: () async {
                                                  AppSettings.preLoaderDialog(context);

                                                  String fileName = widget.recordDetails
                                                      .findingsImages[index]['url']
                                                      .split('/')
                                                      .last;

                                                  var payload = new Map<String, dynamic>();
                                                  payload["urlType"] = 'findings';
                                                  payload["url"] = widget.recordDetails
                                                      .findingsImages[index]['url'];

                                                  // bool deleteStatus = await AppSettings.deleteRecordsNew(payload,widget.recordId);
                                                  try {
                                                    var res =
                                                    await AppSettings.deleteRecordsNew(
                                                        payload,
                                                        widget.recordDetails.recordId,widget.customerId);
                                                    print(jsonDecode(res));
                                                    Navigator.of(context,
                                                        rootNavigator: true)
                                                        .pop();
                                                    Navigator.of(context).pop(true);
                                                    AppSettings.longSuccessToast(
                                                        "Image deleted Successfully");
                                                    setState(() {
                                                      widget.recordDetails.findingsImages =
                                                      jsonDecode(res)['remainingUrls'];
                                                    });
                                                  } catch (e) {
                                                    print(e);
                                                    Navigator.of(context,
                                                        rootNavigator: true)
                                                        .pop();
                                                    Navigator.of(context).pop(true);
                                                    AppSettings.longFailedToast(
                                                        "Image deletion failed");
                                                  }
                                                },
                                                child: const Text('Yes',
                                                    style: TextStyle(
                                                      color: primaryColor,
                                                      fontSize: 20,
                                                    )),
                                              ),
                                              TextButton(
                                                onPressed: () {
                                                  Navigator.of(context).pop(true);
                                                },
                                                child: const Text('No',
                                                    style: TextStyle(
                                                      color: primaryColor,
                                                      fontSize: 20,
                                                    )),
                                              ),
                                            ],
                                          ),
                                        );
                                      },
                                    ),
                                    /* color: Colors.pinkAccent,
                      width: 35,
                      height: 35,*/
                                  ),
                                )
                              ]),
                            ),
                          ),
                        )

                      ],
                    );
                  }),),
              IconButton(
                iconSize: 40,
                icon: const Icon(
                  Icons.add,
                  color: Colors.green,
                ),
                onPressed: () async {
                  showModalBottomSheet<void>(
                      context: context,
                      builder: (BuildContext context) {
                        return SizedBox(
                          height: 200,
                          child: Center(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                GestureDetector(
                                  child: Icon(
                                    Icons.camera_alt_outlined,
                                    size: 100,
                                    color: primaryColor,
                                  ),
                                  onTap: () async {
                                    await takeImageFromCameraForUpdateFindings();
                                    Navigator.pop(context);
                                  },
                                ),
                                SizedBox(
                                  width:
                                  MediaQuery.of(context).size.width * .20,
                                ),
                                GestureDetector(
                                  child: Icon(
                                    Icons.photo,
                                    size: 100,
                                    color: primaryColor,
                                  ),
                                  onTap: () async {
                                    await pickImageFromGalleryForUpdateFindings();
                                    Navigator.pop(context);
                                  },
                                ),
                              ],
                            ),
                          ),
                        );
                      });
                },
              )

            ],
          )
      );
    }
    else{
      return GestureDetector(
        onTap: (){
          showModalBottomSheet<void>(
              context: context,
              builder: (BuildContext context) {
                return SizedBox(
                  height: 200,
                  child: Center(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        GestureDetector(
                          child: Icon(
                            Icons.camera_alt_outlined,
                            size: 100,
                            color: primaryColor,
                          ),
                          onTap: () async {
                            await takeImageFromCameraForUpdateFindings();
                            Navigator.pop(context);
                          },
                        ),
                        SizedBox(
                          width:
                          MediaQuery.of(context).size.width * .20,
                        ),
                        GestureDetector(
                          child: Icon(
                            Icons.photo,
                            size: 100,
                            color: primaryColor,
                          ),
                          onTap: () async {
                            await pickImageFromGalleryForUpdateFindings();
                            Navigator.pop(context);
                          },
                        ),
                      ],
                    ),
                  ),
                );
              });

        },
        child: Row(
          children: [
            Icon(
              Icons.add,
              color: Colors.green,
              size: 40,
            ),
            Text('Add Findings',style: textButtonStyle(),)
          ],
        ),
      );
    }



  }

  Widget reports(var obj){
    if(obj.reportImages.length!=0){
      return Container(
        //color: secondaryColor,
          width: double.infinity,
          height: MediaQuery.of(context).size.height * .20,
          child: Row(
            children: [
              Expanded(child:  ListView.builder(
                  scrollDirection: Axis.horizontal,
                  itemCount: obj.reportImages.length,
                  itemBuilder: (context, index) {
                    return Row(
                      children: [
                        Card(
                          child: GestureDetector(
                            onTap: (){
                              //showPicDialog(obj.prescriptionImages[index]['url']);
                              Navigator.push(
                                  context,
                                  new MaterialPageRoute(
                                      builder: (__) => new ImageZoomPage(imageName:'Findings',imageDetails:obj.reportImages[index]['url'])));},
                            child: Container(
                              width: MediaQuery.of(context).size.width * .30,
                              height: MediaQuery.of(context).size.height * .15,
                              decoration: BoxDecoration(
                                  shape: BoxShape.rectangle,
                                  image: DecorationImage(
                                      image: NetworkImage(
                                          obj.reportImages[index]['url'])
                                      as ImageProvider, // picked file
                                      fit: BoxFit.fill)),
                              child: Stack(children: [
                                Positioned(
                                  right: 0,
                                  child: Container(
                                    child: IconButton(
                                      iconSize: 30,
                                      icon: const Icon(
                                        Icons.delete,
                                        color: Colors.red,
                                      ),
                                      onPressed: () async {
                                        showDialog(
                                          barrierDismissible: false,
                                          context: context,
                                          builder: (BuildContext context) => AlertDialog(
                                            title:
                                            const Text('Do you want to delete image?',
                                                style: TextStyle(
                                                  color: primaryColor,
                                                  fontSize: 20,
                                                )),
                                            actionsAlignment:
                                            MainAxisAlignment.spaceBetween,
                                            actions: [
                                              TextButton(
                                                onPressed: () async {
                                                  AppSettings.preLoaderDialog(context);

                                                  String fileName = widget.recordDetails
                                                      .reportImages[index]['url']
                                                      .split('/')
                                                      .last;

                                                  var payload = new Map<String, dynamic>();
                                                  payload["urlType"] = 'reports';
                                                  payload["url"] = widget.recordDetails
                                                      .reportImages[index]['url'];

                                                  try {
                                                    var res =
                                                    await AppSettings.deleteRecordsNew(
                                                        payload,
                                                        widget.recordDetails.recordId,widget.customerId);
                                                    print(jsonDecode(res));
                                                    Navigator.of(context,
                                                        rootNavigator: true)
                                                        .pop();
                                                    Navigator.of(context).pop(true);
                                                    AppSettings.longSuccessToast(
                                                        "Image deleted Successfully");
                                                    setState(() {
                                                      widget.recordDetails.reportImages =
                                                      jsonDecode(res)['remainingUrls'];
                                                    });
                                                  } catch (e) {
                                                    print(e);
                                                    Navigator.of(context,
                                                        rootNavigator: true)
                                                        .pop();
                                                    Navigator.of(context).pop(true);
                                                    AppSettings.longFailedToast(
                                                        "Image deletion failed");
                                                  }
                                                },
                                                child: const Text('Yes',
                                                    style: TextStyle(
                                                      color: primaryColor,
                                                      fontSize: 20,
                                                    )),
                                              ),
                                              TextButton(
                                                onPressed: () {
                                                  Navigator.of(context).pop(true);
                                                },
                                                child: const Text('No',
                                                    style: TextStyle(
                                                      color: primaryColor,
                                                      fontSize: 20,
                                                    )),
                                              ),
                                            ],
                                          ),
                                        );
                                      },
                                    ),
                                    /* color: Colors.pinkAccent,
                      width: 35,
                      height: 35,*/
                                  ),
                                )
                              ]),
                            ),
                          ),
                        )

                      ],
                    );
                  }),),
              IconButton(
                iconSize: 40,
                icon: const Icon(
                  Icons.add,
                  color: Colors.green,
                ),
                onPressed: () async {
                  showModalBottomSheet<void>(
                      context: context,
                      builder: (BuildContext context) {
                        return SizedBox(
                          height: 200,
                          child: Center(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                GestureDetector(
                                  child: Icon(
                                    Icons.camera_alt_outlined,
                                    size: 100,
                                    color: primaryColor,
                                  ),
                                  onTap: () async {
                                    await takeImageFromCameraForUpdateReports();
                                    Navigator.pop(context);
                                  },
                                ),
                                SizedBox(
                                  width: MediaQuery.of(context).size.width *
                                      .20,
                                ),
                                GestureDetector(
                                  child: Icon(
                                    Icons.photo,
                                    size: 100,
                                    color: primaryColor,
                                  ),
                                  onTap: () async {
                                    await pickImageFromGalleryForUpdateReports();
                                    Navigator.pop(context);
                                  },
                                ),
                              ],
                            ),
                          ),
                        );
                      });
                },
              )

            ],
          )
      );
    }
    else{
      return GestureDetector(
        onTap: (){

          showModalBottomSheet<void>(
              context: context,
              builder: (BuildContext context) {
                return SizedBox(
                  height: 200,
                  child: Center(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        GestureDetector(
                          child: Icon(
                            Icons.camera_alt_outlined,
                            size: 100,
                            color: primaryColor,
                          ),
                          onTap: () async {
                            await takeImageFromCameraForUpdateReports();
                            Navigator.pop(context);
                          },
                        ),
                        SizedBox(
                          width:
                          MediaQuery.of(context).size.width * .20,
                        ),
                        GestureDetector(
                          child: Icon(
                            Icons.photo,
                            size: 100,
                            color: primaryColor,
                          ),
                          onTap: () async {
                            await pickImageFromGalleryForUpdateReports();
                            Navigator.pop(context);
                          },
                        ),
                      ],
                    ),
                  ),
                );
              });
        },
        child: Row(
          children: [
          Icon(
          Icons.add,
          color: Colors.green,
          size: 40,
        ),
            Text('Add Reports',style: textButtonStyle(),)
          ],
        ),
      );
    }



  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppSettings.appBar('Records'),
      body: Padding(
        padding: EdgeInsets.all(10),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Container(
              height: MediaQuery.of(context).size.height * .15,
              width: double.infinity,
              child: Padding(
                padding: EdgeInsets.all(0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                        'Problem: ' +
                            widget.recordDetails.problem.toString().toUpperCase(),
                        style: problemTextStyle()),
                    Text(widget.recordDetails.doctorName.toString().toUpperCase(),
                        style: valuesTextStyle()),
                    Text(
                        widget.recordDetails.hospitalName
                            .toString()
                            .toUpperCase(),
                        style: valuesTextStyle()),
                    Text(widget.recordDetails.date.toString().toUpperCase(),
                        style: valuesTextStyle()),
                    Text('Patient details: ', style: problemTextStyle()),
                    Text(
                        widget.recordDetails.patient_name
                            .toString()
                            .toUpperCase(),
                        style: valuesTextStyle()),
                    Row(
                      children: [
                        Text(widget.recordDetails.gender.toString().toUpperCase(),
                            style: valuesTextStyle()),
                        SizedBox(
                          width: MediaQuery.of(context).size.width * .05,
                        ),
                        Text(
                            widget.recordDetails.age.toString().toUpperCase() +
                                " Yrs",
                            style: valuesTextStyle()),
                      ],
                    ),
                  ],
                ),
              ),
            ),
            Text('Findings',style: TextStyle(color: Colors.red,fontSize: 16,fontWeight: FontWeight.bold),),
            findings(widget.recordDetails),
            Text('Reports',style: TextStyle(color: Colors.red,fontSize: 16,fontWeight: FontWeight.bold),),
            reports(widget.recordDetails),
            Text('Prescriptions',style: TextStyle(color: Colors.red,fontSize: 16,fontWeight: FontWeight.bold),),
            prescriptions(widget.recordDetails)
          ],
        ),
      ),
    );
  }
}
