import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import 'NullCard.dart';

class RefreshMoreList extends StatefulWidget {
  RefreshMoreList({
    Key? key,
    required this.cardText,
    required this.pPrams,
    required this.fetch,
    required this.builder,
    required this.useState,
    this.isHome,
  }) : super(key: key);
  final String cardText;
  final Rx<Map<String, dynamic>> pPrams;
  final Function fetch;
  bool? isHome;
  Function(List<dynamic> state) useState;
  Widget Function({
    required BuildContext context,
    required Function toggle,
  }) builder;

  @override
  State<RefreshMoreList> createState() =>
      _RefreshMoreList(this.cardText, this.fetch);
}

class _RefreshMoreList extends State<RefreshMoreList> {
  EasyRefreshController _controller = EasyRefreshController();
  final String cardText;
  final Function fetch;

  _RefreshMoreList(this.cardText, this.fetch);

  @override
  void initState() {
    super.initState();
    getNewData();
  }

  var pageIndex = 1;
  var count = 10;
  int maxSum = 0;
  final params = Rx<Map<String, dynamic>>({'pageNum': 1});
  final dataList = Rx<List<dynamic>>([]);
  final newDataList = Rx<List<dynamic>>([]);

  void getNewData() {
    Map<String, dynamic> map = {...params.value, ...widget.pPrams.value};
    pageIndex = 1;
    map['pageNum'] = pageIndex;
    params(map);
    fetch(params.value).then((value) {
      maxSum = value['total'];
      widget.useState(value['records']);
      dataList(value['records']);
      newDataList(value['records']);
    });
  }

  void getMoreData() {
    Map<String, dynamic> map = {...params.value, ...widget.pPrams.value};
    pageIndex++;
    map['pageNum'] = pageIndex;
    params(map);
    fetch(params.value).then((value) {
      maxSum = value['total'];
      newDataList.value.addAll(value['records']);
      widget.useState(newDataList.value);
      dataList(newDataList.value);
    });
  }

  int _count = 0;
  toggle() {
    getNewData();
  }

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => EasyRefresh(
        controller: _controller,
        header: ClassicalHeader(
          infoText: DateTime.now().toString().substring(0, 19),
          refreshedText: '刷新完成',
          refreshText: '刷新中....',
          refreshingText: '刷新中....',
          refreshReadyText: '刷新完毕',
          noMoreText: '',
          textColor: const Color(0xff666666),
          infoColor: const Color(0xff666666),
        ),
        footer: ClassicalFooter(
          infoText: DateTime.now().toString().substring(0, 19),
          loadText: '上拉加载更多',
          loadingText: '加载中....',
          loadedText: '',
          loadReadyText: '释放加载',
          noMoreText: '暂无更多',
          enableInfiniteLoad: false,
          textColor: const Color(0xff666666),
          infoColor: const Color(0xff666666),
        ),
        // firstRefresh: true,//是否首次刷新
        onRefresh: widget.isHome != null
            ? null
            : () async {
                await Future.delayed(const Duration(seconds: 1), () {
                  // print("下拉刷新-----");
                  getNewData();
                  _count = dataList.value.length;
                  // print("最新条数" + _count.toString());
                  _controller.resetLoadState();
                });
              },
        onLoad: () async {
          await Future.delayed(const Duration(seconds: 1), () {
            // print("上拉加载-----");
            getMoreData();
            _count = dataList.value.length;
            // print("加载更多条数" + _count.toString());
            _controller.finishLoad(noMore: _count >= maxSum);
          });
        },
        child: Column(
          children: [
            widget.builder(context: context, toggle: toggle),
            dataList.value.isEmpty
                ? NullCard(text: cardText)
                : const SizedBox(
                    height: 0.1,
                  )
          ],
        ),
      ),
    );
  }
}
