import 'package:flustars/flustars.dart' as FlutterStars;
import 'package:flutter/material.dart';

import 'package:liftmanager/internal/wode/page/input_account.dart';

import 'package:liftmanager/common/common.dart';
import 'package:liftmanager/internal/account/model/user_info_entity.dart';
import 'package:liftmanager/internal/account/provider/user_provider.dart';

import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/widgets/click_item.dart';
import 'package:provider/provider.dart';

class BandAccount extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return BandAccountState();
  }
}

class BandAccountState extends State<BandAccount>
    with AutomaticKeepAliveClientMixin {
  // PersonalPageState({Key key}) : super(key: key);
  UserProvider provider = UserProvider();

  TextEditingController _nameController = new TextEditingController();
  TextEditingController _alipayController = new TextEditingController();

  @override
  void initState() {
    super.initState();
    getUserInfo();
  }

  ///获取用户信息
  void getUserInfo() {
    ApiService().userInfo(
        onSuccess: (data) {
          setUser(data);
        },
        onError: (code, msg) {});
  }

  void setUser(UserInfoEntity user) {
    provider.setUser(user);
  }

  updateUser({name, phone}) {
    NewApiService().updateUser({
      "userId": FlutterStars.SpUtil.getString(Constant.userId),
      "alipayAccount": phone,
      "alipayRealName": name
    }, onSuccess: (res) {
      getUserInfo();
      setState(() {});
    }, onError: (code, msg) {
      toasts(msg);
    });
  }

  @override
  void dispose() {
    // provider.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;

    return ChangeNotifierProvider<UserProvider>(
        create: (_) => provider,
        child: Container(
          child: Scaffold(
              resizeToAvoidBottomPadding: false,
              appBar: MyAppBar(
                centerTitle: "绑定账号",
              ),
              body: Consumer<UserProvider>(builder: (_, provider, __) {
                return ListView(
                    padding: EdgeInsets.all(0.0),
                    children: <Widget>[
                      ClickItem(
                        title: "支付宝账号",
                        content: "${provider.user?.alipayAccount ?? ""}",
                        onTap: () {
                          Navigator.push(context,
                              MaterialPageRoute(builder: (_) {
                            return InputAccount(
                              name: provider.user?.alipayRealName,
                              phone: provider.user?.alipayAccount,
                              backBlock: (phone, name) {
                                updateUser(name: name, phone: phone);
                              },
                            );
                          }));
                        
                 
                          if (provider.user?.alipayAccount != null) {
                            _alipayController.text =
                                provider.user?.alipayAccount;
                          }
                          if (provider.user?.alipayRealName != null) {
                            _nameController.text =
                                provider.user?.alipayRealName;
                          }

                      

                            
                          
                        },
                      ),
                    ]);
              })),
        ));
  }

  @override
  bool get wantKeepAlive => true;
}
