import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:http/http.dart';
import 'package:netrain_flutter_app/laishanqi/mqtt/MqttClient.dart';
import 'package:netrain_flutter_app/laishanqi/netrain/Http/HttpUtil.dart';
import 'package:netrain_flutter_app/laishanqi/netrain/user/RegisterPage.dart';

import '../PharmacistHomePage.dart';
import '../../../common/Images.dart';

/// 登录页
class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _LoginState();
}

class _LoginState extends State<LoginPage> {
  var select = 1;
  var checkText;
  final phoneController = TextEditingController();
  final codeController = TextEditingController();

  @override
  void initState() {
    super.initState();
    var mqtt = MqttClient();
    mqtt.connect();

    HttpUtil.get(HttpUtil.global_config).then((Response value) {
      Fluttertoast.showToast(msg: "global_config 请求成功 ");
    });
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
        body: SingleChildScrollView(
          child: Container(
            alignment: Alignment.topCenter,
            padding: EdgeInsets.only(left: 16, right: 16),
            child: Column(
              children: [
                // logo
                Container(
                  margin: EdgeInsets.only(top: 108),
                  child: Image.asset(Images.logo_01),
                ),
                // 验证码登录按钮 和 账号密码登录按钮
                Container(
                  height: 40,
                  margin: EdgeInsets.only(top: 91),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: tab(),
                  ),
                ),
                // 手机号输入控件
                Stack(
                  children: getPhoneView(),
                ),
                // 密码和验证码输入控件
                getCodeInputView(),
                // 登录按钮
                Container(
                  height: 45,
                  margin: EdgeInsets.only(top: 30, left: 34, right: 34),
                  decoration: BoxDecoration(
                      gradient: LinearGradient(
                          colors: [Color(0xff36D7FF), Color(0xff247DFF)]),
                      borderRadius: BorderRadius.circular(22)),
                  child: MaterialButton(
                    elevation: 0,
                    highlightElevation: 0,
                    minWidth: double.infinity,
                    color: Colors.transparent,
                    onPressed: () {
                      // 登录点击事件
                      if (phoneController.text.isEmpty) {
                        Fluttertoast.showToast(msg: "手机号不能为空");
                        return;
                      }
                      if (codeController.text.isEmpty) {
                        Fluttertoast.showToast(msg: "验证码或密码不能为空");
                        return;
                      }
                      // 跳转
                      Navigator.push(context,
                          MaterialPageRoute(builder: (context) => HomePage()));
                      Fluttertoast.showToast(
                          msg:
                              "登录 手机号：${phoneController.text}  验证码或密码：${codeController.text}");
                    },
                    child: Container(
                      child: Text(
                        "登录",
                        style: TextStyle(color: Colors.white, fontSize: 18),
                      ),
                    ),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 10),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      MaterialButton(
                        onPressed: () {
                          // 跳转到注册页，把手机号传递过去
                          Navigator.pushNamed(context, "register",
                              arguments: [phoneController.text]);
                          // Navigator.push(context,MaterialPageRoute(builder: (context) => RegisterPage()));
                        },
                        child: Text(
                          "快速注册",
                          style: TextStyle(color: Colors.blue),
                        ),
                      ),
                      Text("  |  ", style: TextStyle(color: Colors.blue)),
                      MaterialButton(
                        onPressed: () {
                          Navigator.pushNamed(context, "layout");
                          // Navigator.push(context,MaterialPageRoute(builder: (context) => RegisterPage()));
                        },
                        child: Text(
                          "忘记密码",
                          style: TextStyle(color: Colors.blue),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
    );
  }

  Container getCodeInputView() {
    var view = Container(
      height: 46,
      margin: EdgeInsets.only(left: 32, right: 32, top: 10),
      child: TextField(
        obscureText: select == 2,
        controller: codeController,
        style: TextStyle(color: Colors.blue, fontSize: 16),
        decoration: InputDecoration(
          contentPadding:
              EdgeInsets.only(left: 10, right: 10, top: 10, bottom: 10),
          hintText: checkText,
          enabledBorder: OutlineInputBorder(
            borderSide: BorderSide(color: Color(0xFFf1f1f1), width: 1),
            borderRadius: BorderRadius.circular(23),
          ),
          focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.blue, width: 1),
          ),
        ),
      ),
    );

    return view;
  }

  List<Widget> getPhoneView() {
    List<Widget> view = [
      Container(
        height: 70,
        margin: EdgeInsets.only(left: 32, right: 32, top: 20),
        child: TextField(
          maxLength: 11,
          controller: phoneController,
          keyboardType: TextInputType.numberWithOptions(),
          style: TextStyle(color: Colors.blue, fontSize: 16),
          decoration: InputDecoration(
            contentPadding: EdgeInsets.symmetric(vertical: 2, horizontal: 10),
            hintText: "请输入手机号",
            enabledBorder: OutlineInputBorder(
              borderSide: BorderSide(color: Color(0xFFf1f1f1), width: 1),
              borderRadius: BorderRadius.circular(23),
            ),
            focusedBorder: OutlineInputBorder(
              borderSide: BorderSide(color: Colors.blue, width: 1),
            ),
          ),
        ),
      ),
    ];

    var getCodeView = Positioned(
        right: 50,
        top: 33,
        child: GestureDetector(
          child: Text("获取验证码"),
          onTap: () {},
        ));

    if (select == 1) {
      view.add(getCodeView);
    }
    return view;
  }

  tab() {
    var style_01 = TextStyle(
        fontWeight: FontWeight.bold, color: Color(0xff2893FF), fontSize: 16);
    var style_02 = TextStyle(
        fontWeight: FontWeight.bold, color: Color(0xff949494), fontSize: 16);
    var currentStyle01;
    var currentStyle02;
    var image = Container(
      margin: EdgeInsets.only(top: 5),
      child: Image.asset(Images.ic_login_mode),
    );

    if (select == 1) {
      checkText = "请输入验证码";
      currentStyle01 = style_01;
      currentStyle02 = style_02;
    } else {
      checkText = "请输入密码";
      currentStyle02 = style_01;
      currentStyle01 = style_02;
    }

    // 验证码登录按钮
    var textCode = Container(
        alignment: Alignment.topCenter,
        child: GestureDetector(
          child: Text(
            "验证码登陆",
            style: currentStyle01,
          ),
          onTap: () {
            setState(() {
              select = 1;
            });
          },
        ));
    // 密码登录按钮
    var textPassword = Container(
        alignment: Alignment.topCenter,
        child: GestureDetector(
          child: Text(
            "账号密码登陆",
            style: currentStyle02,
          ),
          onTap: () {
            setState(() {
              select = 2;
            });
          },
        ));

    List<Widget> mChildren01 = [textCode];
    List<Widget> mChildren02 = [textPassword];

    if (select == 1) {
      mChildren01.add(image);
    } else {
      mChildren02.add(image);
    }

    List<Widget> mChildrenRoot = [
      Column(children: mChildren01),
      Column(children: mChildren02)
    ];

    return mChildrenRoot;
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    phoneController.dispose();
    codeController.dispose();
  }
}
