import 'package:flutter/material.dart';
import 'package:zhiying_comm/zhiying_comm.dart';

///渠道授权成功页面
class AuthSuccessPage extends StatefulWidget {
  final AuthResultType authResultType;

  const AuthSuccessPage({Key key, this.authResultType}) : super(key: key);

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

class _AuthSuccessPageState extends State<AuthSuccessPage> {
  String title = '';
  String contentTitle = '';
  String content = '';
  String btntext = '';

  @override
  void initState() {
    switch (widget.authResultType) {
      case AuthResultType.success:
        title = '授权成功';
        contentTitle = '恭喜您授权成功';
        content = '前往享受超多优惠吧';
        btntext = '知道了';
        break;
      case AuthResultType.error:
        title = '授权失败';
        contentTitle = '授权失败';
        content = '很抱歉，您的淘宝账号已经成为了合作方，如需申请渠道授权，您可更换其他淘宝账号';
        btntext = '重新授权';
        break;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        brightness: Brightness.light,
        leading: IconButton(
          onPressed: () {
            Navigator.pop(context);
          },
          icon: Icon(
            Icons.arrow_back_ios,
            color: HexColor.fromHex("#FF333333"),
            size: 18,
          ),
        ),
        centerTitle: true,
        elevation: 1,
        backgroundColor: Colors.white,
        title: Text(
          title,
          style: TextStyle(color: HexColor.fromHex("#FF333333"), fontSize: 17),
        ),
      ),
      body: Container(
        child: Center(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(
                height: 30,
              ),
              Image.asset(
                "assets/images/auth/auth_success.png",
                package: 'zhiying_base_widget',
                width: 107,
                height: 107,
                fit: BoxFit.fill,
              ),
              SizedBox(
                height: 30,
              ),
              Text(
                contentTitle,
                style: TextStyle(color: HexColor.fromHex("#FF333333"), fontSize: 15),
              ),
              Container(
                margin: EdgeInsets.only(left: 56, right: 56),
                child: Text(
                  content,
                  style: TextStyle(color: HexColor.fromHex("#FF999999"), fontSize: 13),
                ),
              ),
              SizedBox(
                height: 30,
              ),
              GestureDetector(
                onTap: () {
                  onClick();
                },
                child: Container(
                  height: 46.5,
                  width: 265,
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(100),
                      boxShadow: [BoxShadow(color: HexColor.fromHex("FFFF5500"), blurRadius: 1, spreadRadius: 2, offset: Offset(1, 4))],
                      gradient: LinearGradient(colors: [HexColor.fromHex("#FFFF8E00"), HexColor.fromHex("#FFFF5500")], begin: Alignment.topCenter, end: Alignment.bottomCenter)),
                  child: Center(
                    child: Text(
                      btntext,
                      style: TextStyle(color: HexColor.fromHex("#FFFFFFFF"), fontSize: 13),
                    ),
                  ),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  ///点击事件
  void onClick() async {
    if (widget.authResultType == AuthResultType.success) {
      Navigator.maybePop(context);
    } else if (widget.authResultType == AuthResultType.error) {
      ///继续授权
      await TaobaoAuth.auth(context).then((value) {
        Navigator.pop(context);
      });
    }
  }
}

enum AuthResultType { success, error }
