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';
import 'ExamListModel.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();
  PickedFile _image1;
  PickedFile _image2;

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

  void _getImage(int type) async {
    if(UserModel.getModel() == null){
      Fluttertoast.showToast(msg:'请登录');
      return;
    }
    var image = await ImagePicker.platform.pickImage(source: ImageSource.gallery);
    setState(() {
      if(type == 0){
        _image1 = image;
      }else {
        _image2 = image;
      }
    });
  }
  _upLoadImage() {
    List images = new List();
    if(_image1 != null){
      images.add(_image1.path);
    }
    if(_image2 != null){
      images.add(_image2.path);
    }
    LoginApi.uploadAvatarRequest(images, (httpModel, error){
      if(error != null){
        Fluttertoast.showToast(msg: error);
      }else{
        Fluttertoast.showToast(msg:'更改成功');
      }
    });
  }

  void _requestListData() {
    if (UserModel.getModel() == null) {
      Fluttertoast.showToast(msg: '请登录');
      return;
    }
    Map<String, String> map = new Map<String, String>();
    map['page'] = '0';
    LoginApi.requestListData(map, (httpModel, error) {
      if (error != null) {
        Fluttertoast.showToast(msg: error);
      } else {
        List examList = httpModel.data["content"]
            .map((m) => new ExamListModel.fromJson(m))
            .toList();
        if (examList.length > 0) {
          ExamListModel model = examList[0];
          Fluttertoast.showToast(
              msg: model.toJson().toString(), timeInSecForIos: 5);
        }
      }
    });
  }

  @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: 300,
              height: 100,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  GestureDetector(
                    child: Image.asset(_image1 != null ? _image1.path : Images.lake),
                    onTap: (){
                      _getImage(0);
                    },
                  ),
                  GestureDetector(
                    child: Image.asset(_image2 != null ? _image2.path : Images.lake),
                    onTap: (){
                      _getImage(1);
                    },
                  ),
                ],
              )
            ),
            Container(
              margin: EdgeInsets.only(top: 0),
              width: 150,
              height: 50,
              child: ElevatedButton(
                child: Text("上传多张图"),
                onPressed: _upLoadImage,
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 10),
              width: 150,
              height: 50,
              child: ElevatedButton(
                child: Text("请求列表数据"),
                onPressed: _requestListData,
              )
            ),
          ],
        ),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}