import 'dart:convert';

import 'package:dio/dio.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/user_info.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:huixiang/view_widget/border_text.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class ExchangeOrderSuccessPage extends StatefulWidget {
  final Map<String, dynamic> arguments;

  ExchangeOrderSuccessPage({this.arguments});

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

class _ExchangeOrderSuccessPage extends State<ExchangeOrderSuccessPage> {
  int price = 0;
  ApiService apiService;
  @override
  void initState() {
    super.initState();
    price = double.parse((widget.arguments["points"] ?? "0").toString()).toInt() -
    double.parse((widget.arguments["price"] ?? "0").toString()).toInt();

    UserInfo userInfo;
    SharedPreferences.getInstance().then((value){
      apiService = ApiService(Dio(), context: context, token: value.getString("token"));
          userInfo = UserInfo.fromJson(jsonDecode(value.getString('user')));
          userInfo.points = "$price";
          value.setString('user', jsonEncode(userInfo.toJson()));
        });

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        title: S.of(context).zhifuxiangqing,
          titleColor: Colors.black,
          titleSize: 18.sp,
        background: Color(0xFFFAFAFA),
        leadingColor: Colors.black,
      ),
      body: Container(
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SizedBox(
              height: 46.h,
            ),
            Image.asset(
              "assets/image/icon_order_success.webp",
              width: 76.w,
              height: 76.h,
              fit: BoxFit.contain,
            ),
            SizedBox(
              height: 16.h,
            ),
            Text(
              S.of(context).duihuanchenggong,
              style: TextStyle(
                  color: Color(0xFF353535),
                  fontSize: 21.sp,
                  fontWeight: MyFontWeight.semi_bold),
            ),
            SizedBox(
              height: 70.h,
            ),
            Row(
              children: [
                Expanded(
                  child: Column(
                    children: [
                      Text(
                        widget.arguments["realPay"],
                        style: TextStyle(
                            color: Colors.black,
                            fontSize: 21.sp,
                            fontWeight: MyFontWeight.semi_bold),
                      ),
                      SizedBox(
                        height: 14.h,
                      ),
                      Text(
                        S.of(context).xiaofeijifen,
                        style: TextStyle(
                          color: Color(0xFF727272),
                          fontSize: 14.sp,
                        ),
                      )
                    ],
                  ),
                  flex: 1,
                ),
                Container(
                  width: 2.w,
                  height: 34.h,
                  color: Color(0xFFABABAB),
                  margin: EdgeInsets.only(top: 18.h, bottom: 7.h),
                ),
                Expanded(
                  child: Column(
                    children: [
                      Text(
                        "$price",
                        style: TextStyle(
                          color: Colors.black,
                          fontSize: 21.sp,
                          fontWeight: MyFontWeight.semi_bold,
                        ),
                      ),
                      SizedBox(
                        height: 14.h,
                      ),
                      Text(
                        S.of(context).keyongjifen,
                        style: TextStyle(
                          color: Color(0xFF727272),
                          fontSize: 14.sp,
                        ),
                      )
                    ],
                  ),
                  flex: 1,
                ),
              ],
            ),
            SizedBox(
              height: 40.h,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisSize: MainAxisSize.max,
              children: [
                SizedBox(
                  width: 16.w,
                ),
                Expanded(
                  child: InkWell(
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                    child: BorderText(
                      text: S.of(context).jixuduihuan,
                      textColor: Color(0xFF32A060),
                      fontSize: 16.sp,
                      fontWeight: MyFontWeight.semi_bold,
                      borderColor: Color(0xFF32A060),
                      borderWidth: 1,
                      padding: EdgeInsets.only(top: 12.h, bottom: 12.h),
                    ),
                  ),
                  flex: 1,
                ),
                SizedBox(
                  width: 16.w,
                ),
                // Expanded(
                //   child: InkWell(
                //     onTap: () {
                //       receiveToCard();
                //     },
                //     child: RoundButton(
                //       text: S.of(context).lingqudaokabao,
                //       textColor: Colors.white,
                //       backgroup: Color(0xFF32A060),
                //       radius: 4,
                //       fontWeight: MyFontWeight.semi_bold,
                //       fontSize: 16.sp,
                //       padding: EdgeInsets.only(top: 12.h, bottom: 12.h),
                //     ),
                //   ),
                //   flex: 1,
                // ),
                // SizedBox(
                //   width: 16.w,
                // ),
              ],
            )
          ],
        ),
      ),
    );
  }

  receiveToCard() async {
    BaseData baseData = await apiService.receiveToCard(widget.arguments["id"]);
    if(baseData != null && baseData.isSuccess) {
      await Navigator.of(context).pushNamed('/router/mine_card');
      Navigator.of(context).pop();
    }
  }

}