import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/viewModel/prescription/prescription_view_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/prescription/prescriptions_items/prescription_items_in_patient.dart';
import 'package:doctor_app_flutter/screens/prescription/prescriptions_items/prescription_items_out_patient.dart';
import 'package:doctor_app_flutter/utils/date-utils.dart';
import 'package:doctor_app_flutter/utils/translations_delegate_base_utils.dart';
import 'package:doctor_app_flutter/widgets/patients/patient_service_title.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/card_with_bg_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/doctor_card.dart';
import 'package:doctor_app_flutter/widgets/shared/user-guid/CusomRow.dart';
import 'package:doctor_app_flutter/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';

import '../../utils/utils.dart';
import '../../widgets/shared/errors/error_message.dart';

class OldPrescriptionsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final routeArgs = ModalRoute.of(context)!.settings.arguments as Map;
    PatiantInformtion patient = routeArgs['patient'];
    String patientType = routeArgs['patientType'];
    String arrivalType = routeArgs['arrivalType'];
    bool isInpatient = routeArgs['isInpatient'];
    bool isFromLiveCare = routeArgs['isFromLiveCare'];
    bool isVidaPlusProject = routeArgs['isVidaPlusProject'];
    var admissionNumber = isVidaPlusProject ?  patient.admissionID : patient.admissionNo;
    return BaseView<PrescriptionViewModel>(
        onModelReady: (model) async {
          admissionNumber == null  ? model.getPrescriptions(patient, patientType: patientType, isLocalBusy: false) : model.getMedicationForInPatient(patient, isVidaPlusProject);
        },
        builder: (_, model, w) => AppScaffold(
            baseViewModel: model,
            isShowAppBar: true,
            backgroundColor: Colors.grey[100]!,
            appBar: PatientProfileAppBar(
              patient,
              isInpatient: isInpatient,
            ),
            body: admissionNumber == null
                ? FractionallySizedBox(
                    widthFactor: 1.0,
                    child: SingleChildScrollView(
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: <Widget>[
                          SizedBox(
                            height: 12,
                          ),
                          Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                ServiceTitle(
                                  title: TranslationBase.of(context).orders,
                                  subTitle: TranslationBase.of(context).prescriptions + "(${TranslationBase.of(context).old}) ",
                                ),
                              ],
                            ),
                          ),
                          ListView.builder(
                              physics: BouncingScrollPhysics(),
                              itemCount: model.prescriptionsList.length,
                              shrinkWrap: true,
                              itemBuilder: (BuildContext ctxt, int index) {
                                return InkWell(
                                    onTap: () => Navigator.push(
                                          context,
                                          FadePage(
                                            page: PrescriptionItemsPage(
                                              prescriptions: model.prescriptionsList[index],
                                              patient: patient,
                                              arrivalType: arrivalType,
                                            ),
                                          ),
                                        ),
                                    child: Column(
                                      children: [
                                        DoctorCard(
                                          doctorName: Utils.convertToTitleCase(model.prescriptionsList[index].doctorName!),
                                          profileUrl: model.prescriptionsList[index].doctorImageURL,
                                          branch: model.prescriptionsList[index].name,
                                          clinic: model.prescriptionsList[index].clinicDescription,
                                          isPrescriptions: true,
                                          appointmentDate: AppDateUtils.getDateTimeFromServerFormat(
                                            model.prescriptionsList[index].appointmentDate!,
                                          ),
                                        ),
                                      ],
                                    ));
                              }),
                          if (model.prescriptionsList.isEmpty && patient.patientStatusType != 43)
                            Center(
                                child: ErrorMessage(
                              error: TranslationBase.of(context).noPrescriptionsFound,
                            ))
                        ],
                      ),
                    ),
                  )
                : FractionallySizedBox(
                      widthFactor: 0.9,
                      child: SingleChildScrollView(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            SizedBox(
                              height: 12,
                            ),
                            ServiceTitle(
                              title: TranslationBase.of(context).orders,
                              subTitle: TranslationBase.of(context).prescriptions + "(${TranslationBase.of(context).old}) ",
                            ),
                            model.medicationForInPatient.length == 0
                                ? Center(
                                    child: ErrorMessage(
                                    error: TranslationBase.of(context).noPrescriptionsFound,
                                  ))
                                : ListView.builder(
                                    scrollDirection: Axis.vertical,
                                    physics: NeverScrollableScrollPhysics(),
                                    shrinkWrap: true,
                                    itemCount: model.medicationForInPatient.length,
                                    itemBuilder: (context, index) {
                                      //model.medicationForInPatient.length,
                                      return InkWell(
                                        child: CardWithBgWidget(
                                          bgColor: Colors.transparent,
                                          widget: Column(
                                            children: [
                                              CustomRow(
                                                label: "",
                                                value: Utils.convertToTitleCase(model.medicationForInPatient[index].pHRItemDescription!),
                                                valueSize: SizeConfig.getTextMultiplierBasedOnWidth() * 3.5,
                                              ),
                                              SizedBox(
                                                height: 10,
                                              ),
                                              CustomRow(
                                                label: "Doctor Name :",
                                                value: Utils.convertToTitleCase(model.medicationForInPatient[index].doctorName.toString()),
                                              ),
                                              CustomRow(
                                                label: "Status :",
                                                value: Utils.convertToTitleCase(model.medicationForInPatient[index].statusDescription.toString()),
                                              ),
                                              CustomRow(
                                                label: "dose :",
                                                value: Utils.convertToTitleCase(model.medicationForInPatient[index].dose.toString()),
                                              ),
                                              CustomRow(
                                                label: "comments :",
                                                value: Utils.convertToTitleCase(model.medicationForInPatient[index].comments.toString()),
                                              ),
                                            ],
                                          ),
                                        ),
                                        onTap: () => Navigator.push(
                                          context,
                                          FadePage(
                                            page: PrescriptionItemsInPatientPage(
                                              prescriptionIndex: index,
                                              prescriptions: model.medicationForInPatient[index],
                                              patient: patient,
                                              patientType: patientType,
                                              arrivalType: arrivalType,
                                              isVidaPlusProject: isVidaPlusProject,
                                              startOn: AppDateUtils.getDateTimeFromServerFormat(
                                                model.medicationForInPatient[index].startDatetime!,
                                              ),
                                              stopOn: AppDateUtils.getDateTimeFromServerFormat(
                                                model.medicationForInPatient[index].stopDatetime!,
                                              ),
                                            ),
                                          ),
                                        ),
                                      );
                                    }),
                          ],
                        ),
                      ),

                  )));
  }
}
