import 'package:diplomaticquarterapp/core/model/prescriptions/prescription_report.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/prescription_report_enh.dart';
import 'package:diplomaticquarterapp/models/Appointments/AppoimentAllHistoryResultList.dart';
import 'package:diplomaticquarterapp/pages/medical/prescriptions/prescription_details_page.dart';
import 'package:diplomaticquarterapp/services/appointment_services/GetDoctorsList.dart';
import 'package:diplomaticquarterapp/uitl/app_toast.dart';
import 'package:smart_progress_bar/smart_progress_bar.dart';
import 'package:diplomaticquarterapp/widgets/buttons/button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';

class PrescriptionReportPage extends StatefulWidget {
  List<PrescriptionReportEnh> prescriptionReportEnhList;
  dynamic listPres;
  AppoitmentAllHistoryResultList appo;

  PrescriptionReportPage({@required this.prescriptionReportEnhList, @required this.listPres, @required this.appo});

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

class _PrescriptionReportState extends State<PrescriptionReportPage> {
  @override
  void initState() {
    print(widget.prescriptionReportEnhList.length);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      isShowAppBar: true,
      appBarTitle: 'Items',
      body: Container(
        height: MediaQuery.of(context).size.height * 0.8,
        child: ListView.builder(
          itemBuilder: (context, index) => InkWell(
            onTap: () {
              navigateToPrescriptionDetails(
                  widget.prescriptionReportEnhList[index]);
            },
            child: Container(
              width: double.infinity,
              margin: EdgeInsets.only(top: 10, left: 10, right: 10),
              padding: EdgeInsets.all(8.0),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.all(
                  Radius.circular(10.0),
                ),
                border: Border.all(color: Colors.grey[200], width: 0.5),
              ),
              child: Row(
                children: <Widget>[
                  ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(5)),
                    child: Image.network(
                      widget.prescriptionReportEnhList[index].imageSRCUrl,
                      fit: BoxFit.cover,
                      width: 60,
                      height: 70,
                    ),
                  ),
                  Expanded(
                      child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Center(
                        child: Texts(widget
                            .prescriptionReportEnhList[index].itemDescription)),
                  )),
                  Icon(
                    Icons.arrow_forward_ios,
                    size: 18,
                    color: Colors.grey[500],
                  )
                ],
              ),
            ),
          ),
          itemCount: widget.prescriptionReportEnhList.length,
        ),
      ),
      bottomSheet: Container(
        width: double.infinity,
        height: MediaQuery.of(context).size.height * 0.2,
        color: Colors.grey[100],
        child: Column(
          children: <Widget>[
            Divider(),
            Container(
              width: MediaQuery.of(context).size.width * 0.8,
              child: Button(
                label: 'Send Copy',
                onTap: () {
                  sendPrescriptionReportEmail();
                },
              ),
            ),
            Container(
                width: MediaQuery.of(context).size.width * 0.8,
                child: Button(
                  label: 'Resend order & deliver',
                  backgroundColor: Colors.green[200],
                ))
          ],
        ),
      ),
    );
  }

  sendPrescriptionReportEmail() {
    DoctorsListService service = new DoctorsListService();
    service.sendPrescriptionEmail(widget.appo.appointmentDate, widget.appo.setupID, widget.listPres, context).then((res) {
      AppToast.showSuccessToast(message: 'A copy has been sent to the e-mail');
    }).catchError((err) {
      print(err);
      AppToast.showErrorToast(message: err);
    }).showProgressBar(text: "Loading", backgroundColor: Colors.blue.withOpacity(0.6));
  }

  navigateToPrescriptionDetails(PrescriptionReportEnh prescriptionReportEnh) {
    final PrescriptionReport prescriptionReport = new PrescriptionReport();

    prescriptionReport.imageSRCUrl = prescriptionReportEnh.imageSRCUrl;
    prescriptionReport.itemDescription = prescriptionReportEnh.itemDescription;
    prescriptionReport.itemID = prescriptionReportEnh.itemID;

    prescriptionReport.routeN = prescriptionReportEnh.route;
    prescriptionReport.route = prescriptionReportEnh.route;

    prescriptionReport.frequency = prescriptionReportEnh.frequency;
    prescriptionReport.frequencyN = prescriptionReportEnh.frequency;

    prescriptionReport.doseDailyQuantity =
        prescriptionReportEnh.doseDailyQuantity;

    prescriptionReport.days = prescriptionReportEnh.days;

    Navigator.push(
      context,
      FadePage(
        page: PrescriptionDetailsPage(
          prescriptionReport: prescriptionReport,
        ),
      ),
    );
  }
}
