import 'package:flutter/material.dart';
import '../../components/commonFun.dart';
import '../../widget/JdText.dart';
import '../../widget/JdButton.dart';

import '../../config/Config.dart';
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';
import '../../services/Storage.dart';
import 'dart:convert';
import '../../components/EncryptUtil.dart';

import '../../services/EventBus.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';


class ForgotPassword extends StatefulWidget {
  ForgotPassword({Key key}) : super(key: key);

  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<ForgotPassword> {
  //监听登录页面销毁的事件
  dispose() {
    super.dispose();
    eventBus.fire(new UserEvent('登录成功...'));
  }

  doLogin() async {
    //临时跳转
    Navigator.pushNamed(context, '/', arguments: g_iIndex);
    return;

    RegExp reg = new RegExp(r"^1\d{10}$");
    if (!reg.hasMatch(g_userInfo.username)) {
      Fluttertoast.showToast(
        msg: '手机号格式不对',
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
      );
    } else if (g_userInfo.password.length < 6) {
      Fluttertoast.showToast(
        msg: '密码不正确',
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
      );
    } else {
      var api = '${Config.domain}api/doLogin';
      var response = await Dio().post(api, data: {
        "username": g_userInfo.username,
        "password": g_userInfo.password
      });
      if (response.data["success"]) {
        print(response.data);
        //保存用户信息
        Storage.setString('userInfo', json.encode(response.data["userinfo"]));

        Navigator.pop(context);
      } else {
        Fluttertoast.showToast(
          msg: '${response.data["message"]}',
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text("找回密码"),
        centerTitle: true,
      ),
      body: Container(
        padding: EdgeInsets.only(top: 20, bottom: 20, left: 20, right: 20),
        child: ListView(
          children: <Widget>[
            Center(
              child: Container(
                margin: EdgeInsets.only(top: 30),
                height: ScreenUtil().setWidth(160),
                width: ScreenUtil().setWidth(160),
                //child: Image.asset('assets/images/user.png', fit: BoxFit.cover),
                child: Image.asset('assets/images/ybsthbj.png', fit: BoxFit.fitHeight),
                // child: Image.network(
                //     'https://www.itying.com/images/flutter/list5.jpg',
                //     fit: BoxFit.cover),
              ),
            ),
            SizedBox(height: 30),
            JdText(
              height: ScreenUtil().setHeight(300),
              title: '账号：',
              text: "请输入账号（手机号）",
              onChanged: (String value) {
                // print(value);
                g_userInfo.username = value;
              },
              endBtn: 'ClearBtn',
            ),
            SizedBox(height: 10),
            JdText(
              height: ScreenUtil().setHeight(300),
              title: '验证码：',
              text: "请输入短信验证码",
              password: true,
              onChanged: (String value) {
                // print(value);
                g_userInfo.password = value;
              },
              endBtn: 'OutlineButton',
            ),
            SizedBox(height: 10),
            JdText(
              height: ScreenUtil().setHeight(300),
              title: '新密码：',
              text: "请输入6-12位新密码",
              password: true,
              onChanged: (String value) {
                // print(value);
                g_userInfo.password = value;
              },
              endBtn: 'ShowHiddenBtn',
            ),
            SizedBox(height: 10),
            JdText(
              height: ScreenUtil().setHeight(300),
              title: '新密码：',
              text: "请再次输入6-12位新密码",
              password: true,
              onChanged: (String value) {
                // print(value);
                g_userInfo.password = value;
              },
              endBtn: 'ShowHiddenBtn',
            ),
            SizedBox(height: 40),
            JdButton(
              height: ScreenUtil().setHeight(350),
              text: "确认",
              color: Colors.blueAccent,
              onTop: doLogin,
            )
          ],
        ),
      ),
    );
  }
}
