/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2022-04-07 16:04:50
 * @LastEditTime : 2023-06-07 14:50:14
 * @Description  : Do not edit
 */
import 'package:app_package/app_package.dart';
import 'package:flutter/material.dart';
import 'package:intelligent_package/src/models/intelligent_detail_list_model.dart';
import 'package:intelligent_package/src/models/intelligent_total_model.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
import '../service/impl/intelligent_impl.dart';
import '../service/intelligent_service.dart';

class PrivateDetailStore with ChangeNotifier {
  final BuildContext context, providerContext;
  late Map<dynamic, dynamic> routingArgs;
  PrivateDetailStore(this.context, this.providerContext) {
    routingArgs =
        ModalRoute.of(context)!.settings.arguments as Map<dynamic, dynamic>? ??
            {};
  }

  final appGlobalStore = GetIt.I.get<AppGlobalStore>();
  final scorllViewListKeyIndex = GlobalKey<WisScrollViewState>();
  final IntelligentService _intelligentService = IntelligentServiceImpl();

  ScrollController outerController = new ScrollController();

  final scrollKey = GlobalKey();

  num get arrearsFee => routingArgs['item']['arrearsFee'] ?? null;
  String? get chargeMode => appGlobalStore.chargeMode;
  num get currentAmount => routingArgs['item']['currentAmount'] ?? null;
  Map<String, dynamic> params = {
    'pageNum': 1,
    'pageSize': 20,
  };
  Map actionType = {
    'TOP_UP_REFUND': '充值退费',
    'TOP_UP_INVALID': '充值作废',
    'AUTOMATIC_ELECTRIC': '自动通电',
    'TOP_UP': '充值',
    'METER_READING_UPDATES': '抄表更新',
    'ADJUST_THE_ALLOWANCE': '调整余量',
    'BINDING': '绑定',
    'AUTOMATIC_POWER_OFF': '自动断电',
    'REMOTE_ELECTRIC': '远程通电',
    'REMOTE_POWER_OUTAGES': '远程断电',
  };
  IntelligentTotalModel? totalDetail;

  Future getDetail({isChange = false}) async {
    // ignore: await_only_futures
    await ([getTotalDetail(), getDetailList(isLoad: false)]);
    routingArgs['item']['powerOn'] =
        !isChange && routingArgs['item']['powerOn'] == 0 ? 0 : 1;
    notifyListeners();
  }

  ///智能表详情

  Future getTotalDetail() async {
    var res = await _intelligentService.getTotalDetail({
      'meterId': routingArgs['item']['meterId'],
    });
    totalDetail = res.data;
    notifyListeners();
  }

  ///智能表详细列表
  List<IntelligentDetailListModel> detailList = [];

  Future getDetailList({bool isLoad = true}) async {
    if (!isLoad) params['pageNum'] = 1;
    if (routingArgs['item']['id'] == null) return;
    var res = await _intelligentService.getDetailPage({
      ...params,
      'meterId': routingArgs['item']['meterId'],
    });
    final _data = res.data;
    final _list = _data?.list ?? [];
    if ((_data?.total ?? 0) == 0) {
      scorllViewListKeyIndex.currentState?.isEmpty();
    } else if ((_data?.pages ?? 0) <= (_data?.pageNum ?? 0)) {
      scorllViewListKeyIndex.currentState?.isLast();
    } else {
      params['pageNum']++;
    }
    isLoad ? detailList.addAll(_list) : detailList = _list;
    notifyListeners();
  }
}
