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_referred_patient_model.dart';
import 'package:doctor_app_flutter/models/patient/request_my_referral_patient_model.dart';
import 'package:doctor_app_flutter/models/doctor/verify_referral_doctor_remarks.dart';

import 'package:flutter/cupertino.dart';
import '../util/helpers.dart';


class MyReferredPatientProvider with ChangeNotifier {
  List<MyReferredPatientModel> listMyReferredPatientModel = [];

  bool isLoading = true;
  bool isError = false;
  String error = '';
  Helpers helpers = Helpers();
  RequestMyReferralPatientModel _requestMyReferralPatient = RequestMyReferralPatientModel();
  VerifyReferralDoctorRemarks _verifyreferraldoctorremarks = VerifyReferralDoctorRemarks();
  MyReferredPatientProvider() {
    getMyReferralPatient();
  }

  getMyReferralPatient() async {
    try {
      await BaseAppClient.post(GET_MY_REFERRED_PATIENT,
        body: _requestMyReferralPatient.toJson(),
        onSuccess: (dynamic response, int statusCode) {
          response['List_MyReferredPatient'].forEach((v) {
            listMyReferredPatientModel.add(MyReferredPatientModel.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 verify(
      MyReferredPatientModel model) async {
    try {
      _verifyreferraldoctorremarks.patientID=model.projectId;
      _verifyreferraldoctorremarks.admissionNo =model.admissionNo;
      _verifyreferraldoctorremarks.lineItemNo = model.lineItemNo;
      _verifyreferraldoctorremarks.referredDoctorRemarks=model.referredDoctorRemarks;
      _verifyreferraldoctorremarks.referringDoctor=model.referringDoctor;
      _verifyreferraldoctorremarks.firstName=model.firstName;
      _verifyreferraldoctorremarks.middleName=model.middleName;
      _verifyreferraldoctorremarks.lastName=model.lastName;
      _verifyreferraldoctorremarks.patientMobileNumber=model.mobileNumber;
      _verifyreferraldoctorremarks.patientIdentificationID=model.patientIdentificationNo;

      await BaseAppClient.post(GET_MY_REFERRED_PATIENT,
        body: _verifyreferraldoctorremarks.toJson(),
        onSuccess: (dynamic body, int statusCode) {

          listMyReferredPatientModel[
          listMyReferredPatientModel.indexOf(model)] = model;
          notifyListeners();
        },
        onFailure: (String error, int statusCode) {
          throw(error);
        },
      );
    } catch (error) {
      throw(error);
    }
  }
}
