import 'package:rasadyar_chicken/presentation/pages/root/logic.dart';
import 'package:rasadyar_chicken/presentation/pages/sale/logic.dart';
import 'package:rasadyar_chicken/presentation/pages/sales_out_of_province_buyers/logic.dart';
import 'package:rasadyar_chicken/presentation/pages/sales_out_of_province_sales_list/logic.dart';
import 'package:rasadyar_core/core.dart';

class SalesOutOfProvinceLogic extends GetxController {
  RootLogic get rootLogic => Get.find<RootLogic>();

  SaleLogic get saleLogic => Get.find<SaleLogic>();

  SalesOutOfProvinceBuyersLogic get buyersLogic =>
      Get.find<SalesOutOfProvinceBuyersLogic>();

  SalesOutOfProvinceSalesListLogic get saleListLogic =>
      Get.find<SalesOutOfProvinceSalesListLogic>();

  RxInt selectedSegmentIndex = 0.obs;
  RxBool isExpanded = false.obs;

  RxList<int> isExpandedList = <int>[].obs;
  Rx<Jalali> fromDateFilter = Jalali.now().obs;
  Rx<Jalali> toDateFilter = Jalali.now().obs;
  RxnString searchedValue = RxnString();
  RxList<String> routesName = RxList();

  @override
  void onInit() {
    super.onInit();
    routesName.value = [...saleLogic.routesName, 'خارج استان'].toList();

    routesName.add(selectedSegmentIndex.value == 0 ? 'فروش' : 'خریداران');
    ever(selectedSegmentIndex, (callback) {
      routesName.removeLast();
      routesName.add(callback == 0 ? 'فروش' : 'خریداران');
    });
  }

  @override
  void onReady() {
    super.onReady();


  }

  void setSearchValue(String? value) {
    searchedValue.value = value?.trim();
    if (selectedSegmentIndex.value == 0) {
      saleListLogic.searchedValue.value = value?.trim();
    } else {
      buyersLogic.searchedValue.value = value?.trim();
    }
  }

  void submitFilter() {
    if (selectedSegmentIndex.value == 0) {
      saleListLogic.fromDateFilter.value = fromDateFilter.value;
      saleListLogic.toDateFilter.value = toDateFilter.value;
      saleListLogic.getOutProvinceSales();
    } else {
      buyersLogic.fromDateFilter.value = fromDateFilter.value;
      buyersLogic.toDateFilter.value = toDateFilter.value;
      buyersLogic.getOutProvinceCarcassesBuyer();
    }
  }
}
