/*
*@Author: XianKaiQun
*@Date: 2020-04-20 16:39:51
 * @LastEditors: wuwei
 * @LastEditTime: 2023-12-21 17:16:42
*@Descripttion:
*/
part of 'index.dart';

class _PageView extends StatefulWidget {
  _PageView({Key? key}) : super(key: key);
  @override
  _PageViewState createState() => _PageViewState();
}

class _PageViewState extends State<_PageView> {
  _Store get store => Provider.of(context, listen: false);
  @override
  void initState() {
    super.initState();
  }

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

  // Future<void> onInitDialog() async {
  //   if (mounted && store.isDialogShowed == false) {
  //     Future.delayed(Duration(milliseconds: 300)).then((e) {
  //       setState(() {
  //         onInitShowDialog();
  //       });
  //     });
  //   }
  // }

  // /// 隐私政策弹窗
  // void onInitShowDialog() {
  //   showGeneralDialog(
  //       context: WNavUtil.instance!.navigatorKey.currentContext!,
  //       barrierDismissible: false,
  //       barrierLabel: '',
  //       transitionDuration: Duration(milliseconds: 200),
  //       pageBuilder: (BuildContext context, Animation<double> animation,
  //           Animation<double> secondaryAnimation) {
  //         return _CenterWebView(
  //           onNotAgree: () => {
  //             store.onSetAgree(false),
  //             Navigator.of(WNavUtil.instance!.navigatorKey.currentContext!)
  //                 .pop(),
  //           },
  //           onAgree: () => {
  //             store.onSetAgree(true),
  //             Navigator.of(WNavUtil.instance!.navigatorKey.currentContext!)
  //                 .pop(),
  //           },
  //         );
  //       });
  // }

  final _entCodeFocus = FocusNode();
  final _accountFocus = FocusNode();
  final _passwordFocus = FocusNode();

  buildItem(
      {String? hintText,
      String? title,
      String? asset,
      bool isPassword = false,
      bool showPassWord = false,
      bool isNum = false,
      onchange,
      focusNode,
      String? initialValue,
      Function(String)? onChanged,
      String? Function(String?)? validator}) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    Widget? suffix;
    if (isPassword) {
      suffix = Consumer<_Store>(
        builder: (ctx, store, child) {
          return GestureDetector(
            onTap: () => store.setshowPassword(),
            child: Wisdom(
              padding: EdgeInsets.only(left: 15.pt),
              child: Image(
                image: store.showPassWord == true
                    ? AssetList.$login_ly_png_image
                    : AssetList.$login_hy_png_image,
                height: 15.pt,
                width: 25.pt,
              ),
            ),
          );
        },
      );
    }
    return Consumer<_Store>(
      builder: (ctx, store, child) {
        return Wisdom.column(
          color: Color(0xffF6F7FC),
          margin: EdgeInsets.only(top: 20.pt),
          borderRadius: BorderRadius.circular(30.pt),
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Listener(
              onPointerDown: (e) =>
                  FocusScope.of(context).requestFocus(focusNode),
              child: TextFormField(
                keyboardType: isNum ? TextInputType.number : TextInputType.text,
                focusNode: focusNode,
                initialValue: initialValue,
                onChanged: onChanged,
                validator: validator,
                obscureText: isPassword && !store.showPassWord,
                obscuringCharacter: '*',
                decoration: InputDecoration(
                  hintText: hintText,
                  suffix: suffix,
                  fillColor: Colors.transparent,
                  // 去除input左右内边距
                  contentPadding:
                      EdgeInsets.fromLTRB(20.pt, 10.pt, 15.pt, 10.pt),
                  isDense: true,
                  hintStyle: TextStyle(
                    fontSize: 15.pt,
                    color: Color(0xff959595),
                    fontWeight: FontWeight.w300,
                  ),
                  labelStyle: TextStyle(
                    fontSize: 15.pt,
                    color: colorScheme.mainTitle,
                    fontWeight: FontWeight.w300,
                  ),
                ),
              ),
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    final style = WTheme.of(context);
    final colorScheme = style.colorScheme;
    final store = Provider.of<_Store>(context, listen: true);
    // final appGlobalStore = GetIt.I.get<AppGlobalStore>();


    return Scaffold(
      backgroundColor: Colors.white,
      resizeToAvoidBottomInset: false,
      body: WisScrollView(
        onInit: () => store.onInit(),
        slivers: <Widget>[
          SliverToBoxAdapter(
            child: SizedBox(
              /// 为小屏手机，大字号用户增加屏幕高度
              height: MediaQuery.of(context).size.height <= 667
                  ? 800
                  : MediaQuery.of(context).size.height,
              width: MediaQuery.of(context).size.width,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Stack(
                    children: [
                      Wisdom.row(children: [
                        Image.asset(
                          'lib/assets/login/bg.png',
                          package: 'app_package',
                          width: MediaQuery.of(context).size.width,
                        ),
                      ]),
                      Wisdom.column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        margin: EdgeInsets.only(
                          top: 140.pt,
                          left: 30.pt,
                        ),
                        children: [
                          Wisdom(
                            alignment: Alignment.centerLeft,
                            child: Image.asset(
                              'lib/assets/login/logo.png',
                              package: 'app_package',
                              width: 60.pt,
                            ),
                          ),
                          Wisdom(
                            margin: EdgeInsets.only(
                              top: 20.pt,
                            ),
                            child: WisText(
                              '欢迎登录绘服务',
                              size: 29.pt,
                              color: colorScheme.mainTitle,
                            ),
                          ),
                          Wisdom(
                            margin: EdgeInsets.only(top: 10.pt),
                            child: WisText(
                              '可使用企业号、账号密码登录',
                              color: colorScheme.grey,
                              size: 15.pt,
                            ),
                          ),
                          Form(
                            key: store.formKey,
                            child: Column(
                              children: <Widget>[
                                Wisdom.column(
                                  padding: EdgeInsets.fromLTRB(
                                      0, 10.pt, 30.pt, 30.pt),
                                  children: <Widget>[
                                    buildItem(
                                      title: '企业号',
                                      initialValue: store.entCode,
                                      onChanged: (v) => store.setEntCode(v),
                                      validator: (v) =>
                                          v == '' ? '企业号不可为空' : null,
                                      hintText: '请输入企业号',
                                      asset: 'lib/assets/login/qyh.png',
                                      focusNode: _entCodeFocus,
                                      // isNum: true,
                                    ),
                                    buildItem(
                                      title: '账号',
                                      initialValue: store.account,
                                      onChanged: (v) => store.setAccount(v),
                                      validator: (v) =>
                                          v == '' ? '账号不可为空' : null,
                                      hintText: '请输入账号',
                                      asset: 'lib/assets/login/zhh.png',
                                      focusNode: _accountFocus,
                                      // isNum: true,
                                    ),
                                    buildItem(
                                      title: '密码',
                                      initialValue: store.password,
                                      onChanged: (v) => store.setPassword(v),
                                      validator: (v) =>
                                          v == '' ? '密码不可为空' : null,
                                      hintText: '请输入密码',
                                      asset: 'lib/assets/login/icon_mima.png',
                                      isPassword: true,
                                      focusNode: _passwordFocus,
                                    ),
                                  ],
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                  Wisdom(
                    child: Wisdom.column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      padding: EdgeInsets.symmetric(horizontal: 30.pt),
                      children: [
                        Wisdom.row(
                          padding: EdgeInsets.only(left: 12.5.pt),
                          children: [
                            if (store.isCheck == true) ...[
                              Wisdom(
                                onTap: () => store.onCheck(),
                                child: Stack(
                                  children: [
                                    Wisdom(
                                      borderRadius:
                                          BorderRadius.circular(100.pt),
                                      border: Border.all(
                                          color: colorScheme.primary),
                                      height: 17.pt,
                                      width: 17.pt,
                                    ),
                                    Positioned(
                                      left: 3.5.pt,
                                      top: 3.5.pt,
                                      child: Wisdom(
                                          borderRadius:
                                              BorderRadius.circular(100.pt),
                                          height: 10.pt,
                                          width: 10.pt,
                                          color: colorScheme.primary),
                                    ),
                                  ],
                                ),
                              ),
                            ] else ...[
                              Wisdom(
                                onTap: () => store.onCheck(),
                                borderRadius: BorderRadius.circular(100.pt),
                                border: Border.all(
                                  color: colorScheme.grey,
                                ),
                                height: 17.pt,
                                width: 17.pt,
                              ),
                            ],
                            SizedBox(width: 10.pt),
                            Wisdom(
                              alignment: Alignment.centerLeft,
                              onTap: () async => await launchUrl(Uri.parse(
                                  'https://app.huiguanjia.cn/privacy.html')),
                              child: RichText(
                                text: TextSpan(children: [
                                  TextSpan(
                                    text: '我已阅读并同意',
                                    style: TextStyle(
                                      fontSize: 13.pt,
                                      color: colorScheme.minor,
                                    ),
                                  ),
                                  TextSpan(
                                    text: '《绘服务个人信息保护政策》',
                                    style: TextStyle(
                                      fontSize: 13.pt,
                                      color: colorScheme.mainTitle,
                                      fontWeight: FontWeight.bold,
                                    ),
                                  )
                                ]),
                              ),
                            ),
                          ],
                        ),
                        SizedBox(height: 30.pt),
                        Container(
                          decoration: BoxDecoration(
                            gradient: LinearGradient(
                              colors: [Color(0xFF4D80FF), Color(0xFF0F67F8)],
                              begin: Alignment.centerLeft,
                              end: Alignment.centerRight,
                            ),
                            borderRadius: BorderRadius.circular(40.pt),
                            boxShadow: [
                              BoxShadow(
                                color: Color(0x332F78FA),
                                blurRadius: 10.pt,
                                offset: Offset(0, 4),
                              )
                            ],
                          ),
                          child: WLoadingButton(
                            onPressed: () => store.login(),
                            textTheme: ButtonTextTheme.primary,
                            color: Colors.transparent,
                            disabledColor: Colors.transparent,
                            child: WisText(
                              '登录',
                              size: 18.pt,
                            ),
                          ),
                        ),
                        SizedBox(height: 30.pt),
                        Column(
                          children: <Widget>[
                            WisText.c999(
                              '绘服务 ${store.version}',
                              size: 10.pt,
                            ),
                            WisText.c999(
                                '2016-${DateTime.now().year}Ⓒ海南智慧城科技有限公司',
                                size: 10.pt),
                            WisText.c999('琼ICP备18001846号-4A', size: 10.pt),
                          ],
                        ),
                      ],
                    ),
                  ).asExpanded(),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class _CenterWebView extends StatefulWidget {
  _CenterWebView({Key? key, this.onAgree, this.onNotAgree}) : super(key: key);
  final Function()? onAgree;
  final Function()? onNotAgree;
  @override
  __CenterWebViewState createState() => __CenterWebViewState();
}

class __CenterWebViewState extends State<_CenterWebView> {
  bool loading = false;
  // WebViewController controller = WebViewController();
  void initState() {
    // setState(() {
    //   loading = true;
    // });
    // controller.loadRequest(Uri.parse('https://app.huiguanjia.cn/privacy.html'))
    //   ..whenComplete(() {
    //     setState(() {
    //       loading = false;
    //     });
    //   });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    final colorScheme = WTheme.of(context).colorScheme;
    return Center(
      child: Wisdom.column(
        padding: EdgeInsets.only(top: 30.pt, bottom: 20.pt),
        mainAxisSize: MainAxisSize.min,
        // height: MediaQuery.of(context).size.height * .42,
        width: MediaQuery.of(context).size.width * .85,
        borderRadius: BorderRadius.circular(12.pt),
        color: Colors.white,
        children: [
          Wisdom(
            padding: EdgeInsets.only(bottom: 30.pt),
            alignment: Alignment.center,
            child: WisText(
              '用户协议与隐私政策',
              size: 16.pt,
              weight: FontWeight.w600,
              color: colorScheme.mainTitle,
            ),
          ),
          Wisdom.column(
            padding: EdgeInsets.symmetric(horizontal: 15.pt),
            children: [
              Row(
                children: [
                  WisText('(1) 《隐私政策》中关于个人设备用户信息的收集和使用说明》。')
                      .asFlexible(flex: 1),
                ],
              ),
              SizedBox(
                height: 7.pt,
              ),
              Row(
                children: [
                  WisText('(2)《隐私政策》中与第三方SDK类服务商数据共享、相关信息收集和使用说明。')
                      .asFlexible(flex: 1),
                ],
              ),
              SizedBox(
                height: 7.pt,
              ),
              Row(
                children: [
                  WisText('(3)允许应用获取SN信息，通过消息推送，用于信息通知。').asFlexible(flex: 1),
                ],
              ),
              SizedBox(
                height: 7.pt,
              ),
              Row(
                children: [
                  WisText('用户协议和隐私政策说明：').asFlexible(flex: 1),
                ],
              ),
              SizedBox(
                height: 20.pt,
              ),
              Wisdom.wrap(
                alignment: Alignment.centerLeft,
                children: [
                  WisText(
                    '阅读完整的',
                    color: colorScheme.grey,
                  ),
                  Wisdom(
                    onTap: () async => await launchUrl(
                        Uri.parse('https://app.huiguanjia.cn/privacy.html')),
                    child: WisText(
                      '《绘服务个人信息保护政策》',
                      color: colorScheme.primary,
                    ),
                  ),
                  WisText(
                    '了解',
                    color: colorScheme.grey,
                  ),
                  WisText(
                    '更多详细内容',
                    color: colorScheme.grey,
                  ),
                ],
              )
            ],
          ),
          Wisdom.row(
            padding: EdgeInsets.only(top: 20.pt),
            children: [
              Wisdom(
                onTap: widget.onNotAgree,
                color: Color(0xffF1F1F3),
                height: 44.pt,
                borderRadius: BorderRadius.circular(20.pt),
                margin: EdgeInsets.only(
                  left: 20.pt,
                  right: 10.pt,
                ),
                alignment: Alignment.center,
                child: WisText(
                  '不同意',
                  size: 16.pt,
                  color: colorScheme.mainTitle,
                ),
              ).asFlexible(flex: 1),
              Wisdom(
                onTap: widget.onAgree,
                height: 44.pt,
                color: colorScheme.primary,
                borderRadius: BorderRadius.circular(20.pt),
                margin: EdgeInsets.only(right: 20.pt, left: 10.pt),
                alignment: Alignment.center,
                child: WisText(
                  '同意',
                  size: 16.pt,
                  color: Colors.white,
                ),
              ).asFlexible(flex: 1),
            ],
          ),
        ],
      ),
    );
  }
}
