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


class LoginPage extends StatefulWidget {

  LoginPage({
    Key key,
  }): super(key: key);

  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State < LoginPage > {

  String phone ='';
  String password='';
  bool loginStatus = true;

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334,allowFontScaling:false)..init(context);
    return Scaffold(
      body: Container(
        color: Colors.white,
        child: ListView(
          children: < Widget > [
            Container(
            padding: EdgeInsets.fromLTRB(ScreenUtil.getInstance().setHeight(55), 0, ScreenUtil.getInstance().setHeight(55), 0),
              child: Column(
                children: < Widget > [
                  Padding(
                    padding: EdgeInsets.fromLTRB(0, ScreenUtil.getInstance().setHeight(150), 0, ScreenUtil.getInstance().setHeight(60)),
                    child: Center(
                      child: Image.asset(
                        'lib/images/icon_logo.png',
                        width: ScreenUtil.getInstance().setWidth(210),
                        height: ScreenUtil.getInstance().setHeight(210),
                        fit: BoxFit.cover,
                      ),
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(60)),
                    child: TextField(
                      keyboardType: TextInputType.phone,
                      decoration: InputDecoration(
                        border: UnderlineInputBorder(),
                        hintText: "请输入手机号",
                        hintStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(32), color: Color.fromRGBO(155, 155, 155, 1)),
                      ),
                      onChanged: (value){
                        setState(() {
                          phone = value;
                        });
                      },
                    ),
                  ),
                  Padding(
                    padding: EdgeInsets.only(bottom: ScreenUtil.getInstance().setHeight(125)),
                    child: TextField(
                      cursorColor: Color.fromRGBO(153, 153, 153, 0.5),
                      keyboardType: TextInputType.text,
                      obscureText:true,
                      decoration: InputDecoration(
                        hintText: "请输入密码",
                        hintStyle: TextStyle(fontSize: ScreenUtil.getInstance().setSp(32), color: Color.fromRGBO(155, 155, 155, 1)),
                      ),
                      onChanged: (value){
                        setState(() {
                          password =value;
                        });
                      },
                    )
                  ),
                  InkWell(
                    child: Container(
                      height: ScreenUtil.getInstance().setHeight(100),
                      decoration: BoxDecoration(
                        color: (phone!=""&&password!="")?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(phone=='' || password==''){
                        return;
                      }
                      if(!loginStatus) return;//防止重复提交
                      setState(() {
                        loginStatus = false;
                      });
                      login(phone,password).then((resp){
                        if(isNotError(context, resp)){
                          Login loginObj = Login.fromJson(resp.data);
                          setKey('token', loginObj.data.token);
                          setKey('phone',phone);
                          setKey('password',password);
                          setKey('nickName',loginObj.data.nickName);
                          Navigator.pushReplacement(context, MaterialPageRoute(builder: (context){
                            return HomePage();
                          }));
                        }else{
                         setState(() {
                            loginStatus = true;
                          });
                        }
                      });
                    },
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}