import 'package:doctor_app_flutter/client/base_app_client.dart';
import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/models/patient/my_referral/my_referral_patient_model.dart';
import 'package:doctor_app_flutter/models/doctor/request_add_referred_doctor_remarks.dart';
import 'package:doctor_app_flutter/models/patient/request_my_referral_patient_model.dart';
import 'package:doctor_app_flutter/util/helpers.dart';
import 'package:flutter/cupertino.dart';


class MyReferralPatientProvider with ChangeNotifier {
  List<MyReferralPatientModel> listMyReferralPatientModel = [];

  bool isLoading = true;
  bool isError = false;
  String error = '';
  Helpers helpers = Helpers();


  RequestMyReferralPatientModel _requestMyReferralPatient = RequestMyReferralPatientModel();
  RequestAddReferredDoctorRemarks _requestAddReferredDoctorRemarks = RequestAddReferredDoctorRemarks();

  MyReferralPatientProvider() {
    getMyReferralPatient();
  }

  getMyReferralPatient() async {
    try {
      await BaseAppClient.post(GET_MY_REFERRAL_PATIENT,
        body: _requestMyReferralPatient.toJson(),
        onSuccess: (dynamic response, int statusCode) {
          response['List_MyReferralPatient'].forEach((v) {
            listMyReferralPatientModel.add(MyReferralPatientModel.fromJson(v));
          });
          isError = false;
          isLoading = false;
        },
        onFailure: (String error, int statusCode) {
          isError = true;
          isLoading = false;
          this.error = error;
        },
      );
      notifyListeners();
    } catch (error) {
      isLoading = false;
      isError = true;
      this.error = 'Something wrong happened, please contact the admin';
      notifyListeners();
    }
  }

  Future replay(
      String referredDoctorRemarks, MyReferralPatientModel model) async {
    try {
      _requestAddReferredDoctorRemarks.admissionNo = model.admissionNo;
      _requestAddReferredDoctorRemarks.patientID = model.patientID;
      _requestAddReferredDoctorRemarks.referredDoctorRemarks = referredDoctorRemarks;
      _requestAddReferredDoctorRemarks.lineItemNo = model.lineItemNo;
      _requestAddReferredDoctorRemarks.referringDoctor = model.referringDoctor;
      await BaseAppClient.post(ADD_REFERRED_DOCTOR_REMARKS,
        body: _requestAddReferredDoctorRemarks.toJson(),
        onSuccess: (dynamic body, int statusCode) {
          model.referredDoctorRemarks = referredDoctorRemarks;
          listMyReferralPatientModel[
              listMyReferralPatientModel.indexOf(model)] = model;
          notifyListeners();
        },
        onFailure: (String error, int statusCode) {
          throw (error);
        },
      );
    } catch (error) {
      throw error;
    }
  }
}
