import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../YSPage.dart';
import 'base/YSNetWorking.dart';

class YSCodeLogin extends StatefulWidget {
  @override
  _YSCodeLoginState createState() => _YSCodeLoginState();
}

class _YSCodeLoginState extends State<YSCodeLogin> {
  TextEditingController _phone = TextEditingController();
  TextEditingController _code = TextEditingController();
  int _count = 60;
  Timer _timer;
  @override
  void dispose() {
    if(_timer!=null){
      _timer.cancel();
    }
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return CupertinoPageScaffold(
      backgroundColor: Colors.white,
      child: SingleChildScrollView(
        child: Column(
          children: [
            Container(
              margin: EdgeInsets.only(top: MediaQuery.of(context).padding.top+44,bottom: 60),
              alignment: Alignment.center,
              child: Text('验证码登录',style: TextStyle(fontSize: 24,color: Color(0xFF292929),decoration: TextDecoration.none),),
            ),
            Container(
              width: MediaQuery.of(context).size.width-100,
              margin: EdgeInsets.only(bottom: 10),
              height: 50,
              decoration: BoxDecoration(
                  border: Border(bottom: BorderSide(color: Color(0xFFD7D7D7),width: 0.5))
              ),
              child: CupertinoTextField(
                style: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
                placeholderStyle: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
                placeholder: '请输入手机号码',
                decoration: BoxDecoration(),
                keyboardType: TextInputType.phone,
                controller: _phone,
                maxLength: 11,
              ),
            ),
            Container(
              width: MediaQuery.of(context).size.width-100,
              height: 50,
              decoration: BoxDecoration(
                  border: Border(bottom: BorderSide(color: Color(0xFFD7D7D7),width: 0.5))
              ),
              child: CupertinoTextField(
                style: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
                placeholderStyle: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
                placeholder: '请输入验证码',
                keyboardType: TextInputType.number,
                maxLength: 4,
                decoration: BoxDecoration(),
                suffix: CupertinoButton(
                  child: Text(_count==60?'获取':'$_count秒',style: TextStyle(fontSize: 12,color: Color(0xFFB3B3B3),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
                  onPressed: _count==60?(){
                    if(_phone.text.isEmpty){
                      ysFlutterToast(context,'手机号码不能为空');
                      return;
                    }
                    getCode();
                  }:null,
                ),
                controller: _code,
              ),
            ),
            Container(
              height: 20,
              width: MediaQuery.of(context).size.width-100,
              margin: EdgeInsets.only(top: 20,bottom: 30),
              alignment: Alignment.centerRight,
              //child: Text('忘记密码？',style: TextStyle(fontSize: 12,color: Color(0xFF8E1B3C),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
            ),
            CupertinoButton(
              padding: EdgeInsets.all(0),
              child: Container(
                width: MediaQuery.of(context).size.width-100,
                height: 40,
                decoration: BoxDecoration(
                    color: Color(0xFFEA6C8F),
                    borderRadius: BorderRadius.all(Radius.circular(20))
                ),
                alignment: Alignment.center,
                child: Text('登录',style: TextStyle(fontSize: 16,color: Colors.white,decoration: TextDecoration.none),),
              ),
              onPressed: (){
                FocusScope.of(context).unfocus();
                loginToPage();
              },
            ),
            Container(
                height: 20,
                margin: EdgeInsets.only(top: MediaQuery.of(context).size.height-435),
                alignment: Alignment.center,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Container(color: Color(0xFFD7D7D7),height: 0.5,width: 70,),
                    CupertinoButton(
                      padding: EdgeInsets.all(0),
                      child: Text('密码登陆',style: TextStyle(fontSize: 12,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
                      onPressed: (){
                        FocusScope.of(context).unfocus();
                        if(_timer!=null){
                          _timer.cancel();
                        }
                        Navigator.pop(context);
                      },
                    ),
                    Container(color: Color(0xFFD7D7D7),height: 0.5,width: 70,),
                  ],
                )
            )
          ],
        ),
      ),
    );
  }

  loginToPage() async{
    if(_phone.text.isEmpty){
      ysFlutterToast(context,'手机号码不能为空');
      return;
    }
    if(_code.text.isEmpty){
      ysFlutterToast(context,'验证码不能为空');
      return;
    }
    Map dict = await ysRequestHttp(context,requestType.post, 'sign/in', {'username':_phone.text,'code':_code.text});
    if(dict!=null){
      if(_timer!=null){
        _timer.cancel();
      }
      Future<SharedPreferences> _prefer = SharedPreferences.getInstance();
      Map data = dict['data'];
      _prefer.then((value){
        value.setString('token', data['token'].toString());
      });
      Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
          builder: (context){
            return YSPage();
          }
      ), (route) => false);
    }
  }

  getCode() {
    _timer =Timer.periodic(Duration(milliseconds: 1000), (timer) {
      _count--;
      if(_count<=0){
        _count = 60;
        timer.cancel();
      }
      setState(() {});
    });
    ysRequestHttp(context,requestType.post, 'sign/sendSms', {'phone':_phone.text,'type':'login'});
  }
}
