import 'package:electronic_assistant/base/base_controller.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../data/bean/talks.dart';
import '../../../data/repositories/talk_repository.dart';
import '../../../utils/error_handler.dart';

class FileSearchController extends BaseController {
  final refreshController = RefreshController(initialRefresh: false);

  int page = 1;

  int pageSize = 10;

  @override
  void onReady() {
    super.onReady();
    onRefreshData();
  }

  RxList<TalkBean> get talkList => talkRepository.talkList;

  void requestTalkData({int page = 1}) {
    talkRepository.requestTalkPagePaginate(page, pageSize).then((response) {
      debugPrint("requestTalkData-response-$response");
      if (talkRepository.talkList.length >= response.count) {
        debugPrint("requestTalkData-没有更多数据了");
        refreshController.loadNoData();
      } else {
        refreshController.loadComplete();
      }
      refreshController.refreshCompleted();
    }).catchError((error) {
      debugPrint("requestTalkData-catchError-$error");
      refreshController.loadFailed();
      refreshController.refreshFailed();
      ErrorHandler.toastError(error);
    });
  }

  void onRefreshData() {
    page = 1;
    requestTalkData(page: page);
  }

  void onLoadMoreTalkData() {
    requestTalkData(page: ++page);
  }
}
