import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/viewModel/authentication_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/model/pharmacy-intervention-model/new_medication_res_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/utils/date-utils.dart';
import 'package:doctor_app_flutter/utils/dr_app_shared_pref.dart';
import 'package:doctor_app_flutter/utils/dr_app_toast_msg.dart';
import 'package:doctor_app_flutter/utils/utils.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/app_bar/patient-profile-app-bar.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/card_with_bg_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/errors/error_message.dart';
import 'package:doctor_app_flutter/widgets/shared/loader/gif_loader_dialog_utils.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../../../config/config.dart';
import '../../../../core/viewModel/profile/intervention_medication_view_model.dart';
import '../../../../widgets/shared/user-guid/CusomRow.dart';


DrAppSharedPreferances sharedPref = new DrAppSharedPreferances();

class InterventionMedicationHistoryScreen extends StatefulWidget {
  final PatiantInformtion patient;
  final InterventionMedicationResModel interventionMedication;
  const InterventionMedicationHistoryScreen(this.patient, this.interventionMedication, {Key? key}) : super(key: key);

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

class _InterventionMedicationHistoryScreenState extends State<InterventionMedicationHistoryScreen> {
  bool isDischargedPatient = false;
  AuthenticationViewModel authenticationViewModel = AuthenticationViewModel();
  late ProjectViewModel projectViewModel;

  @override
  Widget build(BuildContext context) {
    authenticationViewModel = Provider.of(context);
    projectViewModel = Provider.of(context);

    return BaseView<InterventionMedicationViewModel>(
      onModelReady: (model) => model.getInterventionMedicationHistory(
        patientId: widget.patient.patientId,
        projectId: widget.interventionMedication.projectID,
        admissionNo: widget.interventionMedication.admissionNo,
        prescriptionNo: widget.interventionMedication.prescriptionNo,
        orderNo: widget.interventionMedication.orderNo,
      ),
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        backgroundColor: Theme.of(context).scaffoldBackgroundColor,
        appBar: PatientProfileAppBar(
          widget.patient,
          isInpatient: true,
        ),
        body: model.allInterventionHistoryList == null ||
            model.allInterventionHistoryList.length == 0
            ? Center(
          child: ErrorMessage(
            error: TranslationBase.of(context).noDataAvailable,
          ),
        )
            : Container(
          color: Colors.grey[200],
          child: Column(
            children: <Widget>[
              Expanded(
                child: Container(
                  child: ListView.builder(
                      itemCount: model.allInterventionHistoryList.length,
                      itemBuilder: (BuildContext ctxt, int index) {
                        return FractionallySizedBox(
                          widthFactor: 0.95,
                          child: CardWithBgWidget(
                            hasBorder: false,
                            bgColor: Colors.transparent,
                            widget: Column(
                              children: [
                                Column(
                                  crossAxisAlignment:
                                  CrossAxisAlignment.start,
                                  children: [
                                    SizedBox(
                                      height: 10,
                                    ),
                                    Row(
                                      mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                      crossAxisAlignment:
                                      CrossAxisAlignment.start,
                                      children: [
                                        Container(
                                          width: MediaQuery.of(context)
                                              .size
                                              .width *
                                              0.60,
                                          child: Column(
                                            crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                            children: [
                                              CustomRow(
                                                labelSize: 12,
                                                valueSize: 12,
                                                label: TranslationBase.of(context).createdByName,
                                                value: model.allInterventionHistoryList[index].createdByName!,
                                              ),
                                            ],
                                          ),
                                        ),
                                        Column(
                                          children: [
                                            Row(
                                              crossAxisAlignment:
                                              CrossAxisAlignment
                                                  .start,
                                              children: [
                                                AppText(

                                                  AppDateUtils.getDayMonthYearDateFormatted(
                                                      AppDateUtils.convertStringToDate(
                                                          model
                                                              .allInterventionHistoryList[
                                                          index].createdOn!)
                                                      ,isArabic:
                                                  projectViewModel
                                                      .isArabic,
                                                      isMonthShort: true),
                                                  fontWeight: FontWeight.w600,
                                                  fontSize: 14,
                                                  color: Color(0xFF2B353E),
                                                ),
                                              ],
                                            ),
                                          ],
                                        )
                                      ],
                                    ),
                                    SizedBox(
                                      height: 8,
                                    ),
                                    CustomRow(
                                      labelSize: 12,
                                      valueSize: 12,
                                      label: TranslationBase.of(context).remarks,
                                      value: model.allInterventionHistoryList[index].remark!,
                                    ),
                                    SizedBox(
                                      height: 8,
                                    ),
                                    SizedBox(
                                      height: 8,
                                    ),
                                    SizedBox(
                                      height: 8,
                                    ),
                                    Row(
                                      crossAxisAlignment:
                                      CrossAxisAlignment.start,
                                      mainAxisAlignment:
                                      MainAxisAlignment.end,
                                      children: [
                                        InkWell(
                                          onTap: () async {

                                            GifLoaderDialogUtils.showMyDialog(context);
                                            await model.setAcceptedOrRejected(
                                              remarks: "",
                                              memberId: model.allInterventionHistoryList[index].commentedBy,
                                              prescriptionNo: model.allInterventionHistoryList[index].prescriptionId,
                                              patientID: model.allInterventionHistoryList[index].patientId,
                                              orderNo: model.allInterventionHistoryList[index].orderNo,
                                              admissionNo: model.allInterventionHistoryList[index].admissionNo,
                                              interventionStatus: 1,
                                              status: 61
                                            );
                                            if(model.state == ViewState.ErrorLocal) {
                                              GifLoaderDialogUtils.hideDialog(context);

                                              Utils.showErrorToast(model.error);
                                            } else {
                                              await model.getInterventionMedicationHistory(patientId: widget.patient.patientId,
                                                projectId: widget.interventionMedication.projectID,
                                                admissionNo: widget.interventionMedication.admissionNo,
                                                prescriptionNo: widget.interventionMedication.prescriptionNo,
                                                orderNo: widget.interventionMedication.orderNo,);
                                              GifLoaderDialogUtils.hideDialog(context);
                                              DrAppToastMsg.showSuccesToast(
                                                  "Has been Successfully accepted");
                                            }
                                          },
                                          child: Container(
                                            decoration: BoxDecoration(
                                              color: AppGlobal.appGreenColor,
                                              borderRadius:
                                              BorderRadius.circular(10),
                                            ),
                                            // color:Colors.red[600],

                                            child: Row(
                                              children: [

                                                SizedBox(
                                                  width: 2,
                                                ),
                                                AppText(
                                                  TranslationBase.of(context)
                                                      .accept,
                                                  fontSize: 10,
                                                  color: Colors.white,
                                                ),
                                              ],
                                            ),
                                            padding: EdgeInsets.all(6),
                                          ),
                                        ),
                                        SizedBox(
                                          width: 10,
                                        ),
                                        InkWell(
                                          onTap: () async {

                                            GifLoaderDialogUtils.showMyDialog(context);
                                            await model.setAcceptedOrRejected(
                                              remarks: "",
                                              memberId: model.allInterventionHistoryList[index].commentedBy,
                                              prescriptionNo: model.allInterventionHistoryList[index].prescriptionId,
                                              patientID: model.allInterventionHistoryList[index].patientId,
                                              orderNo: model.allInterventionHistoryList[index].orderNo,
                                              admissionNo: model.allInterventionHistoryList[index].admissionNo,
                                              interventionStatus: 2,
                                              status: 61
                                            );
                                            if(model.state == ViewState.ErrorLocal) {
                                              GifLoaderDialogUtils.hideDialog(context);

                                              Utils.showErrorToast(model.error);
                                            } else {
                                              await model.getInterventionMedicationHistory(patientId: widget.patient.patientId,
                                                projectId: widget.interventionMedication.projectID,
                                                admissionNo: widget.interventionMedication.admissionNo,
                                                prescriptionNo: widget.interventionMedication.prescriptionNo,
                                                orderNo: widget.interventionMedication.orderNo,);
                                              GifLoaderDialogUtils.hideDialog(context);

                                              DrAppToastMsg.showSuccesToast(
                                                  "Has been Successfully Rejected");
                                            }
                                          },
                                          child: Container(
                                            decoration: BoxDecoration(
                                              color: AppGlobal.appRedColor,
                                              borderRadius:
                                              BorderRadius.circular(10),
                                            ),
                                            // color:Colors.red[600],

                                            child: Row(
                                              children: [

                                                SizedBox(
                                                  width: 2,
                                                ),
                                                AppText(
                                                  TranslationBase.of(context)
                                                      .reject,
                                                  fontSize: 10,
                                                  color: Colors.white,
                                                ),
                                              ],
                                            ),
                                            padding: EdgeInsets.all(6),
                                          ),
                                        ),
                                      ],
                                    ),
                                  ],
                                ),
                                SizedBox(
                                  height: 20,
                                ),
                              ],
                            ),
                          ),
                        );
                      }),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
