import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../util/session.dart';
import '../../showAlert.dart';
import '../../util/api.dart';
import '../../util/util.dart';

class RevisePasswordPage extends StatefulWidget {
  final Widget child;

  RevisePasswordPage({
    Key key,
    this.child
  }): super(key: key);

  _RevisePasswordPageState createState() => _RevisePasswordPageState();
}

class _RevisePasswordPageState extends State < RevisePasswordPage > {

  String oldPassword = '';
  String newPassword = '';
  bool submitStatus = false;
  bool loginStatus = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text('修改密码',
          style: TextStyle(
            fontSize: ScreenUtil.getInstance().setSp(36)
          ), ),
        centerTitle: true,
        backgroundColor: Color.fromRGBO(64, 98, 254, 1),
      ),
      body: Container(
        color: Colors.white,
        child: Column(
          children: < Widget > [
            Container(
              padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(55), ScreenUtil.getInstance().setHeight(150), ScreenUtil.getInstance().setHeight(55), 0),
              child: Column(
                children: < Widget > [
                  Padding(
                    padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(60)),
                    child: TextField(
                      obscureText: true,
                      decoration: InputDecoration(
                        border: UnderlineInputBorder(),
                        hintText: "请输入旧密码",
                        hintStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(32), color: Color.fromRGBO(155, 155, 155, 1)),
                      ),
                      onChanged: (value) {
                        setState(() {
                          oldPassword = value;
                        });
                      },
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(125)),
                    child: TextField(
                      obscureText: true,
                      cursorColor: Color.fromRGBO(153, 153, 153, 0.5),
                      decoration: InputDecoration(
                        hintText: "请输入新密码",
                        hintStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(32), color: Color.fromRGBO(155, 155, 155, 1)),
                      ),
                      onChanged: (value) {
                        setState(() {
                          newPassword = value;
                        });
                      },
                    )
                  ),
                  InkWell(
                    child: Container(
                      height: ScreenUtil.getInstance().setHeight(100),
                      decoration: BoxDecoration(
                        color: (oldPassword != '' && newPassword != '') ? Color.fromRGBO(64, 98, 254, 1) : Colors.grey,
                        borderRadius: BorderRadius.circular(4),
                      ),
                      child: Center(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: < Widget > [
                            Offstage(
                              offstage: loginStatus,
                              child: SizedBox(
                                width: ScreenUtil.getInstance().setWidth(45),
                                height: ScreenUtil.getInstance().setHeight(45),
                                child: Image.asset('lib/images/loading.gif'),
                              ),
                            ),
                            Text('登录', style: TextStyle(fontSize: ScreenUtil.getInstance().setSp(36), color: Colors.white)),
                          ],
                        )
                      ),

                    ),
                    onTap: () {
                      if (oldPassword == '' || newPassword == '') {
                        return;
                      }
                      getKey('password').then((value) {
                        if (value != oldPassword) {
                          showDialog < Null > (
                            context: context, //BuildContext对象
                            barrierDismissible: false,
                            builder: (BuildContext context) {
                              return new LoadingDialog( //调用对话框
                                text: '旧密码错误，请重新输入', childCallback: (val) {},
                              );
                            });
                        } else {
                          setState(() {
                            loginStatus = false;
                          });
                          updataPasswrod(oldPassword, newPassword).then((resp) {
                            if (isNotError(context, resp)) {
                              setKey('password', newPassword);
                              showDialog < Null > (
                                context: context, //BuildContext对象
                                barrierDismissible: false,
                                builder: (BuildContext context) {
                                  return new LoadingDialog( //调用对话框
                                    text: '修改成功', childCallback: (val) {
                                      if (val) {
                                        setState(() {
                                          loginStatus = true;
                                        });
                                        Navigator.pop(context);
                                      }
                                    },
                                  );
                                });
                            }
                          });
                        }
                      });
                    },
                  )

                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}