import 'package:doctor_app_flutter/core/viewModel/prescription_view_model.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/util/date-utils.dart';
import 'package:doctor_app_flutter/util/translations_delegate_base.dart';
import 'package:doctor_app_flutter/widgets/patients/profile/patient-page-header-widget.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/network_base_view.dart';
import 'package:flutter/material.dart';

class NewPrescriptionHistoryScreen extends StatefulWidget {
  @override
  _NewPrescriptionHistoryScreenState createState() =>
      _NewPrescriptionHistoryScreenState();
}

class _NewPrescriptionHistoryScreenState
    extends State<NewPrescriptionHistoryScreen> {
  PersistentBottomSheetController _controller;
  final _scaffoldKey = GlobalKey<ScaffoldState>();
  TextEditingController strengthController = TextEditingController();
  int testNum = 0;
  int strengthChar;
  PatiantInformtion patient;

  @override
  void initState() {
    super.initState();
  }

  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    final routeArgs = ModalRoute.of(context).settings.arguments as Map;
    patient = routeArgs['patient'];
    return BaseView<PrescriptionViewModel>(
      onModelReady: (model) => model.getPrescription(mrn: patient.patientId),
      builder:
          (BuildContext context, PrescriptionViewModel model, Widget child) =>
              AppScaffold(
                  isShowAppBar: true,
                  appBarTitle: TranslationBase.of(context).prescription,
                  body: NetworkBaseView(
                    baseViewModel: model,
                    child: SingleChildScrollView(
                      child: Container(
                        color: Colors.white,
                        child: Column(
                          children: [
                            PatientPageHeaderWidget(patient),
                            Divider(
                              height: 1.0,
                              thickness: 1.0,
                              color: Colors.grey,
                            ),
                            (model.prescriptionList.length != 0)
                                ? SizedBox(
                                    height:
                                        model.prescriptionList[0].rowcount == 0
                                            ? 200.0
                                            : 10.0)
                                : SizedBox(height: 200.0),
                            //model.prescriptionList == null
                            (model.prescriptionList.length != 0)
                                ? model.prescriptionList[0].rowcount == 0
                                    ? Container(
                                        child: AppText(
                                          'Sorry , Theres no prescriptions for this patient',
                                          color: Color(0xFFB9382C),
                                        ),
                                      )
                                    : Padding(
                                        padding: EdgeInsets.all(14.0),
                                        child: NetworkBaseView(
                                          baseViewModel: model,
                                          child: Column(
                                            mainAxisAlignment:
                                                MainAxisAlignment.start,
                                            children: [
                                              ...List.generate(
                                                model.prescriptionList[0]
                                                    .rowcount,
                                                (index) => Container(
                                                  color: Colors.white,
                                                  child: Column(
                                                    children: [
                                                      SizedBox(
                                                        height: MediaQuery.of(
                                                                    context)
                                                                .size
                                                                .height *
                                                            0.022,
                                                      ),
                                                      Row(
                                                        mainAxisAlignment:
                                                            MainAxisAlignment
                                                                .spaceBetween,
                                                        // crossAxisAlignment:
                                                        //     CrossAxisAlignment.start,
                                                        children: [
                                                          Container(
                                                            height: MediaQuery.of(
                                                                        context)
                                                                    .size
                                                                    .height *
                                                                0.21,
                                                            width: MediaQuery.of(
                                                                        context)
                                                                    .size
                                                                    .width *
                                                                0.1,
                                                            child: Column(
                                                              children: [
                                                                AppText(
                                                                  (DateTime.parse(model.prescriptionList[0].entityList[index].createdOn) !=
                                                                              null
                                                                          ? (DateTime.parse(model.prescriptionList[0].entityList[index].createdOn).year)
                                                                              .toString()
                                                                          : DateTime.now()
                                                                              .year)
                                                                      .toString(),
                                                                  color: Colors
                                                                      .green,
                                                                  fontSize:
                                                                      13.5,
                                                                ),
                                                                AppText(
                                                                  DateUtils.getMonth(model.prescriptionList[0].entityList[index].createdOn !=
                                                                              null
                                                                          ? (DateTime.parse(model.prescriptionList[0].entityList[index].createdOn)
                                                                              .month)
                                                                          : DateTime.now()
                                                                              .month)
                                                                      .toUpperCase(),
                                                                  color: Colors
                                                                      .green,
                                                                ),
                                                                AppText(
                                                                  DateTime.parse(model
                                                                          .prescriptionList[
                                                                              0]
                                                                          .entityList[
                                                                              index]
                                                                          .createdOn)
                                                                      .day
                                                                      .toString(),
                                                                  color: Colors
                                                                      .green,
                                                                ),
                                                                AppText(
                                                                  DateUtils.getTimeFormated(DateTime.parse(model
                                                                          .prescriptionList[
                                                                              0]
                                                                          .entityList[
                                                                              index]
                                                                          .createdOn))
                                                                      .toString(),
                                                                  color: Colors
                                                                      .green,
                                                                ),
                                                              ],
                                                            ),
                                                          ),
                                                          Container(
                                                            // height: MediaQuery.of(
                                                            //             context)
                                                            //         .size
                                                            //         .height *
                                                            //     0.3499,
                                                            width: MediaQuery.of(
                                                                        context)
                                                                    .size
                                                                    .width *
                                                                0.77,
                                                            child: Column(
                                                              children: [
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      'Start Date:',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          14.0,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          AppText(
                                                                            DateUtils.getDateFormatted(DateTime.parse(model
                                                                            .prescriptionList[0]
                                                                            .entityList[index]
                                                                            .startDate)),
                                                                        fontSize:
                                                                            13.5,
                                                                      ),
                                                                    ),
                                                                    SizedBox(
                                                                      width:
                                                                          6.0,
                                                                    ),
                                                                    AppText(
                                                                      'Order Type:',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          14.0,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          AppText(
                                                                        model
                                                                            .prescriptionList[0]
                                                                            .entityList[index]
                                                                            .orderTypeDescription,
                                                                        fontSize:
                                                                            13.0,
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                SizedBox(
                                                                  height: 5.5,
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    Container(
                                                                      child:
                                                                          Expanded(
                                                                        child:
                                                                            AppText(
                                                                          model
                                                                              .prescriptionList[0]
                                                                              .entityList[index]
                                                                              .medicationName,
                                                                          fontWeight:
                                                                              FontWeight.w700,
                                                                          fontSize:
                                                                              15.0,
                                                                        ),
                                                                      ),
                                                                    )
                                                                  ],
                                                                ),
                                                                SizedBox(
                                                                  height: 5.5,
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    Expanded(
                                                                      child:
                                                                          AppText(
                                                                        model
                                                                            .prescriptionList[0]
                                                                            .entityList[index]
                                                                            .doseDetail,
                                                                        fontSize:
                                                                            15.0,
                                                                      ),
                                                                    )
                                                                  ],
                                                                ),
                                                                SizedBox(
                                                                  height: 10.0,
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      'Indication: ',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          17.0,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          RichText(
                                                                        maxLines:
                                                                            3,
                                                                        overflow:
                                                                            TextOverflow.ellipsis,
                                                                        strutStyle:
                                                                            StrutStyle(fontSize: 12.0),
                                                                        text: TextSpan(
                                                                            style:
                                                                                TextStyle(color: Colors.black),
                                                                            text: model.prescriptionList[0].entityList[index].indication),
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      'UOM: ',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          17.0,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          RichText(
                                                                        maxLines:
                                                                            3,
                                                                        overflow:
                                                                            TextOverflow.ellipsis,
                                                                        strutStyle:
                                                                            StrutStyle(fontSize: 12.0),
                                                                        text: TextSpan(
                                                                            style:
                                                                                TextStyle(color: Colors.black),
                                                                            text: model.prescriptionList[0].entityList[index].uom),
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      'BOX Quantity: ',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          17.0,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          RichText(
                                                                        maxLines:
                                                                            3,
                                                                        overflow:
                                                                            TextOverflow.ellipsis,
                                                                        strutStyle:
                                                                            StrutStyle(fontSize: 12.0),
                                                                        text: TextSpan(
                                                                            style:
                                                                                TextStyle(color: Colors.black),
                                                                            text: model.prescriptionList[0].entityList[index].quantity.toString() == null ? "" : model.prescriptionList[0].entityList[index].quantity.toString()),
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      'pharmacy Intervention ',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          17.0,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          RichText(
                                                                        maxLines:
                                                                            3,
                                                                        overflow:
                                                                            TextOverflow.ellipsis,
                                                                        strutStyle:
                                                                            StrutStyle(fontSize: 12.0),
                                                                        text: TextSpan(
                                                                            style:
                                                                                TextStyle(color: Colors.black),
                                                                            text: model.prescriptionList[0].entityList[index].pharmacyInervention == null ? "" : model.prescriptionList[0].entityList[index].pharmacyInervention.toString()),
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                SizedBox(
                                                                    height:
                                                                        5.0),
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      'pharmacist Remarks : ',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          15.0,
                                                                    ),
                                                                    Expanded(
                                                                      child: AppText(
                                                                          // commening below code because there is an error coming in the model please fix it before pushing it
                                                                          model.prescriptionList[0].entityList[index].pharmacistRemarks == null ? "" : model.prescriptionList[0].entityList[index].pharmacistRemarks,
                                                                          fontSize: 15.0),
                                                                    )
                                                                  ],
                                                                ),
                                                                SizedBox(
                                                                  height: 20.0,
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      TranslationBase.of(context)
                                                                              .doctorName +
                                                                          ": ",
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w600,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          AppText(
                                                                        model
                                                                            .prescriptionList[0]
                                                                            .entityList[index]
                                                                            .doctorName,
                                                                        fontWeight:
                                                                            FontWeight.w700,
                                                                      ),
                                                                    )
                                                                  ],
                                                                ),
                                                                SizedBox(
                                                                  height: 8.0,
                                                                ),
                                                                Row(
                                                                  children: [
                                                                    AppText(
                                                                      'Doctor Remarks : ',
                                                                      fontWeight:
                                                                          FontWeight
                                                                              .w700,
                                                                      fontSize:
                                                                          13.0,
                                                                    ),
                                                                    Expanded(
                                                                      child:
                                                                          Container(
                                                                        // height: MediaQuery.of(context).size.height *
                                                                        //     0.038,
                                                                        child:
                                                                            RichText(
                                                                          // maxLines:
                                                                          //     2,
                                                                          // overflow:
                                                                          //     TextOverflow.ellipsis,
                                                                          strutStyle:
                                                                              StrutStyle(fontSize: 10.0),
                                                                          text:
                                                                              TextSpan(
                                                                            style:
                                                                                TextStyle(color: Colors.black),
                                                                            text: model.prescriptionList[0].entityList[index].remarks != null
                                                                                ? model.prescriptionList[0].entityList[index].remarks
                                                                                : "",
                                                                          ),
                                                                        ),
                                                                      ),
                                                                    ),
                                                                  ],
                                                                ),
                                                                SizedBox(
                                                                  height: 10.0,
                                                                ),

                                                                // SizedBox(
                                                                //   height: 40,
                                                                // ),
                                                              ],
                                                            ),
                                                          ),
                                                        ],
                                                      ),
                                                      Divider(
                                                        height: 0,
                                                        thickness: 1.0,
                                                        color: Colors.grey,
                                                      ),
                                                    ],
                                                  ),
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                      )
                                : Container(
                                    child: AppText(
                                      'Sorry , theres no prescriptions listed for this patient',
                                      color: Color(0xFFB9382C),
                                    ),
                                  )
                          ],
                        ),
                      ),
                    ),
                  )),
    );
  }

  selectDate(BuildContext context, PrescriptionViewModel model) async {
    DateTime selectedDate;
    selectedDate = DateTime.now();
    final DateTime picked = await showDatePicker(
      context: context,
      initialDate: selectedDate,
      firstDate: DateTime.now().add(Duration(hours: 2)),
      lastDate: DateTime(2040),
      initialEntryMode: DatePickerEntryMode.calendar,
    );
    if (picked != null && picked != selectedDate) {
      setState(() {
        selectedDate = picked;
      });
    }
  }
}
