import 'package:flutter/cupertino.dart'; import 'package:rasadyar_auth/data/utils/safe_call.dart'; import 'package:rasadyar_chicken/data/models/response/roles_products/roles_products.dart'; import 'package:rasadyar_chicken/data/models/response/segmentation_model/segmentation_model.dart'; import 'package:rasadyar_chicken/presentation/pages/root/logic.dart'; import 'package:rasadyar_chicken/presentation/utils/utils.dart'; import 'package:rasadyar_core/core.dart'; class SegmentationLogic extends GetxController { RootLogic rootLogic = Get.find(); RxBool isLoadingMoreAllocationsMade = false.obs; RxInt currentPage = 1.obs; late List routesName; RxInt selectedSegmentIndex = 0.obs; RxBool isExpanded = false.obs; RxList isExpandedList = [].obs; Rx fromDateFilter = Jalali.now().obs; Rx toDateFilter = Jalali.now().obs; RxnString searchedValue = RxnString(); GlobalKey formKey = GlobalKey(); TextEditingController weightController = TextEditingController(); RxBool isSaleSubmitButtonEnabled = false.obs; RxList rolesProductsModel = RxList(); Rxn selectedProduct = Rxn(); Rxn selectedSegment = Rxn(); Rx>> segmentationList = Resource>.loading().obs; @override void onInit() { super.onInit(); routesName = ['قطعه‌بندی'].toList(); getAllSegmentation(); } @override void onReady() { super.onReady(); setUpListener(); } @override void onClose() { // TODO: implement onClose super.onClose(); } void setSearchValue(String? value) { searchedValue.value = value?.trim(); } void setUpListener() { debounce( searchedValue, (callback) => getAllSegmentation(), time: Duration(milliseconds: timeDebounce), ); } void setEditData(SegmentationModel item) { selectedSegment.value = item; weightController.text = item.weight.toString(); } void clearForm() { weightController.clear(); isSaleSubmitButtonEnabled.value = false; selectedProduct.value = null; selectedSegment.value = null; formKey.currentState?.reset(); } Future getAllSegmentation([bool isLoadingMore = false]) async { if (isLoadingMore) { isLoadingMoreAllocationsMade.value = true; } else { segmentationList.value = Resource>.loading(); } if (searchedValue.value != null && searchedValue.value!.trim().isNotEmpty && currentPage.value > 1) { currentPage.value = 1; // Reset to first page if search value is set } await safeCall( call: () async => await rootLogic.chickenRepository.getSegmentation( token: rootLogic.tokenService.accessToken.value!, queryParameters: buildQueryParams( pageSize: 20, page: currentPage.value, search: 'filter', role: 'Steward', value: searchedValue.value, fromDate: fromDateFilter.value.toDateTime(), toDate: toDateFilter.value.toDateTime(), ), ), onSuccess: (result) { if ((result?.count ?? 0) == 0) { segmentationList.value = Resource>.empty(); } else { segmentationList.value = Resource>.success( PaginationModel( count: result?.count ?? 0, next: result?.next, previous: result?.previous, results: [ ...(segmentationList.value.data?.results ?? []), ...(result?.results ?? []), ], ), ); isLoadingMoreAllocationsMade.value = false; } }, ); } Future deleteSegmentation(String key) async { await safeCall( call: () => rootLogic.chickenRepository.deleteSegmentation( token: rootLogic.tokenService.accessToken.value!, key: key, ), ); } Future getRolesProducts() async { safeCall( call: () async => await rootLogic.chickenRepository.getRolesProducts( token: rootLogic.tokenService.accessToken.value!, ), onSuccess: (result) { if (result != null) { rolesProductsModel.value = result; selectedProduct.value = rolesProductsModel.first; } }, onError: (error, stacktrace) {}, ); } Future editSegment() async { var res = true; safeCall( call: () async => await rootLogic.chickenRepository.editSegmentation( token: rootLogic.tokenService.accessToken.value!, model: SegmentationModel( key: selectedSegment.value?.key, weight: int.tryParse(weightController.text) ?? 0, ), ), onSuccess: (result) { res = true; }, onError: (error, stacktrace) { res = false; }, ); return res; } Future createSegment() async { var res = true; safeCall( call: () async => await rootLogic.chickenRepository.createSegmentation( token: rootLogic.tokenService.accessToken.value!, model: SegmentationModel( key: selectedProduct.value?.key, weight: int.tryParse(weightController.text) ?? 0, ), ), onSuccess: (result) { res = true; }, onError: (error, stacktrace) { res = false; }, ); return res; } }