import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/prescriptions/Prescriptions.dart';
import 'package:hmg_patient_app/core/model/prescriptions/prescription_report.dart';
import 'package:hmg_patient_app/core/viewModels/medical/prescriptions_view_model.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/extensions/string_extensions.dart';
import 'package:hmg_patient_app/main.dart';
import 'package:hmg_patient_app/models/header_model.dart';
import 'package:hmg_patient_app/pages/base/base_view.dart';
import 'package:hmg_patient_app/pages/medical/prescriptions/prescription_details_inp.dart';
import 'package:hmg_patient_app/pages/medical/prescriptions/prescription_details_page.dart';
import 'package:hmg_patient_app/uitl/app_toast.dart';
import 'package:hmg_patient_app/uitl/date_uitl.dart';
import 'package:hmg_patient_app/uitl/gif_loader_dialog_utils.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/uitl/utils_new.dart';
import 'package:hmg_patient_app/widgets/buttons/defaultButton.dart';
import 'package:hmg_patient_app/widgets/my_rich_text.dart';
import 'package:hmg_patient_app/widgets/new_design/doctor_header.dart';
import 'package:hmg_patient_app/widgets/others/app_scaffold_widget.dart';
import 'package:hmg_patient_app/widgets/show_zoom_image_dialog.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../widgets/dialogs/confirm_send_email_dialog.dart';
import 'PrescriptionIDeliveryAddressPage.dart';

class PrescriptionItemsPage extends StatelessWidget {
  final Prescriptions prescriptions;
  late ProjectViewModel projectViewModel;

  PrescriptionItemsPage({Key? key, required this.prescriptions});

  @override
  Widget build(BuildContext context) {
    projectViewModel = Provider.of(context);
    return BaseView<PrescriptionsViewModel>(
      onModelReady: (model) => model.getPrescriptionReport(prescriptions: prescriptions),
      builder: (_, model, widget) => AppScaffold(
        isShowAppBar: true,
        appBarTitle: prescriptions.doctorName!,
        baseViewModel: model,
        showNewAppBar: true,
        showNewAppBarTitle: true,
        body: model.state != ViewState.Busy
            ? Column(
                children: [
                  Expanded(
                    child: SingleChildScrollView(
                      physics: BouncingScrollPhysics(),
                      child: Column(
                        children: [
                          DoctorHeader(
                            headerModel: HeaderModel(
                              prescriptions.doctorName!,
                              prescriptions.doctorID!,
                              prescriptions.doctorImageURL!,
                              prescriptions.speciality ?? [""],
                              "",
                              prescriptions.name!,
                              DateUtil.convertStringToDate(prescriptions.appointmentDate!),
                              DateUtil.formatDateToTime(
                                model.prescriptionReportEnhList.length > 0 || model.prescriptionReportListINP.length > 0
                                    ? DateUtil.convertStringToDate(
                                        model.prescriptionReportEnhList.length > 0 ? model.prescriptionReportEnhList[0].orderDate! : model.prescriptionReportListINP[0].orderDate!,
                                      )
                                    : DateTime.now(),
                              ),
                              prescriptions.nationalityFlagURL ?? "",
                              prescriptions.doctorRate,
                              prescriptions.actualDoctorRate,
                              prescriptions.noOfPatientsRate,
                              projectViewModel.user.emailAddress!,
                            ),
                            isNeedToShowButton: (model.prescriptionReportListINP.length > 0 || model.prescriptionReportEnhList.length > 0) ? projectViewModel.havePrivilege(13) : false,
                            buttonTitle: TranslationBase.of(context).download,
                            buttonIcon: "assets/images/new/download_1.svg",
                            showConfirmMessageDialog: false,
                            isDownload: true,
                            onTap: () async {
                              // showConfirmMessage(context, model);
                              GifLoaderDialogUtils.showMyDialog(context);
                              await model.sendPrescriptionEmail(
                                  appointmentDate: prescriptions.appointmentDate!,
                                  patientID: prescriptions.patientID!,
                                  clinicName: prescriptions.companyName!,
                                  doctorName: prescriptions.doctorName!,
                                  doctorID: prescriptions.doctorID!,
                                  mes: TranslationBase.of(context).sendSuc,
                                  projectID: prescriptions.projectID!,
                                  isInOutPatient: prescriptions.isInOutPatient!,
                                  isDownload: true);
                              GifLoaderDialogUtils.hideDialog(context);
                            },
                          ),
                          if (!prescriptions.isInOutPatient!)
                            model.prescriptionReportListINP.length > 0
                                ? ListView.separated(
                                    itemBuilder: (context, index) {
                                      return InkWell(
                                        onTap: () {
                                          Navigator.push(
                                            context,
                                            FadePage(
                                              page: PrescriptionDetailsPageINP(
                                                prescriptionReport: model.prescriptionReportListINP[index],
                                                prescriptions: prescriptions,
                                              ),
                                            ),
                                          );
                                        },
                                        child: Container(
                                          width: double.infinity,
                                          child: Container(
                                            margin: EdgeInsets.only(left: 16, right: 16, top: index == 0 ? 16 : 4, bottom: 4),
                                            decoration: cardRadius(12),
                                            child: Padding(
                                              padding: EdgeInsets.only(top: 12, bottom: 20, left: projectViewModel.isArabic ? 12 : 20, right: projectViewModel.isArabic ? 20 : 12),
                                              child: Row(
                                                children: [
                                                  Flexible(
                                                    child: Column(
                                                      crossAxisAlignment: CrossAxisAlignment.start,
                                                      children: [
                                                        Text(
                                                          model.prescriptionReportListINP[index].itemDescription!.isNotEmpty
                                                              ? model.prescriptionReportListINP[index].itemDescription!
                                                              : model.prescriptionReportListINP[index].itemDescriptionN ?? '',
                                                          style: TextStyle(
                                                            fontSize: 16,
                                                            letterSpacing: -0.64,
                                                            fontWeight: FontWeight.w600,
                                                          ),
                                                        ),
                                                        mHeight(10),
                                                        Row(
                                                          children: [
                                                            // InkWell(
                                                            //   child: Stack(
                                                            //     alignment: Alignment.center,
                                                            //     children: [
                                                            //       Container(
                                                            //         child: Image.network(
                                                            //           model.prescriptionReportListINP[index].imageSRCUrl,
                                                            //           fit: BoxFit.cover,
                                                            //           width: 60,
                                                            //           height: 60,
                                                            //         ),
                                                            //         margin: EdgeInsets.zero,
                                                            //         clipBehavior: Clip.antiAlias,
                                                            //         decoration: cardRadius(2000),
                                                            //       ),
                                                            //       Container(
                                                            //         child: Icon(
                                                            //           Icons.search,
                                                            //           size: 18,
                                                            //           color: Colors.white,
                                                            //         ),
                                                            //         padding: EdgeInsets.all(6),
                                                            //         decoration: containerRadius(Colors.black.withOpacity(0.3), 200),
                                                            //       )
                                                            //     ],
                                                            //   ),
                                                            //   onTap: () {
                                                            //     showZoomImageDialog(
                                                            //       context,
                                                            //       model.prescriptionReportListINP[index].imageSRCUrl,
                                                            //     );
                                                            //   },
                                                            // ),
                                                            // mWidth(12),
                                                            Column(
                                                              crossAxisAlignment: CrossAxisAlignment.start,
                                                              children: [
                                                                Row(
                                                                  children: [
                                                                    Text(
                                                                      TranslationBase.of(context).route + ": ",
                                                                      style: TextStyle(
                                                                        color: Colors.grey,
                                                                        fontSize: 10,
                                                                        letterSpacing: -0.4,
                                                                        fontWeight: FontWeight.w600,
                                                                      ),
                                                                    ),
                                                                    Text(
                                                                      model.prescriptionReportListINP[index].route!,
                                                                      style: TextStyle(
                                                                        fontSize: 12,
                                                                        letterSpacing: -0.48,
                                                                        fontWeight: FontWeight.w600,
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    Text(
                                                                      TranslationBase.of(context).dailyDoses + ": ",
                                                                      style: TextStyle(
                                                                        color: Colors.grey,
                                                                        fontSize: 10,
                                                                        letterSpacing: -0.4,
                                                                        fontWeight: FontWeight.w600,
                                                                      ),
                                                                    ),
                                                                    Text(
                                                                      model.prescriptionReportListINP[index].doseDailyQuantity.toString(),
                                                                      style: TextStyle(
                                                                        fontSize: 12,
                                                                        letterSpacing: -0.48,
                                                                        fontWeight: FontWeight.w600,
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                mHeight(12),
                                                                Text(
                                                                  model.prescriptionReportListINP[index].remarks!,
                                                                  style: TextStyle(
                                                                    color: Colors.grey,
                                                                    fontSize: 10,
                                                                    letterSpacing: -0.4,
                                                                    fontWeight: FontWeight.w600,
                                                                  ),
                                                                ),
                                                              ],
                                                            )
                                                          ],
                                                        ),
                                                      ],
                                                    ),
                                                  ),
                                                  Icon(
                                                    Icons.arrow_forward,
                                                    size: 16,
                                                  ),
                                                ],
                                              ),
                                            ),
                                          ),
                                        ),
                                      );
                                    },
                                    separatorBuilder: (context, index) {
                                      return mHeight(8);
                                    },
                                    physics: NeverScrollableScrollPhysics(),
                                    shrinkWrap: true,
                                    itemCount: model.prescriptionReportListINP.length)
                                : Padding(
                                    padding: const EdgeInsets.only(top: 150.0),
                                    child: getNoDataWidget(context),
                                  )
                          else
                            model.prescriptionReportEnhList.length > 0
                                ? ListView.separated(
                                    padding: EdgeInsets.all(21),
                                    itemBuilder: (context, index) {
                                      return InkWell(
                                        onTap: () {
                                          PrescriptionReport prescriptionReport = PrescriptionReport(
                                              imageSRCUrl: model.prescriptionReportEnhList[index].imageSRCUrl,
                                              itemDescription: model.prescriptionReportEnhList[index].itemDescription,
                                              itemDescriptionN: model.prescriptionReportEnhList[index].itemDescription,
                                              routeN: model.prescriptionReportEnhList[index].route,
                                              frequency: model.prescriptionReportEnhList[index].frequency,
                                              frequencyN: model.prescriptionReportEnhList[index].frequency,
                                              doseDailyQuantity: model.prescriptionReportEnhList[index].doseDailyQuantity,
                                              days: model.prescriptionReportEnhList[index].days,
                                              itemID: model.prescriptionReportEnhList[index].itemID,
                                              orderDate: model.prescriptionReportEnhList[index].orderDate,
                                              startDate: model.prescriptionReportEnhList[index].startDate,
                                              remarks: model.prescriptionReportEnhList[index].remarks);
                                          Navigator.push(
                                            context,
                                            FadePage(
                                              page: PrescriptionDetailsPage(
                                                prescriptionReport: prescriptionReport,
                                                prescriptions: prescriptions,
                                              ),
                                            ),
                                          );
                                        },
                                        child: Container(
                                          decoration: BoxDecoration(
                                            color: Colors.white,
                                            borderRadius: BorderRadius.all(
                                              Radius.circular(10.0),
                                            ),
                                            boxShadow: [
                                              BoxShadow(
                                                color: Color(0xff000000).withOpacity(.05),
                                                blurRadius: 27,
                                                offset: Offset(0, -3),
                                              ),
                                            ],
                                          ),
                                          child: Padding(
                                            padding: EdgeInsets.only(top: 12, bottom: 20, left: projectViewModel.isArabic ? 12 : 20, right: projectViewModel.isArabic ? 20 : 12),
                                            child: Row(
                                              children: [
                                                Flexible(
                                                  child: Column(
                                                    crossAxisAlignment: CrossAxisAlignment.start,
                                                    children: [
                                                      Text(
                                                        (model.prescriptionReportEnhList[index].itemDescription!.isNotEmpty
                                                                ? model.prescriptionReportEnhList[index].itemDescription
                                                                : model.prescriptionReportEnhList[index].itemDescriptionN ?? '')!
                                                            .toLowerCase()
                                                            .capitalizeFirstofEach,
                                                        style: TextStyle(
                                                          fontSize: 16,
                                                          color: Color(0xff2E303A),
                                                          letterSpacing: -0.64,
                                                          fontWeight: FontWeight.w600,
                                                        ),
                                                      ),
                                                      mHeight(10),
                                                      Row(
                                                        crossAxisAlignment: CrossAxisAlignment.start,
                                                        children: [
                                                          // InkWell(
                                                          //   child: Stack(
                                                          //     alignment: Alignment.center,
                                                          //     children: [
                                                          //       Container(
                                                          //         child: Image.network(
                                                          //           model.prescriptionReportEnhList[index].imageSRCUrl,
                                                          //           fit: BoxFit.cover,
                                                          //           width: 60,
                                                          //           height: 60,
                                                          //         ),
                                                          //         margin: EdgeInsets.zero,
                                                          //         clipBehavior: Clip.antiAlias,
                                                          //         decoration: cardRadius(2000),
                                                          //       ),
                                                          //       Container(
                                                          //         child: Icon(
                                                          //           Icons.search,
                                                          //           size: 18,
                                                          //           color: Colors.white,
                                                          //         ),
                                                          //         padding: EdgeInsets.all(6),
                                                          //         decoration: containerRadius(Colors.black.withOpacity(0.3), 200),
                                                          //       )
                                                          //     ],
                                                          //   ),
                                                          //   onTap: () {
                                                          //     showZoomImageDialog(
                                                          //       context,
                                                          //       model.prescriptionReportEnhList[index].imageSRCUrl,
                                                          //     );
                                                          //   },
                                                          // ),
                                                          // mWidth(12),
                                                          Expanded(
                                                            child: Column(
                                                              crossAxisAlignment: CrossAxisAlignment.start,
                                                              children: [
                                                                MyRichText(TranslationBase.of(context).route + ": ", model.prescriptionReportEnhList[index].route!, projectViewModel.isArabic),
                                                                MyRichText(TranslationBase.of(context).dailyDoses + ": ", model.prescriptionReportEnhList[index].doseDailyQuantity.toString(),
                                                                    projectViewModel.isArabic),
                                                                mHeight(9),
                                                                Text(
                                                                  model.prescriptionReportEnhList[index].remarks!,
                                                                  style: TextStyle(
                                                                    color: Color(0xff575757),
                                                                    fontSize: 12,
                                                                    letterSpacing: -0.4,
                                                                    fontWeight: FontWeight.w600,
                                                                  ),
                                                                ),
                                                              ],
                                                            ),
                                                          )
                                                        ],
                                                      ),
                                                    ],
                                                  ),
                                                ),
                                                Icon(
                                                  Icons.arrow_forward,
                                                  size: 16,
                                                ),
                                              ],
                                            ),
                                          ),
                                        ),
                                      );
                                    },
                                    separatorBuilder: (context, index) {
                                      return mHeight(12);
                                    },
                                    shrinkWrap: true,
                                    physics: NeverScrollableScrollPhysics(),
                                    itemCount: model.prescriptionReportEnhList.length)
                                : Padding(
                                    padding: const EdgeInsets.only(top: 150.0),
                                    child: getNoDataWidget(context),
                                  ),
                        ],
                      ),
                    ),
                  ),
                  Container(
                    color: Colors.white,
                    padding: EdgeInsets.only(top: 16, bottom: 16, right: 21, left: 21),
                    child: DefaultButton(
                      TranslationBase.of(context).resendOrder,
                      () {
                        if (model.isMedDeliveryAllowed == false) {
                          AppToast.showErrorToast(message: TranslationBase.of(context).prescriptionDeliveryError);
                        } else {
                          Navigator.push(
                            context,
                            FadePage(
                              page: PrescriptionDeliveryAddressPage(
                                prescriptions: prescriptions,
                                prescriptionReportList: model.prescriptionReportList,
                                prescriptionReportEnhList: model.prescriptionReportEnhList,
                              ),
                            ),
                          );
                        }
                      },
                      color: model.isMedDeliveryAllowed == false ? Color(0xff575757) : Color(0xff359846),
                      disabledColor: Color(0xff575757),
                    ),
                  ),
                ],
              )
            : Container(),
      ),
    );
  }

  void showConfirmMessage(BuildContext context, PrescriptionsViewModel model) {
    showDialog(
      context: context,
      builder: (cxt) => ConfirmSendEmailDialog(
        email: projectViewModel.user!.emailAddress!,
        onTapSendEmail: () {
          model.sendPrescriptionEmail(
              appointmentDate: prescriptions.appointmentDate!,
              patientID: prescriptions.patientID!,
              clinicName: prescriptions.companyName!,
              doctorName: prescriptions.doctorName!,
              doctorID: prescriptions.doctorID!,
              mes: TranslationBase.of(context).sendSuc,
              projectID: prescriptions.projectID!,
              isInOutPatient: prescriptions.isInOutPatient!,
              isDownload: true);
        },
      ),
    );
  }
}
