import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutterappfuyou/code/YSlogin.dart';
import 'package:flutterappfuyou/code/base/YSBase.dart';
import 'package:flutterappfuyou/code/base/YSNetWorking.dart';
import 'package:shared_preferences/shared_preferences.dart';

class YSChangePassWord extends StatefulWidget {
  const YSChangePassWord({Key key}) : super(key: key);

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

class _YSChangePassWordState extends State<YSChangePassWord> {
  List _showArray = [
    {'title':'旧密码','isObscure':true},
    {'title':'新密码','isObscure':true},
    {'title':'再次输入新密码','isObscure':true},
  ];
  TextEditingController _newField = TextEditingController();
  TextEditingController _passField = TextEditingController();
  TextEditingController _wordField = TextEditingController();
  @override
  void dispose() {
    _newField.dispose();
    _passField.dispose();
    _wordField.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ysright: GestureDetector(
        onTap: () async{
          FocusScope.of(context).unfocus();
          if(_passField.text.isEmpty){
            ysFlutterToast(context, '请输入旧密码');
            return;
          }
          if(_newField.text.isEmpty){
            ysFlutterToast(context, '请输入新密码');
            return;
          }
          if(_wordField.text.isEmpty){
            ysFlutterToast(context, '请再次输入旧密码');
            return;
          }
          if(_newField.text!=_wordField.text){
            ysFlutterToast(context, '两次新密码输入不一致');
            return;
          }
          Map request = {};
          request['password_old'] = _passField.text;
          request['password_confirm'] = _wordField.text;
          request['password_new'] = _newField.text;
          Map dict = await ysRequestHttp(context, requestType.post, 'i/changePassword', request);
          if(dict!=null){
            ysFlutterToast(context, '密码已更改请重新登陆');
            SharedPreferences  _perfer = await SharedPreferences.getInstance();
            _perfer.remove('token');
            Navigator.pushAndRemoveUntil(context, MaterialPageRoute(
                builder: (context){
                  return YSLogin(isCheck: true,);
                }
            ), (route) => false);
          }
        },
        child: Container(
          child: Text('提交',style: TextStyle(fontSize: 15,color: Colors.white,decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
          height: 40,
          width: 40,
          alignment: Alignment.center,
        )
      ),
      ystitle: '修改密码',
      yschild: ListView.separated(
        padding: EdgeInsets.only(left: 20,right: 20,top: 20,bottom: 20),
          itemBuilder: (context,index){
          Map item = _showArray[index];
          return Container(
            child: LayoutBuilder(
              builder: (context,conSize){
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(item['title'],style: TextStyle(fontSize: 12,color: Color(0xFF808080),decoration: TextDecoration.none,fontWeight: FontWeight.w600),),
                    Container(
                      height: 40,
                      margin: EdgeInsets.only(top: 10),
                      width: conSize.maxWidth,
                      child: CupertinoTextField(
                        placeholder: '请输入',
                        style: TextStyle(fontSize: 12,color: Color(0xFF808080)),
                        controller: item['title']=='旧密码'?_passField:item['title']=='再次输入新密码'?_wordField:_newField,
                        obscureText: item['isObscure'],
                        suffix: GestureDetector(
                          onTap: (){
                            item['isObscure'] = !item['isObscure'];
                            setState(() {});
                          },
                          child: Padding(
                            padding: EdgeInsets.only(right: 10),
                            child: Image.asset('lib/images/${item['isObscure']==true?'eye-slash':'eye'}.png',height: 15,width: 15,)
                          ),
                        ),
                      ),
                    )
                  ],
                );
              },
            ),
          );
          },
          separatorBuilder: (context,index){
            return Container(height: 20,);
          },
          itemCount: _showArray.length
      ),
    );
  }
}
