import 'package:diplomaticquarterapp/core/enum/viewstate.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/Prescriptions.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/prescription_report.dart';
import 'package:diplomaticquarterapp/core/model/prescriptions/prescription_report_enh.dart';
import 'package:diplomaticquarterapp/core/service/AlHabibMedicalService/customer_addresses_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/medical/PrescriptionDeliveryViewModel.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/dialogs/ConfirmWithMessageDialog.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class PrescriptionOrderOverview extends StatelessWidget {
  final Prescriptions prescriptions;
  final List<PrescriptionReport> prescriptionReportList;
  final List<PrescriptionReportEnh> prescriptionReportEnhList;
  final AddressInfo selectedAddress;
  final double latitude;
  final double longitude;
  PrescriptionOrderOverview(
      {Key key,
      this.prescriptions,
      this.prescriptionReportList,
      this.prescriptionReportEnhList, this.selectedAddress, this.latitude, this.longitude});

  @override
  Widget build(BuildContext context) {
    return BaseView<PrescriptionDeliveryViewModel>(
      builder: (_, model, widget) => AppScaffold(
        isShowAppBar: true,
        appBarTitle: TranslationBase.of(context).orderOverview,
        baseViewModel: model,
        body: Container(
          height: MediaQuery.of(context).size.height * 0.8,
          child: Column(
            children: [
              if (!prescriptions.isInOutPatient)
                ...List.generate(
                    prescriptionReportList.length,
                    (index) => 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(
                                  prescriptionReportList[index].imageSRCUrl,
                                  fit: BoxFit.cover,
                                  width: 60,
                                  height: 70,
                                ),
                              ),
                              SizedBox(
                                width: 10,
                              ),
                              Expanded(
                                  child: Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Center(
                                    child: Texts(prescriptionReportList[index]
                                            .itemDescription
                                            .isNotEmpty
                                        ? prescriptionReportList[index]
                                            .itemDescription
                                        : prescriptionReportList[index]
                                            .itemDescriptionN)),
                              )),
                              Icon(
                                Icons.arrow_forward_ios,
                                size: 18,
                                color: Colors.grey[500],
                              )
                            ],
                          ),
                        ))
              else
                ...List.generate(
                  prescriptionReportEnhList.length,
                  (index) => Container(
                    margin: EdgeInsets.all(8.0),
                    color: Colors.white,
                    child: Row(
                      children: <Widget>[
                        ClipRRect(
                          borderRadius: BorderRadius.all(Radius.circular(5)),
                          child: Image.network(
                            prescriptionReportEnhList[index].imageSRCUrl,
                            fit: BoxFit.cover,
                            width: 60,
                            height: 70,
                          ),
                        ),
                        SizedBox(
                          width: 10,
                        ),
                        Expanded(
                          child: Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Texts(prescriptionReportEnhList[index]
                                    .itemDescription),
                              ],
                            ),
                          ),
                        ),
                        Icon(
                          Icons.arrow_forward_ios,
                          size: 18,
                          color: Colors.grey[500],
                        )
                      ],
                    ),
                  ),
                )
            ],
          ),
        ),
        bottomSheet: Container(
          width: double.infinity,
          height: MediaQuery.of(context).size.height * 0.15,
          color: Colors.grey[100],
          child: Column(
            children: <Widget>[
              SizedBox(height: 12,),
              Container(
                  width: MediaQuery.of(context).size.width * 0.8,
                  child: SecondaryButton(
                    label: TranslationBase.of(context).submit.toUpperCase(),
                    color: Colors.green[800],
                    onTap: () async {
                      showDialog(
                        context: context,
                        child: ConfirmWithMessageDialog(
                          message: TranslationBase.of(context).confirmPrescription,
                          okTitle: TranslationBase.of(context).ok,
                          onTap: () async {
                            GifLoaderDialogUtils.showMyDialog(context);
                            await model.insertDeliveryOrder(lineItemNo: 0,
                                longitude: longitude,
                                latitude: latitude,appointmentNo: prescriptions.appointmentNo,
                                dischargeID: prescriptions.dischargeNo,createdBy: model.user.patientID)
                                .then((value) {
                              GifLoaderDialogUtils.hideDialog(context);
                              if (model.state == ViewState.ErrorLocal)
                                showErrorDialog(context,model.error);
                              else
                                {
                                  Navigator.pop(context);
                                  Navigator.pop(context);
                                  Navigator.pop(context);
                                }

                            }).catchError((e) {
                              GifLoaderDialogUtils.hideDialog(context);
                            });
                          },
                        ),
                      );

                    },
                  ))
            ],
          ),
        ),
      ),
    );
  }

  void showErrorDialog(BuildContext context,String error) {
    showDialog(
      context: context,
      child: ConfirmWithMessageDialog(
        message: TranslationBase.of(context).youAlreadyHaveOrder,
        okTitle: TranslationBase.of(context).orderOverview,
        onTap: () {
          Navigator.pop(context);
          Navigator.pop(context);
          Navigator.pop(context);

        },
      ),
    );

  }

}
