import 'dart:convert';
import 'dart:developer';

import 'package:flutter/widgets.dart';
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/extensions/context_extension.dart';
import 'package:test_sa/models/all_requests_and_count_model.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/models/ppm/recurrent_wo.dart';
import 'package:test_sa/models/timer_model.dart';

import '../../../models/search_all_requests_model.dart';

class AllRequestsProvider extends ChangeNotifier {
  bool isAllLoading = false;
  bool isOpenLoading = false;
  bool isInProgressLoading = false;
  bool isCompleteLoading = false;
  bool isOverdueLoading = false;
  bool isHighPriorityLoading = false;
  bool isCalendarLoading = false;
  bool isLoading = false;
  bool isMultiFilterLoading = false;

  bool _isFilterRequestLoading = false;
  bool _isRequestCategoryLoading = false;

  int _status = 0;

  int get status => _status;

  set status(int value) {
    _status = value;
    notifyListeners();
  }

  int _currentListIndex = 0;

  int get currentListIndex => _currentListIndex;

  set currentListIndex(int value) {
    _currentListIndex = value;
    notifyListeners();
  }

  int? stateCode;

  AllRequestsAndCount? _allRequestsAndCount;
  AllRequestsAndCount? _openRequests;
  AllRequestsAndCount? _inProgressRequests;
  AllRequestsAndCount? _completedRequests;
  AllRequestsAndCount? _filterRequest;
  AllRequestsAndCount? _requestDetailList;

  AllRequestsAndCount? get allRequestsAndCount => _allRequestsAndCount;

  AllRequestsAndCount? get highPriorityRequests => _highPriorityRequests;

  AllRequestsAndCount? get overdueRequests => _overdueRequests;

  // AllRequestsAndCount? get openRequests => _openRequests;
  // AllRequestsAndCount? get completedRequests => _completedRequests;
  AllRequestsAndCount? get requestDetailList => _requestDetailList;

  RecurrentWoData? _recurrentWoData = RecurrentWoData();

  RecurrentWoData? get recurrentWoData => _recurrentWoData;

  set recurrentWoData(RecurrentWoData? value) {
    _recurrentWoData = value;
    notifyListeners();
  }

  set requestDetailList(AllRequestsAndCount? value) {
    _requestDetailList = value;
    notifyListeners();
  }

  AllRequestsAndCount? _selectedRequestCategory;

  bool get isRequestCategoryLoading => _isRequestCategoryLoading;

  set isRequestCategoryLoading(bool value) {
    _isRequestCategoryLoading = value;
    notifyListeners();
  } // overdue,high priority, new , completed

  AllRequestsAndCount? _overdueRequests;
  AllRequestsAndCount? _highPriorityRequests;

  bool isLoadingList = false;

  updateIsLoadingList(value) {
    isLoadingList = value;
    notifyListeners();
  }

  AllRequestsAndCount? calendarRequests;

  final pageItemNumber = 10;
  int pageNum = 1;
  bool nextPage = true;

  void reset() {
    _allRequestsAndCount?.requestsDetails?.clear();
    _allRequestsAndCount = null;
    pageNum = 1;
    nextPage = true;
    stateCode = null;
  }

  void resetRequestsApiData() {
    _highPriorityRequests = null;
    _overdueRequests = null;
    openRequests = null;
    inProgressRequests = null;
    completedRequests = null;
    pageNum = 1;
    nextPage = true;
    stateCode = null;
  }

  Future<void> getRequests() async {
    resetRequestsApiData();
    getOpenRequests(pagination: true);
    getInProgressRequests(pagination: true);
    getCompletedRequests(pagination: true);
    getHighPriorityRequests(pagination: true);
    getOverdueRequests(pagination: true);
  }

  SearchAllRequestsModel? searchedModel;

  List<int> getStatues(BuildContext context) {
    List<int> list = [1, 2, 3, 4];

    if (context.userProvider.isAssessor) {
      list = [9, 15];
      return list;
    }
    if (context.userProvider.isQualityUser) {
      list = [10, 11];
      if (context.settingProvider.isUserFlowMedical) list.insert(0, 7);
      return list;
    }

    if (!context.userProvider.isNurse) {
      list.add(5);
    }
    if (context.userProvider.isEngineer) {
      list.add(10);
    }
    list.add(6); // task module

    if (context.settingProvider.isUserFlowMedical && !context.userProvider.isNurse) {
      list.add(7); // task mod
    }

    //TODO uncommit this to enable TRAF in production.
    context.userProvider.modulesPermissionsList.forEach((module) {
      if (module.module!.value! == 8 || module.module!.value! == 9) {
        list.add(module.module!.value!);
      }
    });
    //Todo commit this when passing to production.
    // context.userProvider.modulesPermissionsList.forEach((module) {
    //   if (module.module!.value! == 8) {
    //     list.add(module.module!.value!);
    //   }
    // });
    //
    // if (context.settingProvider.isUserFlowMedical && !context.userProvider.isAssessor && (context.userProvider.isNurse || context.userProvider.isEngineer)) {
    //   list.add(9); //
    // }
    list.add(12);
    if (context.userProvider.isCommissioningEngineer && context.settingProvider.isUserFlowMedical) {
      list = [13];
    }
    // loan module

    if (context.settingProvider.isUserFlowMedical && !context.userProvider.isNurse) {
      // list = [16];
      list.add(16);
    }
    if ( context.userProvider.isHouseKeeper) {
      list = [5];
    }


    return list;
  }

  Future<int> getAllRequests(BuildContext context, {int? typeTransaction, SearchAllRequestsModel? search, bool? isAscending}) async {
    if (isAllLoading == true) return -2;
    isAllLoading = true;
    if (_allRequestsAndCount == null) notifyListeners();
    Response response;
    try {
      if (search != null) {
        searchedModel = search;
      } else {
        searchedModel = null;
      }
      final type = typeTransaction == null
          ? search?.typeTransaction == null || (search?.typeTransaction?.isEmpty ?? false)
              ? getStatues(context)
              : search!.typeTransaction
          : [typeTransaction];
      List<int> status = (search?.statuses == null || (search?.statuses?.isEmpty ?? false)) ? (((search?.isArchived ?? false) ? [3] : [1, 2, 4])) : search!.statuses!;
      Map<String, dynamic> body = {
        "typeTransaction": type,
        "statusTransaction": status,
        "priority": [],
        "displayData": [],
        "pageNumber": pageNum++,
        "pageSize": pageItemNumber,
        if (search?.requestNumber?.value?.isNotEmpty ?? false) "requestNumber": search!.requestNumber!.value,
        if (search?.assetName?.value?.isNotEmpty ?? false) "assetName": search!.assetName!.value,
        if (search?.assetNo?.value?.isNotEmpty ?? false) "assetNumber": search!.assetNo!.value,
        if (search?.sn?.value?.isNotEmpty ?? false) "assetSerialNo": search!.sn!.value,
        if (search?.model?.value?.isNotEmpty ?? false) "model": search!.model!.value,
        if (search?.manufacture?.value?.isNotEmpty ?? false) "manufacturer": search!.manufacture!.value,
        if (search?.startDate != null) "from": search!.startDate!.toIso8601String(),
        if (search?.endDate != null) "to": search!.endDate!.toIso8601String(),
        if (isAscending != null) "isAscending": isAscending,
      };
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        if (_allRequestsAndCount == null) {
          _allRequestsAndCount = (await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]));
        } else {
          _allRequestsAndCount!.requestsDetails!.addAll((await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0])).requestsDetails!);
        }
        if (_allRequestsAndCount!.requestsDetails!.length >= pageItemNumber) {
          nextPage = true;
        } else {
          nextPage = false;
        }
        notifyListeners();
      } else {
        _allRequestsAndCount = null;
      }
      isAllLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isAllLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getRecurrentWoById({required int id}) async {
    isLoading = true;
    Response response;
    try {
      response = await ApiManager.instance.get(URLs.getRecurrentPlanByIdUrl + "?planRecurrentTaskId=$id");
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        recurrentWoData = RecurrentWoData.fromJson(json.decode(response.body)["data"]);
        recurrentWoData?.planRecurrentMedicalTaskRooms?.forEach((element) {
          element.planRecurrentMedicalTaskRoomTabs?.forEach((tabs) {
            tabs.planRecurrentMedicalTaskRoomTabAttributes?.forEach((attribute) {
              if (attribute.attribute?.type == "bool" && attribute.attributeValue == null) {
                attribute.attributeValue = "true";
              }
            });
          });
        });
        notifyListeners();
      }
      isLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<Map<String, dynamic>> updateRecurrentWo({required int status, bool showToast = true}) async {
    Response response;
    try {
      response = await ApiManager.instance.post(URLs.updateRecurrentPlanUrl, body: recurrentWoData!.toJson(status: status), showToast: showToast);
      stateCode = response.statusCode;

      if (stateCode == 200) {
        return {'success': true, 'message': ''};
      } else {
        // Parse error message from response
        try {
          final responseBody = jsonDecode(response.body);
          String errorMessage = responseBody['message'] ?? responseBody['title'] ?? 'Failed to update the task';
          return {'success': false, 'message': errorMessage};
        } catch (e) {
          return {'success': false, 'message': 'Failed to update the task. Status code: $stateCode'};
        }
      }
    } catch (error) {
      stateCode = -1;
      return {'success': false, 'message': error.toString()};
    }
  }

  void updateRecurrentWoTimer({TimerModel? timer}) {
    recurrentWoData?.recurrentWoTimerModel = timer;
    if (timer?.startAt != null && timer?.endAt != null) {
      recurrentWoData?.timerModelList = recurrentWoData?.timerModelList ?? [];
      recurrentWoData?.timerModelList!.add(timer!);
    }
    notifyListeners();
  }

  Future<int> getCalendarRequests({required DateTime from, DateTime? to}) async {
    if (isCalendarLoading == true) return -2;
    isCalendarLoading = true;
    notifyListeners();
    Response response;
    try {
      Map<String, dynamic> body = {
        "typeTransaction": [1, 2, 3, 4],
        "statusTransaction": [1, 2, 3, 4],
        "priority": [0, 1],
        "displayData": [],
        "from": from.toIso8601String(),
        "to": to?.toIso8601String() ?? from.toIso8601String(),
      };
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        calendarRequests = (await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]));
        notifyListeners();
      }
      isCalendarLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isCalendarLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getHighPriorityRequests({bool pagination = false}) async {
    if (isHighPriorityLoading == true) return -2;

    if (_highPriorityRequests == null || pagination) {
      isHighPriorityLoading = true;
      // notifyListeners();
    }
    Response response;
    try {
      Map<String, dynamic> body = {
        "typeTransaction": [1],
        "statusTransaction": [1, 2, 4],
        "priority": [1],
        "displayData": []
      };
      if (pagination) {
        body["pageNumber"] = 0;
        body["pageSize"] = 0;
      } else {
        body["pageNumber"] = pageNum++;
        body["pageSize"] = pageItemNumber;
      }
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        if (_highPriorityRequests == null) {
          _highPriorityRequests = (await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]));
        } else {
          _highPriorityRequests!.requestsDetails!.addAll((await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0])).requestsDetails!);
        }
        if (_highPriorityRequests!.requestsDetails!.length >= pageItemNumber) {
          nextPage = true;
        } else {
          nextPage = false;
        }

        notifyListeners();
      } else {
        _allRequestsAndCount = null;
      }
      isHighPriorityLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isHighPriorityLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getOverdueRequests({bool pagination = false}) async {
    if (isOverdueLoading == true) return -2;
    if (_overdueRequests == null || pagination) {
      isOverdueLoading = true;
      notifyListeners();
    }
    Response response;
    try {
      Map<String, dynamic> body = {
        "typeTransaction": [1, 2, 3, 4],
        "statusTransaction": [1, 2, 4],
        "priority": [],
        "displayData": [1]
      };
      if (pagination) {
        body["pageNumber"] = 0;
        body["pageSize"] = 0;
      } else {
        body["pageNumber"] = pageNum++;
        body["pageSize"] = pageItemNumber;
      }
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        if (_overdueRequests == null) {
          _overdueRequests = (await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]));
        } else {
          _overdueRequests!.requestsDetails!.addAll((await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0])).requestsDetails!);
        }
        if (_overdueRequests!.requestsDetails!.length >= pageItemNumber) {
          nextPage = true;
        } else {
          nextPage = false;
        }
        notifyListeners();
      } else {
        _allRequestsAndCount = null;
      }
      isOverdueLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isOverdueLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getOpenRequests({bool pagination = false, bool reset = false}) async {
    if (isOpenLoading == true) return -2;
    if (reset) {
      openRequests = null;
      pageNum = 1;
    }
    if (openRequests == null || pagination) {
      isOpenLoading = true;
      notifyListeners();
    }
    Response response;
    try {
      bool isEngineer = ApiManager.instance.user!.type == UsersTypes.engineer;
      Map<String, dynamic> body = {
        "typeTransaction": [1, 2, 3, 4],
        "statusTransaction": [isEngineer ? 2 : 1],
        "priority": [],
        "displayData": []
      };
      if (pagination) {
        body["pageNumber"] = 0;
        body["pageSize"] = 0;
      } else {
        body["pageNumber"] = pageNum++;
        body["pageSize"] = pageItemNumber;
      }
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        if (openRequests == null) {
          openRequests = (await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]));
        } else {
          openRequests!.requestsDetails!.addAll((await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0])).requestsDetails!);
        }
        if (openRequests!.requestsDetails!.length >= pageItemNumber) {
          nextPage = true;
        } else {
          nextPage = false;
        }
        notifyListeners();
      } else {
        _allRequestsAndCount = null;
      }
      isOpenLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isOpenLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getInProgressRequests({bool pagination = false, bool reset = false}) async {
    if (isInProgressLoading == true) return -2;
    if (reset) {
      inProgressRequests = null;
      pageNum = 1;
    }
    if (inProgressRequests == null || pagination) {
      isInProgressLoading = true;
      notifyListeners();
    }
    Response response;
    try {
      Map<String, dynamic> body = {
        "typeTransaction": [1, 2, 3, 4],
        "statusTransaction": [2],
        "priority": [],
        "displayData": []
      };

      if (pagination) {
        body["pageNumber"] = 0;
        body["pageSize"] = 0;
      } else {
        body["pageNumber"] = pageNum++;
        body["pageSize"] = pageItemNumber;
      }
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        if (inProgressRequests == null) {
          inProgressRequests = (await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]));
          notifyListeners();
        } else {
          inProgressRequests!.requestsDetails!.addAll((await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0])).requestsDetails!);
          notifyListeners();
        }
        if (inProgressRequests!.requestsDetails!.length >= pageItemNumber) {
          nextPage = true;
        } else {
          nextPage = false;
        }
        notifyListeners();
      } else {
        _allRequestsAndCount = null;
      }
      isInProgressLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isInProgressLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getFilterRequests({bool showLoader = true, required int status}) async {
    // if (filterRequest == null || pagination) {
    //   isFilterRequestLoading = true;
    //   notifyListeners();
    // }
    isFilterRequestLoading = showLoader;
    Response response;
    try {
      Map<String, dynamic> body = {
        "typeTransaction": [1, 2, 3, 4],
        "statusTransaction": [status],
        "priority": [],
        "displayData": [],
        "pageNumber": pageNum,
        "pageSize": pageItemNumber,
      };
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        if (filterRequest == null) {
          filterRequest = (await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]));
          isFilterRequestLoading = false;
          notifyListeners();
        } else {
          filterRequest!.requestsDetails!.addAll((await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0])).requestsDetails!);
          isFilterRequestLoading = false;
          notifyListeners();
        }
        if (filterRequest!.requestsDetails!.length >= pageItemNumber) {
          nextPage = true;
        } else {
          nextPage = false;
        }
        notifyListeners();
      } else {
        filterRequest = null;
      }
      isFilterRequestLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isFilterRequestLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getSelectedRequestCategory({required RequestQueryModel requestQueryModel}) async {
    isRequestCategoryLoading = requestQueryModel.showLoader;
    Response response;
    try {
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: requestQueryModel.toJson());
      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        if (selectedRequestCategory == null) {
          selectedRequestCategory = (await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]));
          isRequestCategoryLoading = false;
          notifyListeners();
        } else {
          selectedRequestCategory!.requestsDetails!.addAll((await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0])).requestsDetails!);
          isRequestCategoryLoading = false;
          notifyListeners();
        }
      } else {
        filterRequest = null;
      }
      isRequestCategoryLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isRequestCategoryLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  Future<int> getCompletedRequests({bool pagination = false, bool reset = false}) async {
    if (isCompleteLoading == true) return -2;
    if (reset) {
      completedRequests = null;
      pageNum = 1;
    }
    if (completedRequests == null || pagination) {
      isCompleteLoading = true;
      notifyListeners();
    }
    Response response;
    try {
      Map<String, dynamic> body = {
        "typeTransaction": [1, 2, 3, 4],
        "statusTransaction": [4],
        "priority": [],
        "displayData": []
      };
      if (pagination) {
        body["pageNumber"] = 0;
        body["pageSize"] = 0;
      } else {
        body["pageNumber"] = pageNum++;
        body["pageSize"] = pageItemNumber;
      }
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        if (completedRequests == null) {
          completedRequests = (await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]));
        } else {
          completedRequests!.requestsDetails!.addAll((await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0])).requestsDetails!);
        }
        if (completedRequests!.requestsDetails!.length >= pageItemNumber) {
          nextPage = true;
        } else {
          nextPage = false;
        }
        notifyListeners();
      } else {
        _allRequestsAndCount = null;
      }
      isCompleteLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isCompleteLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

  AllRequestsAndCount? get inProgressRequests => _inProgressRequests;

  set inProgressRequests(AllRequestsAndCount? value) {
    _inProgressRequests = value;
    notifyListeners();
  }

  AllRequestsAndCount? get completedRequests => _completedRequests;

  set completedRequests(AllRequestsAndCount? value) {
    _completedRequests = value;
    notifyListeners();
  }

  AllRequestsAndCount? get openRequests => _openRequests;

  set openRequests(AllRequestsAndCount? value) {
    _openRequests = value;
    notifyListeners();
  }

  AllRequestsAndCount? get filterRequest => _filterRequest;

  set filterRequest(AllRequestsAndCount? value) {
    _filterRequest = value;
    notifyListeners();
  }

  bool get isFilterRequestLoading => _isFilterRequestLoading;

  set isFilterRequestLoading(bool value) {
    _isFilterRequestLoading = value;
    notifyListeners();
  }

  AllRequestsAndCount? get selectedRequestCategory => _selectedRequestCategory;

  set selectedRequestCategory(AllRequestsAndCount? value) {
    _selectedRequestCategory = value;
    notifyListeners();
  }

  Future<int> getDashboardMultifilterRequests(
    BuildContext context, {
    bool pagination = false,
    List<int>? statusTransaction,
    List<int>? priority,
    List<int>? displayData,
    bool? isAscending,
  }) async {
    if (isMultiFilterLoading == true) return -2;
    if (_allRequestsAndCount == null || pagination) {
      isMultiFilterLoading = true;
      notifyListeners();
    }
    Response response;
    try {
      Map<String, dynamic> body = {"typeTransaction": getStatues(context), "statusTransaction": statusTransaction ?? [], "priority": priority ?? [], "displayData": displayData ?? []};
      if (pagination) {
        body["pageNumber"] = 0;
        body["pageSize"] = 0;
      } else {
        body["pageNumber"] = pageNum++;
        body["pageSize"] = pageItemNumber;
      }
      if (isAscending != null) body["isAscending"] = isAscending;
      response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);

      stateCode = response.statusCode;
      if (response.statusCode >= 200 && response.statusCode < 300) {
        if (_allRequestsAndCount == null) {
          _allRequestsAndCount = (await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0]));
        } else {
          _allRequestsAndCount!.requestsDetails!.addAll((await AllRequestsAndCount.fromJson(json.decode(response.body)["data"][0])).requestsDetails!);
        }
        if (_allRequestsAndCount!.requestsDetails!.length >= pageItemNumber) {
          nextPage = true;
        } else {
          nextPage = false;
        }
        notifyListeners();
      } else {
        _allRequestsAndCount = null;
      }
      isMultiFilterLoading = false;
      notifyListeners();
      return response.statusCode;
    } catch (error) {
      isMultiFilterLoading = false;
      stateCode = -1;
      notifyListeners();
      return -1;
    }
  }

// Future<int> getCloseRequests() async {
//   if (isCloseLoading == true) return -2;
//   isCloseLoading = true;
//   if (closeRequests == null) notifyListeners();
//   Response response;
//   try {
//     Map<String, dynamic> body = {
//       "typeTransaction": [1, 2, 3, 4],
//       "statusTransaction": [4],
//       "priority": [],
//       "displayData": []
//     };
//     response = await ApiManager.instance.post(URLs.getAllRequestsAndCount, body: body);
//
//     stateCode = response.statusCode;
//     if (response.statusCode >= 200 && response.statusCode < 300) {
//       closeRequests = AllRequestsAndCount.fromJson(json.decode(response.body)["data"]);
//       notifyListeners();
//     }
//     isCloseLoading = false;
//     notifyListeners();
//     return response.statusCode;
//   } catch (error) {
//     isCloseLoading = false;
//     stateCode = -1;
//     notifyListeners();
//     return -1;
//   }
// }
}
