import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/model/note/note_model.dart';
import 'package:doctor_app_flutter/core/model/patient/patiant_info_model.dart';
import 'package:doctor_app_flutter/core/service/AnalyticsService.dart';
import 'package:doctor_app_flutter/core/viewModel/authentication_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/profile/operation_report_view_model.dart';
import 'package:doctor_app_flutter/core/viewModel/project_view_model.dart';
import 'package:doctor_app_flutter/icons_app/doctor_app_icons.dart';
import 'package:doctor_app_flutter/locator.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/operation_report/update_operation_report.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/profile/app_bar/patient-profile-app-bar.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:flutter/material.dart';
import 'package:provider/provider.dart';

import '../../../../utils/dr_app_shared_pref.dart';
import '../../../../widgets/shared/app_scaffold_widget.dart';
import '../../../../widgets/shared/app_texts_widget.dart';

DrAppSharedPreferances sharedPref = new DrAppSharedPreferances();

class OperationReportScreen extends StatefulWidget {
  final int? visitType;

  const OperationReportScreen({Key? key, this.visitType}) : super(key: key);

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

class _ProgressNoteState extends State<OperationReportScreen> {
  List<NoteModel> notesList = [];
  var filteredNotesList;
  bool isDischargedPatient = false;
  AuthenticationViewModel authenticationViewModel = AuthenticationViewModel();
  late ProjectViewModel projectViewModel;

  @override
  Widget build(BuildContext context) {
    authenticationViewModel = Provider.of(context);
    projectViewModel = Provider.of(context);
    final routeArgs = ModalRoute.of(context)!.settings.arguments as Map;
    PatiantInformtion patient = routeArgs['patient'];
    if (routeArgs.containsKey('isDischargedPatient')) isDischargedPatient = routeArgs['isDischargedPatient'];
    return BaseView<OperationReportViewModel>(
      onModelReady: (model) => model.getReservations(patient.patientMRN!),
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        backgroundColor: Theme.of(context).scaffoldBackgroundColor,
        // appBarTitle: TranslationBase.of(context).progressNote,
        appBar: PatientProfileAppBar(
          patient,
          isInpatient: true,
        ),
        body: Container(
          color: Colors.grey[200],
          child: model.reservationList == null || model.reservationList.length == 0
              ? Center(
                  child: ErrorMessage(
                    error: TranslationBase.of(context).noDataAvailable,
                  ),
                )
              : Column(
                  children: [
                    Expanded(
                      child: Container(
                        child: ListView.builder(
                            itemCount: model.reservationList.length,
                            itemBuilder: (BuildContext ctxt, int index) {
                              return FractionallySizedBox(
                                widthFactor: 0.95,
                                child: CardWithBgWidget(
                                  hasBorder: false,
                                  bgColor: Colors.white,
                                  widget: Column(
                                    children: [
                                      Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        mainAxisAlignment: MainAxisAlignment.start,
                                        children: [
                                          Row(
                                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: [
                                              Container(
                                                width: MediaQuery.of(context).size.width * 0.60,
                                                child: Column(
                                                  crossAxisAlignment: CrossAxisAlignment.start,
                                                  children: [
                                                    Row(
                                                      crossAxisAlignment: CrossAxisAlignment.start,
                                                      children: [
                                                        AppText(
                                                          TranslationBase.of(context).createdBy,
                                                          fontSize: 10,
                                                        ),
                                                        Expanded(
                                                          child: AppText(
                                                            model.reservationList[index].doctorName ?? '',
                                                            fontWeight: FontWeight.w600,
                                                            fontSize: 12,
                                                          ),
                                                        ),
                                                      ],
                                                    ),
                                                  ],
                                                ),
                                              ),
                                              Column(
                                                children: [
                                                  AppText(
                                                    model.reservationList[index].createdOn != null
                                                        ? AppDateUtils.getDayMonthYearDateFormatted(AppDateUtils.getDateTimeFromServerFormat(model.reservationList[index].oTReservationDate!),
                                                            isArabic: projectViewModel.isArabic, isMonthShort: true)
                                                        : AppDateUtils.getDayMonthYearDateFormatted(DateTime.now(), isArabic: projectViewModel.isArabic),
                                                    fontWeight: FontWeight.w600,
                                                    fontSize: 14,
                                                  ),
                                                  AppText(
                                                    model.reservationList[index].createdOn != null
                                                        ? AppDateUtils.getHour(AppDateUtils.getDateTimeFromServerFormat(model.reservationList[index].oTReservationDate!))
                                                        : AppDateUtils.getHour(DateTime.now()),
                                                    fontWeight: FontWeight.w600,
                                                    fontSize: 14,
                                                  ),
                                                ],
                                                crossAxisAlignment: CrossAxisAlignment.end,
                                              )
                                            ],
                                          ),
                                          SizedBox(
                                            height: 8,
                                          ),
                                          Row(mainAxisAlignment: MainAxisAlignment.start, children: [
                                            AppText(
                                              TranslationBase.of(context).reservation,
                                              fontSize: 10,
                                            ),
                                            Expanded(
                                              child: AppText(
                                                model.reservationList[index].oTReservationID.toString(),
                                                fontSize: 10,
                                              ),
                                            )
                                          ]),
                                          SizedBox(
                                            height: 8,
                                          ),
                                          if (model.reservationList[index].operationDate != null)
                                            Row(mainAxisAlignment: MainAxisAlignment.start, children: [
                                              AppText(
                                                TranslationBase.of(context).operationDate,
                                                fontSize: 10,
                                              ),
                                              Expanded(
                                                child: AppText(
                                                  AppDateUtils.getDayMonthYearDateFormatted(AppDateUtils.getDateTimeFromServerFormat(model.reservationList[index].operationDate!),
                                                      isArabic: projectViewModel.isArabic, isMonthShort: true),
                                                  fontSize: 10,
                                                ),
                                              )
                                            ]),
                                          if (model.reservationList[index].timeStart != null)
                                            Row(mainAxisAlignment: MainAxisAlignment.start, children: [
                                              AppText(
                                                TranslationBase.of(context).operationTimeStart,
                                                fontSize: 10,
                                              ),
                                              Expanded(
                                                child: AppText(
                                                  model.reservationList[index].timeStart!,
                                                  fontSize: 10,
                                                ),
                                              )
                                            ]),
                                          if (model.reservationList[index].remarks != null)
                                            Row(mainAxisAlignment: MainAxisAlignment.start, children: [
                                              AppText(
                                                "${TranslationBase.of(context).remarks} :",
                                                fontSize: 10,
                                              ),
                                              Expanded(
                                                child: AppText(
                                                  model.reservationList[index].remarks ?? '',
                                                  fontSize: 10,
                                                ),
                                              ),
                                            ])
                                        ],
                                      ),

                                      SizedBox(
                                        height: 20,
                                      ),

                                      // if (
                                      //     authenticationViewModel
                                      //             .doctorProfile.doctorID ==
                                      //         model
                                      //             .operationReportList[
                                      //                 index]
                                      //             .createdBy)
                                      Row(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        mainAxisAlignment: MainAxisAlignment.end,
                                        children: [
                                          InkWell(
                                            onTap: () async {
                                              await locator<AnalyticsService>().logEvent(
                                                eventCategory: "Operation Report Screen",
                                                eventAction: "Update Operation Report ",
                                              );
                                              Navigator.push(
                                                context,
                                                MaterialPageRoute(
                                                    builder: (context) => UpdateOperationReport(
                                                          reservation: model.reservationList[index],
                                                          patient: patient,
                                                          isUpdate: true,
                                                        )),
                                              );
                                            },
                                            child: Container(
                                              decoration: BoxDecoration(
                                                color: AppGlobal.appGreenColor,
                                                borderRadius: BorderRadius.circular(10),
                                              ),
                                              // color:Colors.red[600],

                                              child: Row(
                                                children: [
                                                  Icon(
                                                    DoctorApp.edit_1,
                                                    size: 12,
                                                    color: Colors.white,
                                                  ),
                                                  SizedBox(
                                                    width: 2,
                                                  ),
                                                  AppText(
                                                    TranslationBase.of(context).operationReports,
                                                    fontSize: 10,
                                                    color: Colors.white,
                                                  ),
                                                ],
                                              ),
                                              padding: EdgeInsets.all(6),
                                            ),
                                          ),
                                          SizedBox(
                                            width: 10,
                                          ),
                                          SizedBox(
                                            width: 10,
                                          ),
                                        ],
                                      ),

                                      SizedBox(
                                        height: 10,
                                      ),
                                    ],
                                  ),
                                ),
                              );
                            }),
                      ),
                    ),
                  ],
                ),
        ),
      ),
    );
  }
}
