import 'package:doctor_app_flutter/core/enum/viewstate.dart';
import 'package:doctor_app_flutter/core/service/DischargedPatientService.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';

import '../../locator.dart';
import 'base_view_model.dart';

class DischargedPatientViewModel extends BaseViewModel {
  DischargedPatientService _dischargedPatientService =
      locator<DischargedPatientService>();

  List<PatiantInformtion> get myDischargedPatient =>
      _dischargedPatientService.myDischargedPatients;


  List<PatiantInformtion> filterData = [];

  searchData(String str) {
    var strExist = str.length > 0 ? true : false;
    if (strExist) {
      filterData = [];
      for (var i = 0; i < myDischargedPatient.length; i++) {
        String firstName = myDischargedPatient[i].firstName.toUpperCase();
        String lastName = myDischargedPatient[i].lastName.toUpperCase();
        String mobile = myDischargedPatient[i].mobileNumber.toUpperCase();
        String patientID = myDischargedPatient[i].patientId.toString();

        if (firstName.contains(str.toUpperCase()) ||
            lastName.contains(str.toUpperCase()) ||
            mobile.contains(str) ||
            patientID.contains(str)) {
          filterData.add(myDischargedPatient[i]);
        }
      }
      notifyListeners();
    } else {
      filterData = myDischargedPatient;
      notifyListeners();
    }
  }


  Future getDischargedPatient() async {
    setState(ViewState.Busy);
    await _dischargedPatientService.getDischargedPatient();
    if (_dischargedPatientService.hasError) {
      error = _dischargedPatientService.error;
      setState(ViewState.Error);
    } else{
      filterData = myDischargedPatient;
      setState(ViewState.Idle);}
  }

  Future gtMyDischargeReferralPatient() async {
    setState(ViewState.Busy);
    await _dischargedPatientService.gtMyDischargeReferralPatient();
    if (_dischargedPatientService.hasError) {
      error = _dischargedPatientService.error;
      setState(ViewState.Error);
    } else
      setState(ViewState.Idle);
  }
}
