import 'dart:convert';

import 'package:http/http.dart';
import 'package:test_sa/controllers/api_routes/api_manager.dart';
import 'package:test_sa/controllers/api_routes/urls.dart';
import 'package:test_sa/modules/loan_module/models/medical_department_model.dart';
import 'package:test_sa/providers/loading_list_notifier.dart';

class FlowMedicalDepartmentProvider extends LoadingListNotifier<MedicalDepartmentModel> {
  @override
  Future getData({int? id}) async {
    if (loading == true) return -2;
    loading = true;
    notifyListeners();
    try {
      Response response = await ApiManager.instance.get(URLs.getFlowMedicalDepartments);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        List categoriesListJson = json.decode(response.body)["data"];
        items = categoriesListJson.map((item) => MedicalDepartmentModel.fromJson(item)).toList();
      }
      loading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      loading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }
}
