import 'package:doctor_app_flutter/core/enum/PatientType.dart';
import 'package:doctor_app_flutter/core/viewModel/patient-referral-viewmodel.dart';
import 'package:doctor_app_flutter/screens/base/base_view.dart';
import 'package:doctor_app_flutter/screens/patients/profile/referral/referral_details/my-referral-detail-screen.dart';
import 'package:doctor_app_flutter/screens/patients/profile/referral/referral_details/referral_patient_detail_in-paint.dart';
import 'package:doctor_app_flutter/screens/patients/profile/referral/refer_details/referred-patient-screen.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-referral-item-widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_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:doctor_app_flutter/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';

class MyReferralInPatientScreen extends StatefulWidget {
  @override
  _MyReferralInPatientScreenState createState() => _MyReferralInPatientScreenState();
}

class _MyReferralInPatientScreenState extends State<MyReferralInPatientScreen> {
  PatientType patientType = PatientType.IN_PATIENT;

  @override
  Widget build(BuildContext context) {
    return BaseView<PatientReferralViewModel>(
      onModelReady: (model) => model.getMyReferralPatientService(),
      builder: (_, model, w) => AppScaffold(
        baseViewModel: model,
        isShowAppBar: false,
        appBarTitle: TranslationBase.of(context).referPatient,
        body: Column(
          children: [
            Container(
              margin: EdgeInsets.only(top: 70),
              child: Container(
                child: PatientTypeRadioWidget(
                  (patientType) async {
                    setState(() {
                      this.patientType = patientType;
                    });
                    GifLoaderDialogUtils.showMyDialog(context);
                    if (patientType == PatientType.IN_PATIENT) {
                      await model.getMyReferralPatientService(localBusy: true);
                    } else {
                      await model.getMyReferralOutPatientService(localBusy: true);
                    }
                    GifLoaderDialogUtils.hideDialog(context);
                  },
                ),
              ),
            ),
            model.myReferralPatients.isEmpty
                ? Center(
                    child: ErrorMessage(
                    error: TranslationBase.of(context).referralEmptyMsg,
                  ))
                : Expanded(
                    child: SingleChildScrollView(
                      child: Container(
                        child: Container(
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              ListView.builder(
                                  itemCount: model.myReferralPatients.length,
                                  scrollDirection: Axis.vertical,
                                  physics: ScrollPhysics(),
                                  shrinkWrap: true,
                                  itemBuilder: (context, index) {
                                    return InkWell(
                                      onTap: () {
                                        if (patientType == PatientType.OUT_PATIENT) {
                                          Navigator.push(
                                            context,
                                            FadePage(
                                              page: MyReferralDetailScreen(referralPatient: model.myReferralPatients[index]),
                                            ),
                                          );
                                        } else {
                                          Navigator.push(
                                            context,
                                            FadePage(
                                              page: ReferralPatientDetailScreen(model.myReferralPatients[index], model),
                                            ),
                                          );
                                        }
                                      },
                                      child: PatientReferralItemWidget(
                                        referralStatus: model.getReferralStatusNameByCode(model.myReferralPatients[index].referralStatus!, context),
                                        referralStatusCode: model.myReferralPatients[index].referralStatus!,
                                        patientName: model.myReferralPatients[index].patientName,
                                        patientGender: model.myReferralPatients[index].gender!,
                                        referredDate: AppDateUtils.getDayMonthYearDateFormatted(model.myReferralPatients[index].referralDate!),
                                        referredTime: AppDateUtils.getTimeHHMMA(model.myReferralPatients[index].referralDate!),
                                        patientID: "${model.myReferralPatients[index].patientID}",
                                        isSameBranch: false,
                                        isReferral: true,
                                        isReferralClinic: true,
                                        referralClinic: "${model.myReferralPatients[index].referringClinicDescription}",
                                        remark: model.myReferralPatients[index].referringDoctorRemarks!,
                                        nationality: model.myReferralPatients[index].nationalityName ?? model.myReferralPatients[index].nationalityID ,
                                        nationalityFlag: model.myReferralPatients[index].nationalityFlagURL!,
                                        doctorAvatar: model.myReferralPatients[index].doctorImageURL!,
                                        referralDoctorName: model.myReferralPatients[index].referringDoctorName!,
                                        clinicDescription: model.myReferralPatients[index].referringClinicDescription!,
                                        infoIcon: Icon(FontAwesomeIcons.arrowRight.data, size: 25, color: Colors.black),
                                      ),
                                    );
                                  })
                            ],
                          ),
                        ),
                      ),
                    ),
                  ),
          ],
        ),
      ),
    );
  }
}
