import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/coupon.dart';
import 'package:huixiang/retrofit/data/page.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/view_widget/coupon_widget.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:huixiang/view_widget/my_footer.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MineCardInvalidPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MineCardInvalidPage();
  }
}

class _MineCardInvalidPage extends State<MineCardInvalidPage> {
  RefreshController _refreshController;

  ApiService apiService;

  @override
  void dispose() {
    super.dispose();
    _refreshController.dispose();
  }

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

    SharedPreferences.getInstance().then((value) => {
          apiService =  ApiService(Dio(), context: context, token: value.getString('token')),
          queryCard(),
        });

    _refreshController = RefreshController(initialRefresh: false);
  }

  int pageNum = 1;
  List<Coupon> coupons = [];

  void _onRefresh() async {
    pageNum = 1;
    queryCard();
  }

  queryCard() async {
    BaseData<PageInfo<Coupon>> baseData = await apiService.queryCard({
      "centre": true,
      "pageNum": pageNum,
      "pageSize": 10,
      "searchKey": "",
      "state": 3
    }).catchError((error) {
      _refreshController.loadFailed();
      _refreshController.refreshFailed();
    });
    if (baseData != null && baseData.isSuccess) {
      if (pageNum == 1) {
        coupons.clear();
      }
      coupons.addAll(baseData.data.list);
      setState(() {
        _refreshController.refreshCompleted();
        _refreshController.loadComplete();
        if (baseData.data.pageNum == baseData.data.pages) {
          _refreshController.loadNoData();
        } else {
          pageNum += 1;
        }
      });
    } else {
      _refreshController.loadFailed();
      _refreshController.refreshFailed();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        title: S.of(context).shixiaoyouhuiquan,
        titleColor: Colors.black,
        titleSize: 18.sp,
        background: Color(0xFFF7F7F7),
        leadingColor: Colors.black,
      ),
      body: Container(
        child: SmartRefresher(
          enablePullDown: true,
          enablePullUp: true,
          header: ClassicHeader(),
          footer: CustomFooter(
            builder: (context, mode) {
              return MyFooter(mode);
            },
          ),
          controller: _refreshController,
          onRefresh: _onRefresh,
          onLoading: queryCard,
          child: ListView.builder(
            itemBuilder: (context, position) {
              return CouponWidget(coupons[position], null);
            },
            itemCount: coupons.length,
          ),
        ),
      ),
    );
  }
}