import 'package:rasadyar_auth/data/utils/safe_call.dart';
import 'package:rasadyar_chicken/data/models/response/imported_loads_model/imported_loads_model.dart';
import 'package:rasadyar_chicken/presentation/pages/root/logic.dart';
import 'package:rasadyar_core/core.dart';

class BuyInProvinceAllLogic extends GetxController {
  RxList<int> isExpandedList = <int>[].obs;
  Rx<Jalali> fromDateFilter = Jalali.now().obs;
  Rx<Jalali> toDateFilter = Jalali.now().obs;
  RxnString searchedValue = RxnString();

  RootLogic rootLogic = Get.find<RootLogic>();

  Rx<Resource<List<ImportedLoadsModel>>> importedLoads =
      Resource<List<ImportedLoadsModel>>.loading().obs;




  @override
  void onReady() {
    debounce(searchedValue, (callback) => getImportedEntries(), time: Duration(milliseconds: 2000));
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  Future<void> getImportedEntries() async {
    safeCall(
      call: () async => await rootLogic.chickenRepository.getImportedLoadsModel(
        token: rootLogic.tokenService.accessToken.value!,
        queryParameters: buildQueryParams(
          queryParams: {'type': 'entered'},
          role: 'Steward',
          search: 'filter',
          page: 1,
          pageSize: 10,
          value: searchedValue.value
        ),
      ),
      onSuccess: (res) async {
        await Future.delayed(Duration(milliseconds: 200));
        if ((res?.count ?? 0) == 0) {
          importedLoads.value = Resource<List<ImportedLoadsModel>>.empty();
        } else {
          importedLoads.value = Resource<List<ImportedLoadsModel>>.success(res!.results!);
        }
      },
    );
  }
}
