import 'package:liftmanager/internal/wode/page/wallet/wallet_page.dart';
import 'package:liftmanager/internal/wode/page/wallet/small_money.dart';
import 'package:liftmanager/mvp/base_page_presenter.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/widgets/state_layout.dart';
import 'package:liftmanager/utils/log_util.dart';

class WalletListPresenter extends BasePagePresenter<WalletPageState> {
  Future getWalletList(int page,
      {int currentYear,
      int currentMonth,
      Function(double, double) complete}) async {
    await NewApiService().getWalletList(page, 20,
        currentYear: currentYear, currentMonth: currentMonth, onSuccess: (res) {
      view.closeProgress();
      if (complete != null) {
        if (res != null && res.records.length > 0) {
          complete(res.records[0].income, res.records[0].outcome);
        } else {
          complete(0, 0);
        }
      }

      if (res != null) {
        view.provider.setHasMore(res.records.length == 20);
        if (page == 1) {
          view.provider.list.clear();
          if (res.records.isEmpty) {
            view.provider.setStateType(StateType.empty);
          } else {
            view.provider.addAll(res.records);
          }
        } else {
          view.provider.addAll(res.records);
        }
      } else {
        /// 加载失败
        view.provider.setHasMore(false);
        view.provider.setStateType(StateType.empty);
      }
    }, onError: (code, msg) {
      if (complete != null) {
        complete(0, 0);
      }
      view.closeProgress();
      view.provider.setHasMore(false);
      view.provider.setStateType(StateType.network);
    });
  }
}

class WalletListPresenterSeconds extends BasePagePresenter<SmallMoneyState> {
  Future getWalletList(int page,
      {int currentYear,
      int currentMonth,
      Function(double, double) complete}) async {
    await NewApiService().getWalletList(page, 20,
        currentYear: currentYear, currentMonth: currentMonth, onSuccess: (res) {
      LogUtil.d(res);
      LogUtil.d(666663);
      view.closeProgress();

      if (res != null && res.records.length > 0) {
        complete(res.records[0].income, res.records[0].outcome);
      } else {
        complete(0, 0);
      }
      if (res != null) {
        view.provider.setHasMore(res.records.length == 20);
        if (page == 1) {
          view.provider.list.clear();
          if (res.records.isEmpty) {
            view.provider.setStateType(StateType.empty);
          } else {
            view.provider.addAll(res.records);
          }
        } else {
          view.provider.addAll(res.records);
        }
      } else {
        /// 加载失败
        view.provider.setHasMore(false);
        view.provider.setStateType(StateType.empty);
      }
    }, onError: (code, msg) {
      if (complete != null) {
        complete(0, 0);
      }
      view.closeProgress();
      view.provider.setHasMore(false);
      view.provider.setStateType(StateType.network);
    });
  }
}
