import 'package:fengshui_compass/pages/login_page.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import '../components/controller.dart';
import '../components/custom_textfield_widget.dart';
import '../net/dio_utils.dart';

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


  @override
  State<RegisterPage> createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  final FocusNode _userNameFocusNode = FocusNode();
  final FocusNode _passwordFocusNode = FocusNode();
  final FocusNode _rePasswordFocusNode = FocusNode();

  final TextEditingController _userNameEditController = TextEditingController();
  final TextEditingController _passwordEditController = TextEditingController();
  final TextEditingController _rePasswordEditController = TextEditingController();

  bool _passwordShow = true;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // appBar: AppBar(
      //   elevation: 0,
      //   centerTitle: true,
      //   title: const Text(W
      //     '登陆',
      //     style: TextStyle(color: Colors.white),
      //   ),
      // ),
      body: SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: GestureDetector(
            onTap: () {
              _userNameFocusNode.unfocus();
              _passwordFocusNode.unfocus();
            },
            child: Stack(
              children: [
                const Hero(
                  tag: "logo",
                  child: Material(
                    color: Colors.transparent,
                    child: Image(
                      width: double.infinity,
                      image: AssetImage("assets/images/head.png"),
                      fit: BoxFit.fitWidth,
                    ),
                  ),
                ),
                Positioned(
                  top: 250,
                  left: 0,
                  right: 0,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: const [
                      Text(
                        "注册",
                        style: TextStyle(
                            fontSize: 32, fontWeight: FontWeight.w600),
                      )
                    ],
                  ),
                ),
                Positioned(
                    left: 25,
                    top: 25,
                    child: CloseButton(
                      color: Colors.white,
                      onPressed: () {
                        Navigator.of(context).pop();
                      },
                    )),
                Positioned(
                  bottom: 80,
                  right: 180,
                  left: 180,
                  child: Column(
                    children: [
                      // 输入表单
                      TextFieldWidget(
                        hintText: "手机号",
                        submit: (value) {
                          if (value.length != 11) {
                            Fluttertoast.showToast(msg: "请输入11位手机号");
                            FocusScope.of(context).requestFocus(_userNameFocusNode);
                            return;
                          }
                          _userNameFocusNode.unfocus();
                          FocusScope.of(context).requestFocus(_passwordFocusNode);
                        },
                        focusNode: _userNameFocusNode,
                        prefixIconData: Icons.phone_android_outlined,
                        controller: _userNameEditController,
                        obscureText: false,
                      ),
                      const SizedBox(height: 20),
                      TextFieldWidget(
                        hintText: "密码",
                        submit: (value) {
                          if(value.length < 5) {
                            Fluttertoast.showToast(msg: "请输入5位以上密码");
                            FocusScope.of(context).requestFocus(_passwordFocusNode);
                            return;
                          }
                          _userNameFocusNode.unfocus();
                          _passwordFocusNode.unfocus();
                          FocusScope.of(context).requestFocus(_rePasswordFocusNode);
                        },
                        focusNode: _passwordFocusNode,
                        prefixIconData: Icons.lock_open_outlined,
                        suffixIconData: _passwordShow
                            ? Icons.visibility
                            : Icons.visibility_off,
                        obscureText: _passwordShow,
                        controller: _passwordEditController,
                        onTap: () {
                          setState(() {
                            _passwordShow = !_passwordShow;
                          });
                        },
                      ),
                      const SizedBox(height: 20),
                      TextFieldWidget(
                        hintText: "再次输入密码",
                        submit: (value) {
                          // 校验两次密码是否一致
                          if(value != _passwordEditController.text) {
                            Fluttertoast.showToast(msg: "两次密码不一致");
                            FocusScope.of(context).requestFocus(_rePasswordFocusNode);
                            return;
                          }
                          _userNameFocusNode.unfocus();
                          _passwordFocusNode.unfocus();
                          _rePasswordFocusNode.unfocus();
                          submitFunction();
                        },
                        focusNode: _rePasswordFocusNode,
                        prefixIconData: Icons.lock_open_outlined,
                        suffixIconData: _passwordShow
                            ? Icons.visibility
                            : Icons.visibility_off,
                        obscureText: _passwordShow,
                        controller: _rePasswordEditController,
                        onTap: () {
                          setState(() {
                            _passwordShow = !_passwordShow;
                          });
                        },
                      ),
                      const SizedBox(height: 50),
                      // 注册
                      SizedBox(
                        width: double.infinity,
                        height: 56,
                        child: ElevatedButton(
                          onPressed: () {
                            submitFunction();
                          },
                          child: const Text("注册"),
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
          )),
    );
  }

  Future<void> submitFunction() async {
    // 获取输入用户名密码
    String userName = _userNameEditController.text;
    String password = _passwordEditController.text;

    if(userName.trim().length != 11) {
      Fluttertoast.showToast(msg: "请输入11位手机号");
      return;
    }

    if(password.trim().length < 5) {
      Fluttertoast.showToast(msg: "请输入5位以上密码");
    }

    Map<String, dynamic> map = {
      "username": userName,
      "password": password,
      "uuid": "",
      "code": ""
    };

    // 注册接口，成功跳转登陆界面
    ResponseInfo responseInfo = await DioUtils.instance.postRequest(
      url: "http://120.26.107.74:1619/register",
      jsonMap: map,
    );

    if (responseInfo.success) {

      Fluttertoast.showToast(msg: "注册成功");

      //关闭当前页面
      Navigator.of(context).pop(true);
      //发送消息更新我的页面显示内容
      loginStreamController.add(0);
    } else {
      //登录失败页面小提示
      Fluttertoast.showToast(msg: responseInfo.message);
    }

  }
}
