import 'dart:async';
import 'dart:convert';

import 'package:zhiying_comm/util/base_bloc.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

import 'model/wallet_detail_data_model.dart';

class WalletDetailBloc extends BlocBase {
  StreamController<WalletDetailDataModel> _dataController =
      StreamController<WalletDetailDataModel>();

  Stream<WalletDetailDataModel> get outData => _dataController.stream;

  ///选中的某一时间的数据
  WalletDetailDataModel selectDateData;

  String selectDay = "today";

  List<WalletDetailDataModel> listDataModel = new List();

  String currentType;

  @override
  void dispose() {
    _dataController.close();
    _dataController = null;
  }

  ///
  /// 加载头部数据
  ///
  loadData(String type) async {
    await NetUtil.request("/api/v1/user/wallet/" + type, onSuccess: (data) {
      currentType = type;
      listDataModel.clear();
      listDataModel.addAll(List.from(data).map((v) {
        return WalletDetailDataModel.fromJson(v);
      }).toList());
      refresh();
    });
  }

  ///刷新页面
  refresh() {
    for (var item in listDataModel) {
      if (selectDay == item.type) {
        selectDateData = item;
        _dataController.add(selectDateData);
        return;
      }
    }
  }
}
