import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.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/flutter_utils.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:huixiang/view_widget/pay_selected_dialog.dart';
import 'package:pin_input_text_field/pin_input_text_field.dart';
import 'package:shared_preferences/shared_preferences.dart';

class PlatformPayCodeSuccess extends StatefulWidget {
  final Map<String, dynamic> arguments;
  PlatformPayCodeSuccess({this.arguments});

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

class _PlatformPayCodeSuccess extends State<PlatformPayCodeSuccess> {
  ApiService apiService;
  int codeType = 0;
  UserInfo userInfo;
  String againInputCode = "";

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

  ///修改支付密码
  modifyPayCode(String payCode,String code) async {
    if (apiService == null) {
    SharedPreferences value = await SharedPreferences.getInstance();
    apiService = ApiService(Dio(),
        context: context, token: value.getString("token"), showLoading: false);
  }
    BaseData baseData = await apiService.updatePayPwd({
      "newPayPwd": payCode,
      "smsCode": code,
    }).catchError((error) {});
    if (baseData != null && baseData.isSuccess) {
      setState(() {
        codeType=4;
      });
      SmartDialog.showToast(baseData.data ?? false, alignment: Alignment.center);
    } else {
      setState(() {
        codeType = 5;
      });
      SmartDialog.showToast(baseData.msg, alignment: Alignment.center);
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.white,
            leading: GestureDetector(
                child: Icon(
                  Icons.arrow_back_ios,
                  color: Colors.black,
                ),
                onTap: () {
                  Navigator.of(context).pop();
                }),
            title: Text(
              S.of(context).pingtaizhifumima,
              style: TextStyle(
                fontWeight: MyFontWeight.regular,
                fontSize: 17.sp,
                color: Color(0xFF0D0D0D),
              ),
            ),
            centerTitle: true,
            elevation: 0.0,
          ),
          body: Column(
            children: [
              if(codeType!=4 && codeType != 5)
              againSettingCode(),
              if(codeType == 4 || codeType == 5)
                codeSuccess(),
            ],
          ),
        ));
  }

  ///再次输入密码
  Widget againSettingCode() {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: 28.h),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            "请再次输入6位数字密码",
            style: TextStyle(
              fontWeight: MyFontWeight.semi_bold,
              fontSize: 18.sp,
              color: Color(0xFF353535),
            ),
          ),
          SizedBox(
            height: 12.h,
          ),
          Text(
            "将用于一心回乡APP下单时平台余额消费",
            style: TextStyle(
              fontWeight: MyFontWeight.medium,
              fontSize: 14.sp,
              color: Color(0xFFA29E9E),
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: 24.h, bottom: 31.h,right: 35.w,left: 35.w),
            height: 45.h,
            child: PinInputTextField(
              onChanged: (txtCode){
              print(txtCode);
              setState(() {
                againInputCode = txtCode;
              });
            },
              decoration: BoxLooseDecoration(
                  strokeColorBuilder: FixedColorBuilder(Color(0xFFEBEAEA)),
                  textStyle: TextStyle(
                    fontWeight: MyFontWeight.medium,
                    fontSize: 18.sp,
                    color: Color(0xFF353535),
                  ),
                  radius: Radius.circular(4.r)),
            ),
          ),
          GestureDetector(
            onTap: (){
              setState(() {
                if(againInputCode.length == 6 && (widget.arguments["inputCode"] == againInputCode)){
                  modifyPayCode(againInputCode,widget.arguments["inputText"]);
                }else{
                  if(widget.arguments["inputCode"] != againInputCode){
                    SmartDialog.showToast("请输入与上次一致的6位数字密码", alignment: Alignment.center);
                  }else{
                    SmartDialog.showToast("请再次输入6位数字密码", alignment: Alignment.center);
                  }
                }
              });
            },
            child: Container(
              width: 163.w,
              height: 46.h,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: againInputCode.length == 6 ? Color(0xFF32A060):Color(0xFFBBE7CC),
                borderRadius: BorderRadius.circular(23.r),
              ),
              child: Text(
               "确认",
                style: TextStyle(
                  fontWeight: MyFontWeight.semi_bold,
                  fontSize: 18.sp,
                  color: Color(0xFFFFFFFF),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

  ///设置密码成功状态
  Widget codeSuccess() {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: 31.h),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Image.asset(
            codeType == 5 ? "assets/image/lose.webp":"assets/image/icon_order_success.webp",
            fit: BoxFit.cover,
            width: 76,
            height: 76,
          ),
          SizedBox(
            height: 16.h,
          ),
          Text(
            codeType == 5 ? "设置密码失败":"设置密码成功",
            style: TextStyle(
              fontWeight: MyFontWeight.semi_bold,
              fontSize: 21.sp,
              color: Color(0xFF353535),
            ),
          ),
          SizedBox(
            height: 69.h,
          ),
          GestureDetector(
            onTap: () {
              Navigator.of(context).pop();
            },
            child: Container(
              width: double.infinity,
              height: 46.h,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Color(0xFF32A060),
                borderRadius: BorderRadius.circular(4.r),
              ),
              margin: EdgeInsets.symmetric(horizontal: 16.w),
              child: Text(
                S.of(context).queren,
                style: TextStyle(
                  fontWeight: MyFontWeight.semi_bold,
                  fontSize: 18.sp,
                  color: Color(0xFFFFFFFF),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

  ///平台支付密码弹窗
  assessShowBottomSheet() {
    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      builder: (context) {
        return PaySelectedDialog();
      },
    );
  }
}