import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sport/pages/login/auth_code_verify.dart';
import 'package:sport/pages/login/login_widget.dart';
import 'package:sport/provider/login_info_model.dart';
import 'package:sport/router/navigator_util.dart';
import 'package:sport/widgets/appbar.dart';
import 'package:sport/widgets/dialog/alert_dialog.dart';
import 'package:sport/widgets/dialog/request_dialog.dart';

class BindPhonePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PageState();
  }
}

class _PageState extends State<BindPhonePage> {
  int _progress = 0;
  String _phone;

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

  setProgress({int progress}) {
    setState(() {
      progress != null ? _progress = progress : _progress += 1;
    });
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async => _progress > 0
          ? showDialog(
              context: context,
              builder: (context) => CustomAlertDialog(
                    title: '你真的要退出吗',
                    ok: () {
                      Navigator.of(context).pop(true);
                    },
                  ))
          : true,
      child: Scaffold(
        backgroundColor: Colors.white,
        body: CustomScrollView(slivers: <Widget>[
          SliverAppBar(
            pinned: true,
            elevation: 0,
            leading: buildBackButton(context),
            expandedHeight: 140.0,
            flexibleSpace: FlexibleSpaceBar(
              title: Text(
                "绑定手机号",
                style: titleStyle,
              ),
            ),
          ),
          SliverFillRemaining(
              hasScrollBody: false,
              fillOverscroll: false,
              child: Container(
                color: Colors.white,
                width: double.infinity,
                padding: EdgeInsets.only(top: 20.0),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    Input(
                      labelText: "请输入手机号",
                      textInputType: TextInputType.phone,
                      maxLength: 11,
                      callBack: (value) {
                        _phone = value; // onchanged
                      },
                    ),
                    Container(
                      padding: EdgeInsets.only(bottom: 48.0),
                      child: Text(
                        "请输入要绑定的手机号",
                        style: TextStyle(color: Color.fromRGBO(153, 153, 153, 1), fontSize: 12.0),
                      ),
                    ),
                    button("获取验证码", () async {
                      setProgress();
                      final _loginInfoModel = LoginInfoModel();
                      var code = await request(context, () async {
                        return await _loginInfoModel.getCaptcha(_phone).catchError((err) {});
                      });
                      if (code == true) {
                        var result = await NavigatorUtil.goPage(
                            context,
                            (context) => AuthCodeVerifyPage(_phone, (captcha) async {
                                  return await _loginInfoModel.loginApi.bindPhone(_phone, captcha);
                                }));
                        if (result != null && result['code'] == 0) {
                          _loginInfoModel.saveUserInfo({"phone": _phone});
                          Navigator.of(context).pop(true);
                        }
                        print("result $result");
                      }
                    }),
                  ],
                ),
              )),
        ]),
      ),
    );
  }
}
