/*
 * @Author: XianKaiQun
 * @Date: 2020-07-08 15:08:28
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2022-12-15 10:50:12
 * @Description: 
 */
import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:receipt_package/src/impl/receipt_service_impl.dart';
import 'package:receipt_package/src/models/arrears_fee.dart';
import 'package:receipt_package/src/models/search_item.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class IndexStore with ChangeNotifier {
  final BuildContext context;
  late Map args;
//_Store(this.context) {
//args = ModalRoute.of(context).settings.arguments??{};
//}
  IndexStore(this.context) {
    args =
        ModalRoute.of(context)!.settings.arguments as Map<dynamic, dynamic>? ??
            {};
  }
  final communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();

  CommunityModel? get community => communityGlobalStore.data;

  TextEditingController? inputController;

  ScrollController viewController = new ScrollController();

  int index = 0;

  void setIndex(index) {
    index = index;
    notifyListeners();
  }

  Future onInit() async {
    if (await communityGlobalStore.intent == false) {
      return Navigator.pop(context);
    }
    await Future.wait([
      getTotal(),
      getList(),
      getHouseList(isLoad: false),
    ]);
    notifyListeners();
  }

  Future onLoad() async {
    if (isShowBuild) return;
    getHouseList(isLoad: true);
  }

  void route2Detail(SearchItemModel item) async {
    late Map params = {
      'assetId': item.assetId,
      'assetType': item.assetType,
      'assetFullName': item.assetName,
    };
    Navigator.pushNamed(
      context,
      'receipt/detail',
      arguments: params,
    );
  }

  ///房屋搜索---------------------------
  final houseScrollViewKey = GlobalKey<WisScrollViewState>();
  Map<String, dynamic> houseParams = {
    'pageNum': 1,
    'pageSize': 20,
  };
  List<SearchItemModel> houseList = [];

  String? houseSearchWord = '';
  String? buildId;
  Future onHouseSearchTrigger(String v) async {
    houseSearchWord = v;
    houseList = [];
    buildId = null;
    houseParams['pageNum'] = 1;
    setIsShowBuild(v);
    FocusScope.of(context).requestFocus(FocusNode());
    notifyListeners();
    houseScrollViewKey.currentState?.initTrigger();
  }

  void setHouseName(v) {
    houseSearchWord = v;
  }

  Future getHouseList({bool isLoad = true}) async {
    // if (!isLoad) houseParams['pageNum'] = 1;
    var res = await ReceiptServiceImpl().getHouseSearchList({
      ...houseParams,
      'fuzzyRoomNo': houseSearchWord,
      'communityId': community!.id,
      'buildId': buildId,
    });
    final _data = res.data;
    final _list = _data?.list ?? [];
    if ((_data?.total ?? 0) == 0) {
      houseScrollViewKey.currentState?.isEmpty();
    } else if ((_data?.pages ?? 0) <= (_data?.pageNum ?? 0)) {
      houseScrollViewKey.currentState?.isLast();
    } else {
      houseParams['pageNum']++;
    }
    isLoad ? houseList.addAll(_list) : houseList = _list;
    notifyListeners();
  }

  ///车辆---------------------------
  final carScrollViewKey = GlobalKey<WisScrollViewState>();
  Map<String, dynamic> carParams = {
    'pageNum': 1,
    'pageSize': 20,
  };
  List<SearchItemModel> carList = [];

  String carSearchWord = '';
  Future oncarSearchTrigger(String v) async {
    carSearchWord = v;
    carList = [];
    FocusScope.of(context).requestFocus(FocusNode());
    notifyListeners();
    carScrollViewKey.currentState?.initTrigger();
  }

  Future getCarList({bool isLoad = true}) async {
    if (!isLoad) carParams['pageNum'] = 1;
    var res = await ReceiptServiceImpl().getCarSearchList({
      'communityId': community!.id,
      'fuzzyCarNo': carSearchWord,
      ...carParams,
    });
    final _data = res.data;
    final _list = _data?.list ?? [];
    if ((_data?.total ?? 0) == 0) {
      carScrollViewKey.currentState?.isEmpty();
    } else if ((_data?.pages ?? 0) <= (_data?.pageNum ?? 0)) {
      carScrollViewKey.currentState?.isLast();
    } else {
      carParams['pageNum']++;
    }
    isLoad ? carList.addAll(_list) : carList = _list;
    notifyListeners();
  }

  ///车位搜索
  final parkScrollViewKey = GlobalKey<WisScrollViewState>();
  Map<String, dynamic> parkParams = {
    'pageNum': 1,
    'pageSize': 20,
  };
  List<SearchItemModel> parkList = [];

  String parkSearchWord = '';

  Future onparkSearchTrigger(String v) async {
    parkSearchWord = v;
    parkList = [];
    notifyListeners();
    FocusScope.of(context).requestFocus(FocusNode());
    parkScrollViewKey.currentState?.initTrigger();
  }

  Future getParkList({bool isLoad = true}) async {
    if (!isLoad) parkParams['pageNum'] = 1;
    var res = await ReceiptServiceImpl().getParkSearchList({
      'communityId': community!.id,
      'fuzzyCode': parkSearchWord,
      ...parkParams,
    });
    final _data = res.data;
    final _list = _data?.list ?? [];
    if ((_data?.total ?? 0) == 0) {
      parkScrollViewKey.currentState?.isEmpty();
    } else if ((_data?.pages ?? 0) <= (_data?.pageNum ?? 0)) {
      parkScrollViewKey.currentState?.isLast();
    } else {
      parkParams['pageNum']++;
    }
    isLoad ? parkList.addAll(_list) : parkList = _list;
    notifyListeners();
  }

  ///欠费总额
  num? total;

  ///卡片数据
  List<ArrearsFeeModel> list = [];

  /// 获取欠费总额
  Future getTotal() async {
    var res = await ReceiptServiceImpl.getArrearsFeeTotal(
      {'communityId': community!.id},
    );
    total = res.data ?? 0;
    notifyListeners();
  }

  List billList = [];

  ///获取卡片数据
  Future getList() async {
    var res = await ReceiptServiceImpl.getArrearsFeeList(
      {'communityId': community!.id},
    );
    billList = res.data ?? [];
  }

  void route2list(ArrearsFeeModel item) {
    Navigator.pushNamed(context, 'bill/list', arguments: {
      'communityId': community!.id,
      ...item.toJson(),
    });
  }

  bool isShowBuild = true;
  void setIsShowBuild(String v) {
    isShowBuild = v.isNotEmpty ? false : true;
    if (isShowBuild) {
      viewController.animateTo(0,
          duration: Duration(milliseconds: 200), curve: Curves.ease);
    }
    notifyListeners();
  }
}
