import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../retrofit/data/user_info.dart';
import '../view_widget/classic_header.dart';
import '../view_widget/my_footer.dart';

class TradingCardPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _TradingCardPage();
  }
}

class _TradingCardPage extends State<TradingCardPage> {
  ApiService apiService;
  final RefreshController refreshController = RefreshController();
  UserInfo userInfo;

  @override
  void initState() {
    super.initState();
    SharedPreferences.getInstance().then((value) {
      apiService =
          ApiService(Dio(), context: context, token: value.getString("token"));
      queryUserBalance();
    });
  }

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

  ///查询会员信息
  queryUserBalance() async {
    BaseData<UserInfo> baseData =
    await apiService.queryInfo().catchError((onError) {
      refreshController.refreshFailed();
      refreshController.loadFailed();});
    if (baseData != null && baseData.isSuccess) {
      userInfo = baseData.data;
      if (mounted) setState(() {});
      refreshController.refreshCompleted();
      refreshController.loadComplete();
    }else {
      SmartDialog.showToast(baseData.msg, alignment: Alignment.center);
      refreshController.refreshFailed();
      refreshController.loadFailed();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Stack(
        children: [
          Container(
            decoration: BoxDecoration(
              image: DecorationImage(
                fit: BoxFit.fill,
                image: AssetImage("assets/image/settlement_bg.webp"),
              ),
            ),
            width: double.infinity,
            height: 306.h,
          ),
          Scaffold(
            backgroundColor: Colors.transparent,
            appBar: MyAppBar(
              title: "",
              titleColor: Colors.white,
              background: Colors.transparent,
              leadingColor: Colors.white,
              brightness: Brightness.dark,
            ),
            body: SmartRefresher(
              controller: refreshController,
              enablePullDown: true,
              enablePullUp: false,
              header: MyHeader(
                color: Colors.white,
              ),
              footer: CustomFooter(
                builder: (context, mode) {
                  return MyFooter(mode);
                },
              ),
              onRefresh: () {
                queryUserBalance();
                },
              physics: BouncingScrollPhysics(),
              scrollController: ScrollController(),
              child: SingleChildScrollView(
                physics: BouncingScrollPhysics(),
                child:Container(
                  margin: EdgeInsets.symmetric(horizontal:14.w),
                  child:Column(
                    children: [
                      Container(
                        width: double.infinity,
                        padding:EdgeInsets.only(top:24.h,bottom: 27.h) ,
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(12),
                          boxShadow: [
                          BoxShadow(
                            color: Colors.black.withAlpha(12),
                            offset: Offset(0, 4),
                            blurRadius: 8,
                            spreadRadius: 8,
                          )
                        ],
                        ),
                        child: Column(
                            children:[
                              Image.asset(
                                "assets/image/my_trading.webp",
                                width:200.w,
                                fit: BoxFit.fill,
                                height:22.h,
                              ),
                              SizedBox(height:28.h),
                              Image.asset(
                                "assets/image/trading_logo.webp",
                                width:196.w,
                                fit: BoxFit.fill,
                                height:150.h,
                              ),
                              Padding(padding:EdgeInsets.only(top: 21.h,bottom: 17.h),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text(
                                    "我已收集",
                                    style: TextStyle(
                                        color: Colors.black,
                                        fontSize: 14.sp,
                                        fontWeight: MyFontWeight.regular),
                                  ),
                                  SizedBox(width: 12.w,),
                                  Text(
                                    (userInfo?.happyBean ?? 0).toString(),
                                    style: TextStyle(
                                        color: Color(0xFF32A060),
                                        fontSize: 18.sp,
                                        fontWeight: MyFontWeight.regular),
                                  )
                                ],
                              ),),
                              Padding(padding:EdgeInsets.only(bottom:31.h),
                                child:  Row(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Padding(padding:EdgeInsets.only(right:1.w),
                                        child: GestureDetector(
                                          onTap: (){
                                            Navigator.of(context).pushNamed('/router/welfare_exchange');
                                          },
                                          child: Text(
                                            "兑换区逛逛",
                                            style: TextStyle(
                                                color: Color(0xFF32A060),
                                                fontSize: 15.sp,
                                                fontWeight: MyFontWeight.regular),
                                          ),
                                        )),
                                    Image.asset(
                                      "assets/image/icon_right_z.webp",
                                      width:16,
                                      fit: BoxFit.fill,
                                      height:16,
                                      color: Color(0xFF32A060),
                                    )
                                  ],
                                ),),
                              Image.asset(
                                "assets/image/trading_text.webp",
                                width:314.w,
                                fit: BoxFit.fill,
                                height:98.h,
                              ),
                            ]
                        ),
                      ),
                      Container(
                        width: double.infinity,
                        padding:EdgeInsets.symmetric(vertical: 17.h,horizontal: 32.w) ,
                        margin: EdgeInsets.only(top:16.h),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(12),
                          boxShadow: [
                            BoxShadow(
                              color: Colors.black.withAlpha(12),
                              offset: Offset(0, 4),
                              blurRadius: 8,
                              spreadRadius: 8,
                            )
                          ],
                        ),
                        child: Row(
                          children: [
                            Image.asset(
                              "assets/image/trading_history.webp",
                              width:16.h,
                              fit: BoxFit.fill,
                              height:16.h,
                            ),
                            GestureDetector(
                              behavior: HitTestBehavior.opaque,
                              onTap:(){
                                Navigator.of(context)
                                    .pushNamed('/router/exchange_history_page');
                              },
                              child: Padding(padding:EdgeInsets.only(left: 4.w),
                                  child: Text(
                                    "兑换记录",
                                    style: TextStyle(
                                        color: Color(0xFF0D0D0D),
                                        fontSize: 12.sp,
                                        fontWeight: MyFontWeight.regular),
                                  )),
                            ),
                            Spacer(),
                            Container(
                              width: 1.w,
                            height: 35.5.h,
                            margin: EdgeInsets.symmetric(),
                            color:Color(0xFFD8D8D8)),
                            Spacer(),
                            Image.asset(
                              "assets/image/trading_detail.webp",
                              width:16.h,
                              fit: BoxFit.fill,
                              height:16.h,
                            ),
                            GestureDetector(
                              behavior: HitTestBehavior.opaque,
                              onTap: (){
                                Navigator.of(context).pushNamed('/router/integral_detailed_page',arguments:{"titleType":1});
                              },
                            child: Padding(padding:EdgeInsets.only(left: 4.w),
                                child: Text(
                                  "印章明细",
                                  style: TextStyle(
                                      color: Color(0xFF0D0D0D),
                                      fontSize: 12.sp,
                                      fontWeight: MyFontWeight.regular),
                                )),)
                          ],
                        ),
                      ),
                      Container(
                        width: double.infinity,
                        padding:EdgeInsets.symmetric(vertical:12.h,horizontal: 11.w) ,
                        margin: EdgeInsets.only(top:16.h),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(12),
                          boxShadow: [
                            BoxShadow(
                              color: Colors.black.withAlpha(12),
                              offset: Offset(0, 4),
                              blurRadius: 8,
                              spreadRadius: 8,
                            )
                          ],
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Padding(padding:EdgeInsets.only(bottom:14.h),
                            child: Text(
                              "规则介绍",
                              style: TextStyle(
                                  color: Color(0xFF0D0D0D),
                                  fontSize: 15.sp,
                                  fontWeight: MyFontWeight.semi_bold),
                            ),),
                            Padding(padding:EdgeInsets.only(bottom:12.h),
                              child: Text(
                                "(1)  商品中所要求的印章达到方可兑换",
                                style: TextStyle(
                                    color: Color(0xFF353535),
                                    fontSize: 12.sp,
                                    fontWeight: MyFontWeight.regular),
                              ),),
                            Text(
                              "(2)  印章卡仅兑换商城指定商品,不能折算现金或兑换其他非指定商品项目",
                              style: TextStyle(
                                  color: Color(0xFF353535),
                                  fontSize: 12.sp,
                                  height: 1.5.h,
                                  fontWeight: MyFontWeight.regular),
                            ),
                          ],
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}