import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../../../../../Data/Repository/Customers/getCustomerbytenantidrepository.dart';
import '../../../../../Helper/toast.dart';
import '../../../../../Model/Response/Customers/GetCustomerById/Getdropcustomerbytenantid.dart';




class TenantDropCustomerController extends GetxController{

  final int limit = 10;
  int page = 1;
  var hasMore = true.obs;

  bool progress = true;

  RxBool shimmer = true.obs;

  var getCustomerBytenantId = <GetDropCustomerByTenantIdDetails>[].obs;

  TextEditingController searchController = TextEditingController();


  GetCustomersByTenantIdRepository getCustomersByTenantIdRepository = GetCustomersByTenantIdRepository();

  getCustomerByTenantId()async{
    GetDropCustomerByTenantId? result = await getCustomersByTenantIdRepository.getDropCustomerByTenantId();
    getCustomerByTenantIdResult(result!);
  }

  getDropTenantByTenantId()async{
    GetDropCustomerByTenantId? result = await getCustomersByTenantIdRepository.getDropTenantByTenantId();
    getCustomerByTenantIdResult(result!);
  }


  getCustomerByTenantIdResult(GetDropCustomerByTenantId data){
    getCustomerBytenantId.value = data.details??[];
    progress =false;
    update();
  }

  Future getSearchCustomerData(keyword) async {
    GetDropCustomerByTenantId? result = await getCustomersByTenantIdRepository.getDropCustomerKeywordByTenantId(keyword);

    if (result?.code == 200) {
      getCustomerBytenantId.clear();
      getCustomerBytenantId.addAll(result?.details??[]);
      shimmer.value = false;
      update();
      print('getSearchCustomerDatalength ${getCustomerBytenantId.length}');
      if(getCustomerBytenantId.isEmpty){
        Toast.showToast('No customer at this moment');
      }

    } else {
      // Toast.showToast(result?.message??"");
    }
  }

  Future refreshData() async {
    searchController.clear();
    page = 1;
    hasMore.value = true;
    getCustomerBytenantId.value = [];


    if(searchController.text.isEmpty){
      print("ifffffffffffffffffffffffffffffffffff");
      await getCustomerData();
    }
  }

  Future getCustomerData() async {
    GetDropCustomerByTenantId? result = await getCustomersByTenantIdRepository.getDropCustomerByPageLimitTenantId(page,limit);

    if (result?.code == 200) {
      if (result!.details!.length < limit) {
        hasMore.value = false;
      }
      // getCustomerBytenantId.clear();
      getCustomerBytenantId.addAll(result.details??[]);
      shimmer.value = false;
      page++;
      update();
    } else {
      // Toast.showToast(result?.message??"");
    }
  }


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