import 'package:hmg_patient_app/config/config.dart';
import 'package:hmg_patient_app/core/model/pharmacy/offer_products_model.dart';
import 'package:hmg_patient_app/core/model/pharmacy/offers_model.dart';

import 'base_service.dart';

class OffersCategoriseService extends BaseService {
  List<OffersModel> _offersList =[];
  List<OffersModel> get offersList => _offersList;
  List<OfferProductsModel> _offerProducts =[];
  List<OfferProductsModel> get offersProducts => _offerProducts;

  clearCategorise() {
    _offerProducts.clear();
  }

  // clearCategorise2() {
  //   _offersList.clear();
  // }

  Future getOffersCategorise() async {
    hasError = false;
    _offersList.clear();
    await baseAppClient.getPharmacy(
      GET_OFFERS_CATEGORISE,
      onSuccess: (dynamic response, int statusCode) {
        response['categories'].forEach((item) {
          _offersList.add(OffersModel.fromJson(item));
        });
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
    );
  }

  Future getOffersProducts({required String id}) async {
    hasError = false;
    _offerProducts.clear();
    String endPoint =
        id != null ? GET_OFFERS_PRODUCTS + "$id" : GET_OFFERS_PRODUCTS + "1";
    await baseAppClient.getPharmacy(
      endPoint,
      onSuccess: (dynamic response, int statusCode) {
        response['products'].forEach((item) {
          _offerProducts.add(OfferProductsModel.fromJson(item));
        });
      },
      onFailure: (String error, int statusCode) {
        hasError = true;
        super.error = error;
      },
    );
  }
}
