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/page.dart';
import 'package:huixiang/retrofit/data/user_bill.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:huixiang/view_widget/no_data_view.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 VipBalancePage extends StatefulWidget {
  final arguments;

  VipBalancePage({this.arguments});

  @override
  State<StatefulWidget> createState() {
    return _VipBalancePage();
  }
}

class _VipBalancePage extends State<VipBalancePage> {
  RefreshController _refreshController;

  ApiService apiService;

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

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

    _refreshController = RefreshController(initialRefresh: false);
  }

  int current = 1;
  List<UserBill> userBills = [];

  queryBillInfo() async {
    BaseData<PageInfo<UserBill>> baseData = await apiService.queryBillInfo({
      "current": current,
      "model": {
        "category": "",
        "mid": widget.arguments["storeId"],
        "title": "bill_tenant_balance",
        "type": ""
      },
      "order": "descending",
      "size": 10,
      "sort": "id"
    }).catchError((error) {
      _refreshController.loadFailed();
      _refreshController.refreshFailed();
    });
    if (baseData != null && baseData.isSuccess) {
      if (current == 1) {
        userBills.clear();
      }
      userBills.addAll(baseData.data.records);
      setState(() {
        _refreshController.refreshCompleted();
        _refreshController.loadComplete();
        if (baseData.data.pageNum == baseData.data.pages) {
          _refreshController.loadNoData();
        } else {
          current += 1;
        }
      });
    } else {
      _refreshController.loadFailed();
      _refreshController.refreshFailed();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        title: S.of(context).yue,
        titleColor: Colors.black,
        background: Color(0xFFF7F7F7),
        leadingColor: Colors.black,
      ),
      body: Container(
        child: (userBills == null || userBills.length == 0)
            ? NoDataView(
          src: "assets/image/icon_empty.webp",
                isShowBtn: false,
                text: S.of(context).nihaimeiyouchongzhihuoxiaofeijilu,
                fontSize: 16.sp,
                margin: EdgeInsets.only(top: 120.h),
              )
            : ListView.builder(
                itemBuilder: (context, position) {
                  return balanceItem(userBills[position]);
                },
                itemCount: userBills.length,
              ),
      ),
    );
  }

  Widget balanceItem(UserBill userBill) {
    return Container(
      margin: EdgeInsets.only(left: 16.w, right: 16.w),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Container(
                width: 19.w,
                height: 19.h,
                margin: EdgeInsets.only(left: 12.w, top: 12.h),
                alignment: Alignment.center,
                child: Image.asset(
                  userBill.pm == 0
                      ? "assets/image/icon_store_c.webp"
                      : "assets/image/icon_wallet_recharge.webp",
                ),
              ),
              Expanded(
                child: Container(
                  width: double.infinity,
                  margin: EdgeInsets.only(left: 6.w, top: 12.h),
                  alignment: Alignment.centerLeft,
                  child: Text(
                    userBill.name,
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 16.sp,
                      color: Color(0xFF353535),
                    ),
                  ),
                ),
                flex: 1,
              ),
              Padding(
                padding: EdgeInsets.only(top: 12.h),
                child: Text("${userBill.pm == 0 ? "-" : "+"}${userBill.number}",
                    style: TextStyle(fontSize: 16, color: Color(0xffF68034))),
              ),
            ],
          ),
          SizedBox(
            height: 8.h,
          ),
          Container(
            margin: EdgeInsets.only(left: 35.w),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text.rich(
                  TextSpan(children: [
                    TextSpan(
                      text: userBill.createTime,
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: Color(0xff727272),
                      ),
                    ),
                  ]),
                ),
                Text.rich(
                  TextSpan(children: [
                    TextSpan(
                      text: S.of(context).yue_(userBill.balance),
                      style: TextStyle(
                        fontSize: 14.sp,
                        fontWeight: FontWeight.bold,
                        color: Color(0xff727272),
                      ),
                    ),
                  ]),
                ),
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(left: 35.w, top: 12.h, bottom: 12.h),
            height: 1,
            color: Color(0xFFF1F1F1),
          ),
        ],
      ),
    );
  }
}