import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:doctor_app_flutter/core/enum/view_state.dart';
import 'package:doctor_app_flutter/core/model/referral/MyReferralPatientModel.dart';
import 'package:doctor_app_flutter/core/viewModel/patient-referral-viewmodel.dart';
import 'package:doctor_app_flutter/icons_app/doctor_app_icons.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_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/profile_medical_info_widget_search.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/buttons/app_buttons_widget.dart';
import 'package:flutter/material.dart';

// ignore: must_be_immutable
class MyReferralDetailScreen extends StatelessWidget {
  final MyReferralPatientModel? referralPatient;

  const MyReferralDetailScreen({Key? key, this.referralPatient}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // final routeArgs = ModalRoute.of(context).settings.arguments as Map;
    // pendingReferral = routeArgs['referral'];

    return BaseView<PatientReferralViewModel>(
      onModelReady: (model) => model.getDoctorProfile(),
      builder: (_, model, w) => AppScaffold(
          baseViewModel: model,
          appBarTitle: TranslationBase.of(context).referPatient,
          isShowAppBar: false,
          body: Column(
            children: [
              Container(
                padding: EdgeInsets.only(left: 0, right: 5, bottom: 5, top: 5),
                decoration: BoxDecoration(
                  color: Colors.white,
                ),
                child: Container(
                  padding: EdgeInsets.only(left: 10, right: 10, bottom: 10),
                  margin: EdgeInsets.only(top: 50),
                  child: Column(
                    children: [
                      Container(
                        padding: EdgeInsets.only(left: 12.0),
                        child: Row(children: [
                          IconButton(
                            icon: Icon(Icons.arrow_back_ios),
                            color: Colors.black, //Colors.black,
                            onPressed: () => Navigator.pop(context),
                          ),
                          Expanded(
                            child: AppText(
                              (Utils.capitalize(referralPatient!.firstName! + " " + referralPatient!.lastName!)),
                              fontSize: SizeConfig.textMultiplier! * 2.5,
                              fontWeight: FontWeight.bold,
                              fontFamily: 'Poppins',
                            ),
                          ),
                          referralPatient!.gender == 1
                              ? Icon(
                                  DoctorApp.male_2,
                                  color: Colors.blue,
                                )
                              : Icon(
                                  DoctorApp.female_1,
                                  color: Colors.pink,
                                ),
                        ]),
                      ),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Padding(
                            padding: EdgeInsets.only(left: 12.0),
                            child: Container(
                              width: 60,
                              height: 60,
                              child: Image.asset(
                                referralPatient!.gender == 1 ? 'assets/images/male_avatar.png' : 'assets/images/female_avatar.png',
                                fit: BoxFit.cover,
                              ),
                            ),
                          ),
                          SizedBox(
                            width: 10,
                          ),
                          Expanded(
                            child: Column(
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    AppText(
                                      referralPatient!.referralStatus != null ? model.getReferralStatusNameByCode(referralPatient!.referralStatus!, context) : "",
                                      fontFamily: 'Poppins',
                                      fontSize: 1.9 * SizeConfig.textMultiplier!,
                                      fontWeight: FontWeight.w700,
                                      color: referralPatient!.referralStatus == 1
                                          ? AppGlobal.inProgressColor
                                          : referralPatient!.referralStatus == 46 || referralPatient!.referralStatus == 2
                                              ? AppGlobal.appGreenColor
                                              : Colors.red[700]!,
                                    ),
                                    AppText(
                                      AppDateUtils.getDayMonthYearDateFormatted(referralPatient!.referralDate!),
                                      fontFamily: 'Poppins',
                                      fontWeight: FontWeight.w600,
                                      fontSize: 2.0 * SizeConfig.textMultiplier!,
                                      color: Color(0XFF28353E),
                                    )
                                  ],
                                ),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    Row(
                                      mainAxisAlignment: MainAxisAlignment.start,
                                      children: [
                                        AppText(
                                          TranslationBase.of(context).fileNumber,
                                          fontFamily: 'Poppins',
                                          fontWeight: FontWeight.w600,
                                          fontSize: 1.7 * SizeConfig.textMultiplier!,
                                          color: Color(0XFF575757),
                                        ),
                                        AppText(
                                          "${referralPatient!.patientID}",
                                          fontFamily: 'Poppins',
                                          fontWeight: FontWeight.w700,
                                          fontSize: 1.8 * SizeConfig.textMultiplier!,
                                          color: Color(0XFF2E303A),
                                        ),
                                      ],
                                    ),
                                    AppText(
                                      AppDateUtils.getTimeHHMMA(referralPatient!.referralDate!),
                                      fontFamily: 'Poppins',
                                      fontWeight: FontWeight.w600,
                                      fontSize: 1.8 * SizeConfig.textMultiplier!,
                                      color: Color(0XFF575757),
                                    )
                                  ],
                                ),
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  children: [
                                    Expanded(
                                      child: Column(
                                        children: [
                                          Row(
                                            mainAxisAlignment: MainAxisAlignment.start,
                                            children: [
                                              AppText(
                                                TranslationBase.of(context).referredFrom,
                                                fontFamily: 'Poppins',
                                                fontWeight: FontWeight.w600,
                                                fontSize: 1.7 * SizeConfig.textMultiplier!,
                                                color: Color(0XFF575757),
                                              ),
                                              AppText(
                                                referralPatient!.targetProjectId == referralPatient!.sourceProjectId ? TranslationBase.of(context).sameBranch : TranslationBase.of(context).otherBranch,
                                                fontFamily: 'Poppins',
                                                fontWeight: FontWeight.w700,
                                                fontSize: 1.8 * SizeConfig.textMultiplier!,
                                                color: Color(0XFF2E303A),
                                              ),
                                            ],
                                          ),
                                          Row(
                                            mainAxisAlignment: MainAxisAlignment.start,
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: [
                                              AppText(
                                                TranslationBase.of(context).remarks + " : ",
                                                fontFamily: 'Poppins',
                                                fontWeight: FontWeight.w600,
                                                fontSize: 1.7 * SizeConfig.textMultiplier!,
                                                color: Color(0XFF575757),
                                              ),
                                              Expanded(
                                                child: AppText(
                                                  referralPatient!.referringDoctorRemarks ?? '',
                                                  fontFamily: 'Poppins',
                                                  fontWeight: FontWeight.w700,
                                                  fontSize: 1.8 * SizeConfig.textMultiplier!,
                                                  color: Color(0XFF2E303A),
                                                ),
                                              ),
                                            ],
                                          ),
                                        ],
                                      ),
                                    ),
                                    Row(
                                      children: [
                                        AppText(
                                          referralPatient!.nationalityName != null ? referralPatient!.nationalityName! : "",
                                          fontWeight: FontWeight.bold,
                                          color: Color(0xFF2E303A),
                                          fontSize: 1.4 * SizeConfig.textMultiplier!,
                                        ),
                                        referralPatient!.nationalityFlagURL != null
                                            ? ClipRRect(
                                                borderRadius: BorderRadius.circular(20.0),
                                                child: Image.network(
                                                  referralPatient!.nationalityFlagURL!,
                                                  height: 25,
                                                  width: 30,
                                                  errorBuilder: (BuildContext context, Object exception, StackTrace? stackTrace) {
                                                    return Text('No Image');
                                                  },
                                                ))
                                            : SizedBox()
                                      ],
                                    )
                                  ],
                                ),
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Container(
                                      margin: EdgeInsets.only(left: 10, right: 0),
                                      child: Image.asset(
                                        'assets/images/patient/ic_ref_arrow_up.png',
                                        height: 50,
                                        width: 30,
                                      ),
                                    ),
                                    Container(
                                      margin: EdgeInsets.only(left: 0, top: 25, right: 0, bottom: 0),
                                      padding: EdgeInsets.only(left: 4.0, right: 4.0),
                                      child: Container(
                                        width: 40,
                                        height: 40,
                                        child: CircleAvatar(
                                          radius: 25.0,
                                          backgroundImage: NetworkImage(referralPatient!.doctorImageURL!),
                                          backgroundColor: Colors.transparent,
                                        ),
                                      ),
                                    ),
                                    Expanded(
                                      flex: 4,
                                      child: Container(
                                        margin: EdgeInsets.only(left: 10, top: 25, right: 10, bottom: 0),
                                        child: Column(
                                          children: [
                                            AppText(
                                              referralPatient!.doctorName!,
                                              fontFamily: 'Poppins',
                                              fontWeight: FontWeight.w700,
                                              fontSize: 1.7 * SizeConfig.textMultiplier!,
                                              color: Color(0XFF2E303A),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
              ),
              Expanded(
                child: SingleChildScrollView(
                  child: Container(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(
                          height: 16,
                        ),
                        Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 16),
                          child: SizedBox(
                            child: ProfileMedicalInfoWidgetSearch(
                              patient: model.getPatientFromReferralO(referralPatient!),
                              patientType: "7",
                              isInpatient: false,
                              from: "",
                              to: "",
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
              if (referralPatient!.referralStatus == 1)
                Container(
                  margin: EdgeInsets.symmetric(horizontal: 16, vertical: 16),
                  child: Row(
                    children: [
                      Expanded(
                        child: AppButton(
                          title: TranslationBase.of(context).accept,
                          color: Color(0xFF4BA821),
                          fontColor: Colors.white,
                          hPadding: 8,
                          vPadding: 12,
                          disabled: model.state == ViewState.Busy,
                          onPressed: () async {
                            await model.responseReferral(referralPatient!, true);
                            if (model.state == ViewState.ErrorLocal) {
                              DrAppToastMsg.showErrorToast(model.error);
                            } else {
                              DrAppToastMsg.showSuccesToast(TranslationBase.of(context).referralSuccessMsgAccept);
                              model.getMyReferralOutPatientService();
                              Navigator.pop(context);
                              Navigator.pop(context);
                            }
                          },
                        ),
                      ),
                      SizedBox(
                        width: 8,
                      ),
                      Expanded(
                        child: AppButton(
                          title: TranslationBase.of(context).reject,
                          color: AppGlobal.appRedColor,
                          fontColor: Colors.white,
                          hPadding: 8,
                          vPadding: 12,
                          disabled: model.state == ViewState.Busy,
                          onPressed: () async {
                            await model.responseReferral(referralPatient!, false);
                            if (model.state == ViewState.ErrorLocal) {
                              DrAppToastMsg.showErrorToast(model.error);
                            } else {
                              DrAppToastMsg.showSuccesToast(TranslationBase.of(context).referralSuccessMsgReject);
                              model.getMyReferralOutPatientService();
                              Navigator.pop(context);
                              Navigator.pop(context);
                            }
                          },
                        ),
                      ),
                    ],
                  ),
                ),
            ],
          )),
    );
  }
}
