import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:netrain_flutter_app/zhangfeng/UserModel.dart';
import 'LoginApi.dart';
import 'package:netrain_flutter_app/common/Images.dart';

class LoginRequestPage extends StatefulWidget {
  final String title = "登录页";

  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<LoginRequestPage> {
  // This widget is the root of your application.
  final phoneTextField = TextEditingController();
  final pwdTextField = TextEditingController();

  @override
  void dispose() {
    // Clean up the controller when disposing of the Widget.
    phoneTextField.dispose();
    pwdTextField.dispose();
    super.dispose();
  }

  void _loginAction() {
    if(UserModel.getModel() != null){
      Fluttertoast.showToast(msg:'已经登录');
      return;
    }
    Map<String,String> map = new Map<String,String>();
    map['workNumber'] = 'NTR000007';
    map['password'] = '123456';
    map['model'] = 'iPhone 12';
    map['grant_type'] = 'worknumber_password';
    map['client_id'] = 'app';
    map['client_secret'] = '123456';
    map['platform'] = '0';
    LoginApi.loginRequest(map, (httpModel, error){
      if(error != null){
        Fluttertoast.showToast(msg: error);
      }else{
        Fluttertoast.showToast(msg:'登录成功');
      }
    });
  }
  void _logoutAction() {
    UserModel.removeModel();
    Fluttertoast.showToast(msg:'退出成功');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
            Container(
              child:Image(image: AssetImage(Images.logo_01)),
              padding: EdgeInsets.only(top: 50,left: 50,right: 50),
            ),
            Container(
              padding: EdgeInsets.only(top: 100,left: 16,right: 16),
              child: TextField(
                controller: phoneTextField,
                decoration: InputDecoration(hintText: "请输入手机号码",border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(30.0),
                    borderSide: BorderSide()),),
              ),
            ),
            Container(
              padding: EdgeInsets.only(top: 20,left: 16,right: 16),
              child: TextField(
                controller: pwdTextField,
                decoration: InputDecoration(hintText: "请输入密码",border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(30.0),
                    borderSide: BorderSide()),
                    suffixIcon: IconButton(
                      icon: Icon(
                        Icons.clear,
                        color: Colors.black45,
                      ),
                      onPressed: (){
                        pwdTextField.clear();
                      },
                    )

                ),
                obscureText: true, //是否是密码
              ),
            ),
            Container(
              padding: EdgeInsets.only(top: 50,left: 0,right: 0),
              child: TextButton(
                child: Stack(
                  alignment: const Alignment(0.0, 0.0),
                  children: [
                    Image.asset(Images.ic_login_button),
                    Text("登录",style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                    )),
                  ],
                ),
                onPressed: _loginAction,
              ),
            ),
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}