import 'package:flutter/material.dart';
import '../styles/colors.dart';
import 'dart:ui';
import '../styles/totast.dart';
import '../widget/ITextInput.dart';
import 'package:redux/redux.dart';
import 'package:flutter_redux/flutter_redux.dart';
import '../redux/AppState.dart';
import '../model/UserInfo.dart';
import '../net/HttpManager.dart';
import '../net/Result.dart';
import '../redux/UserRedux.dart';

class ChangeUserInfo extends StatefulWidget {
  ChangeUserInfo({Key key, this.title, this.val}) : super(key: key);
  final String title; // 用来储存传递过来的值
  final String val; // 用来储存传递过来的值
  @override
  ChangeUserInfoState createState() => ChangeUserInfoState();
}

class ChangeUserInfoState extends State<ChangeUserInfo> {
  Map userInfo = {};
  String changeText = '';
  String changekey = 'nickname';

  @override
  void initState() {
    super.initState();
    switch (widget.title) {
      case '昵称':
        changekey = 'nickname';
        break;
      case '手机号':
        changekey = 'phone';
        break;
    }
    changeText = widget.val;
  }

  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, UserInfo>(
        converter: (Store store) => store.state.userInfo,
        builder: (context, userInfo) {
          return WillPopScope(
              child: Scaffold(
                  appBar: AppBar(
                    // backgroundColor: PRIMARY_COLOR,
                    title: Text('修改' + widget.title),
                    centerTitle: true,
                    elevation: 0,
                  ),
                  body: Container(
                    padding: EdgeInsets.symmetric(vertical: 30, horizontal: 15),
                    color: BG_COLOR,
                    child: Column(
                      children: <Widget>[
                        Container(
                          child: ITextField(
                            autofocus: true,
                            inputText: changeText,
                            maxLength: 15,
                            hintText: '输入用户' + widget.title,
                            hintStyle: TextStyle(
                              fontSize: 16,
                              color: Color(0xFF727785),
                            ),
                            textStyle: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.w500),
                            fieldCallBack: (content) {
                              setState(() {
                                changeText = content;
                              });
                            },
                            counterStyle: TextStyle(color: BG_SUB_COLOR, fontSize: 0),
                          ),
                        ),
                        Container(


                          
                          margin: EdgeInsets.only(top: 30),
                          width: double.infinity,
                          height: 48,
                          child: FlatButton(
                            textColor: Colors.white,
                            color: PRIMARY_COLOR,
                            highlightColor: Color(0xFF763434),
                            child: Text(
                              '确定修改',
                              style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
                            ),
                            onPressed: () async {
                              if (changeText.isEmpty) {
                                Toast.show(context, '修改内容不能为空', 1500, 'info');
                                return;
                              }
                              Toast.show(context, '加载中', -1, 'loading');
                              final Result res = await HttpManager.post('userInfo/update', data: {'id': userInfo.id, changekey: changeText});
                              Toast.hide();
                              if (res.success) {
                                Result res2 = await HttpManager.get('userInfo/getUserInfo');
                                if (res2.success) {
                                  StoreProvider.of<AppState>(context).dispatch(UpdateUserAction(UserInfo.fromJson(res2.data)));
                                  Toast.show(context, '修改成功', 1000, 'success');
                                  Future.delayed(Duration(milliseconds: 1000), () {
                                    Navigator.of(context).pop();
                                  });
                                } else {}
                              } else {
                                Toast.show(context, res.error, 1500, 'info');
                              }
                            },
                          ),
                        ),
                      ],
                    ),
                  )),
              onWillPop: () {
                Toast.hide();
                Navigator.pop(context, false);
                return Future.value(false);
              });
        });
  }
}
