import 'dart:io';
import 'package:flutter/material.dart';
import 'package:liftmanager/res/gaps.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';
import 'package:liftmanager/internal/search/search_router.dart';
import 'package:liftmanager/widgets/app_search_bar.dart';
import 'package:liftmanager/utils/image_utils.dart';
import 'package:liftmanager/res/resources.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/widgets/load_image.dart';
import 'package:liftmanager/internal/bbs/bbs_router.dart';
import 'package:liftmanager/widgets/click_item.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:liftmanager/internal/account/account_router.dart';
import 'package:liftmanager/common/common.dart';
import 'package:liftmanager/internal/account/provider/user_provider.dart';
import 'package:liftmanager/internal/account/model/user_info_entity.dart';
import 'package:liftmanager/internal/team/team_router.dart';
import 'package:liftmanager/common/user_db.dart';
import 'package:liftmanager/internal/wode/page/edit_text_page.dart';
import 'package:image_picker/image_picker.dart';
import 'package:liftmanager/utils/utils.dart';
import 'package:liftmanager/internal/wode/wode_router.dart';
import 'package:flustars/flustars.dart' as FlutterStars;
import 'package:liftmanager/widgets/text_field_item.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(){
    NewApiService().updateUser({
          "userId":FlutterStars.SpUtil.getString(Constant.userId),
          "alipayAccount":_alipayController.text,
          "alipayRealName":_nameController.text
        }, 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: () {
                          // NavigatorUtils.push(context, BbsRouter.videoDetail);
                          // Navigator.of(context).push(
                          //   MaterialPageRoute(
                          //     builder: (context) {
                          //       return EditTextPage(
                          //         title: "编辑支付宝账号",
                          //         value: provider.user?.alipayAccount ?? "",
                          //         mType: 0,
                          //       );
                          //     },
                          //   ),
                          // ).then(
                          //   (value) {
                          //     if (value != null) {
                          //       print(value);
                          //       updateUser(value);
                          //     }
                          //   },
                          // );
                          if(provider.user?.alipayAccount != null){
                            _alipayController.text = provider.user?.alipayAccount;
                          }
                          if(provider.user?.alipayRealName != null){
                            _nameController.text = provider.user?.alipayRealName;
                          }
                        
                        
                      //     showDialog(
                      //   context: context,
                      //   barrierDismissible: true,  // 是否可以点击底部遮罩层取消dialog
                      //   // child: Text('data')
                      //   builder: (context) {
                      //     return Container(
                      //       color:Colors.white,
                      //       width: 300,
                      //       // width: width*0.6,
                      //       height: 300,
                      //       // height: height*0.6,
                      //       child: Column(
                      //         children: <Widget>[
                      //           TextFieldItem(
                      //             title: "支付宝账号",
                      //             isMust: true,
                      //             content: _alipayController.text,
                      //             controller: _alipayController,
                      //             hintText: "请输入您的支付宝账号",
                                  
                      //           ),
                      //           TextFieldItem(
                      //             title: "真实姓名",
                      //             isMust: true,
                      //             content: _nameController.text,
                      //             controller: _nameController,
                      //             hintText: "请输入您的真实姓名",
                                  
                      //           ),

                      //           Container(
                      //             child: Row(
                      //               children: <Widget>[
                      //                 FlatButton(
                      //             color: Color(0xffcccccc),
                      //             child: Text('取消',style: TextStyle(color:Colors.white),),
                      //             onPressed: (){
                      //               Navigator.maybePop(context);
                      //               _nameController.text = "";
                      //               _alipayController.text = "";
                      //             },
                      //           ),
                      //                 Container(
                      //             height:36,
                      //             decoration: BoxDecoration(
                      //               gradient: const LinearGradient(
                      //                   colors: [Color(0xFF00D9FF), Color(0xFF0287FF)]),
                      //             ),
                      //             child: FlatButton(
                      //               child: Text('确认',style: TextStyle(color:Colors.white),),
                      //               onPressed: (){
                      //                 if(_alipayController.text == null || _alipayController.text.trim()==""){
                      //                   toasts("请输入您的支付宝账号");
                      //                   return;
                      //                 }
                      //                 if(_nameController.text == null || _nameController.text.trim()==""){
                      //                   toasts("请输入您的真实姓名");
                      //                   return;
                      //                 }
                      //                 updateUser();
                      //               },
                      //             ),
                      //           ),

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

                      showDialog(
        context: context,
        builder: (context) {
          return new AlertDialog(
            // title: new Text(""),
            content: Container(
              child: 
              // Text("123")
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                                        child: Text(
                                          "您的支付宝账号:",
                                          textAlign: TextAlign.left,
                                          style: TextStyle(
                                            fontSize: ScreenUtil().setSp(18),
                                          ),
                                        ),
                                      ),
                                      SizedBox(
                    height:10
                  ),
                  Container(
                                height: ScreenUtil().setWidth(30),
                                child: TextField(
                                controller: _alipayController,
                                decoration: InputDecoration(
                                  contentPadding: EdgeInsets.only(left:ScreenUtil().setWidth(10),right:ScreenUtil().setWidth(5),),
                                  border: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(2.0),
                                  ),
                                  hintText: '请输入您的支付宝账号', // hint 暗示
                                  errorBorder: InputBorder.none, // 当error时的border
                                  filled: false, // 背景色
                                  focusedBorder: OutlineInputBorder(), // 当focus时的border
                                ),
                                cursorColor: Color(0xFF999999),
                                obscureText: false, // 输入变成**
                                onChanged: (val) {
                                  // xxx
                                },
                              ),
                  ),
                  SizedBox(
                    height:20
                  ),
                  Container(
                                        child: Text(
                                          "您的真实姓名:",
                                          textAlign: TextAlign.left,
                                          style: TextStyle(
                                            fontSize: ScreenUtil().setSp(18),
                                          ),
                                        ),
                                      ),
                                      SizedBox(
                    height:10
                  ),
                  Container(
                                height: ScreenUtil().setWidth(30),
                                child: TextField(
                                controller: _nameController,
                                decoration: InputDecoration(
                                  contentPadding: EdgeInsets.only(left:ScreenUtil().setWidth(10),right:ScreenUtil().setWidth(5),),
                                  border: OutlineInputBorder(
                                      borderRadius: BorderRadius.circular(2.0),
                                  ),
                                  hintText: '请输入您的真实姓名', // hint 暗示
                                  errorBorder: InputBorder.none, // 当error时的border
                                  filled: false, // 背景色
                                  focusedBorder: OutlineInputBorder(), // 当focus时的border
                                ),
                                cursorColor: Color(0xFF999999),
                                obscureText: false, // 输入变成**
                                onChanged: (val) {
                                  // xxx
                                },
                              ),
                  ),
                            
                            
                ],
              ),
            ),
            actions: <Widget>[
              new FlatButton(
                onPressed: () {
                  Navigator.of(context).pop();
                  _nameController.text = "";
                                    _alipayController.text = "";
                },
                child: new Text("取消"),
              ),
              new FlatButton(
                onPressed: () {
                  if(_alipayController.text == null || _alipayController.text.trim()==""){
                                        toasts("请输入您的支付宝账号");
                                        return;
                                      }
                                      if(_nameController.text == null || _nameController.text.trim()==""){
                                        toasts("请输入您的真实姓名");
                                        return;
                                      }
                                      updateUser();
                  Navigator.of(context).pop();
                },
                child: new Text("确认"),
              ),
              
            ],
          );
        });
                        },
                      ),
                      
                    ]);
              })),
        ));
  }

  @override
  bool get wantKeepAlive => true;
}
