import 'package:doctor_app_flutter/config/config.dart';
import 'package:doctor_app_flutter/core/insurance_approval_request_model.dart';
import 'package:doctor_app_flutter/core/model/insurance_approval.dart';
import 'package:doctor_app_flutter/core/model/insurance_approval_in_patient_model.dart';
import 'package:doctor_app_flutter/core/service/base/base_service.dart';
import 'package:doctor_app_flutter/models/patient/patiant_info_model.dart';

class InsuranceCardService extends BaseService {
  InsuranceApprovalModel _insuranceApprovalModel = InsuranceApprovalModel(
      isDentalAllowedBackend: false,
      patientTypeID: 1,
      patientType: 1,
      eXuldAPPNO: 0,
      projectID: 0);
  InsuranceApprovalInPatientRequestModel
      _insuranceApprovalInPatientRequestModel =
      InsuranceApprovalInPatientRequestModel();

  List<InsuranceApprovalModel> _insuranceApproval = List();
  List<InsuranceApprovalModel> get insuranceApproval => _insuranceApproval;

  List<InsuranceApprovalInPatientModel> _insuranceApprovalInPatient = List();
  List<InsuranceApprovalInPatientModel> get insuranceApprovalInPatient =>
      _insuranceApprovalInPatient;

  Future getInsuranceApprovalInPatient({int mrn}) async {
    _insuranceApprovalInPatientRequestModel =
        InsuranceApprovalInPatientRequestModel(
      patientID: mrn,
      patientTypeID: 1,
    );
    hasError = false;
    insuranceApprovalInPatient.clear();

    await baseAppClient.post(GET_INSURANCE_IN_PATIENT,
        onSuccess: (dynamic response, int statusCode) {
      //prescriptionsList.clear();
      response['List_ApprovalMain_InPatient'].forEach((prescriptions) {
        insuranceApprovalInPatient
            .add(InsuranceApprovalInPatientModel.fromJson(prescriptions));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _insuranceApprovalInPatientRequestModel.toJson());
  }

  Future getInsuranceApproval(PatiantInformtion patient,
      {int appointmentNo, int projectId}) async {
    hasError = false;
    // _cardList.clear();
    //  if (appointmentNo != null) {
    //    _insuranceApprovalModel.appointmentNo = appointmentNo;
    //   _insuranceApprovalModel.eXuldAPPNO = null;
    //  _insuranceApprovalModel.projectID = projectId;
    // } else {
    _insuranceApprovalModel.appointmentNo = null;
    _insuranceApprovalModel.eXuldAPPNO = 0;
    _insuranceApprovalModel.projectID = 0;
    //   }

    await baseAppClient.postPatient(GET_PAtIENTS_INSURANCE_APPROVALS,
        patient: patient, onSuccess: (dynamic response, int statusCode) {
      print(response['HIS_Approval_List'].length);
      _insuranceApproval.clear();
      _insuranceApproval.length = 0;
      response['HIS_Approval_List'].forEach((item) {
        _insuranceApproval.add(InsuranceApprovalModel.fromJson(item));
      });
    }, onFailure: (String error, int statusCode) {
      hasError = true;
      super.error = error;
    }, body: _insuranceApprovalModel.toJson());
  }
}
