import 'package:flustars/flustars.dart';
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';
import 'package:image_picker/image_picker.dart';//从相册里面选择图片或者拍照获取照片
import 'dart:io';

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();
  PickedFile _image;

  @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:'退出成功');
  }

  Future getImage() async {
    var image = await ImagePicker.platform.pickImage(source: ImageSource.gallery);
    _upLoadImage(image);//上传图片
    setState(() {
      _image = image;
    });
  }
  _upLoadImage(PickedFile image) async {
    LoginApi.uploadAvatarRequest(image.path, (httpModel, error){
      if(error != null){
        Fluttertoast.showToast(msg: error);
      }else{
        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,
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 50),
              width: 100,
              height: 100,
              child: ElevatedButton(
                child: Text("上传头像"),
                onPressed: getImage,
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 50),
              width: 100,
              height: 100,
              child: Image.asset(_image != null ? _image.path : Images.lake),
            ),
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}