/*
 * @Author: XianKaiQun
 * @Date: 2020-04-21 16:04:10
 * @LastEditors: wuwei
 * @LastEditTime: 2023-12-21 17:12:27
 * @Descripttion: 
 */
import 'package:get/get.dart';
import 'package:app_package/app_package.dart';
import 'package:app_package/src/api/auth.dart';
import 'package:app_package/src/assets.gen.dart';
import 'package:app_package/src/view_models/private_mine_store.dart';
import 'package:flutter/material.dart';
import 'package:flutter_mobx/flutter_mobx.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

import '../../stores/update.dart';
part 'components/user_card.dart';
part 'components/password.dart';

class MinePage extends StatelessWidget {
  const MinePage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (providerContext) => PrivateMineStore(context, providerContext),
      child: _PageView(),
      lazy: false,
    );
  }
}

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

class _PageViewState extends State<_PageView> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    PrivateMineStore store = Provider.of(context);

    final colorScheme = WTheme.of(context).colorScheme;
    // final communityGlobalStore = GetIt.I.get<CommunityGlobalStore>();
    final info = store.currentUserinfo;
    final entCode = info?.entCode;
    final entName = info?.entName ?? '';
    final companyName = info?.companyName ?? '';
    final entAndcompany = '$entName-$companyName';
    int count = store.unread() ?? 0;

    return Scaffold(
      appBar: WAppBar(
        brightness: Brightness.light,
        backgroundColor: Color(0xffDDEEFE),
        toolbarHeight: 0,
        automaticallyImplyLeading: false,
      ),
      body: Stack(children: [
        Image.asset(
          'lib/assets/main/bg.png',
          package: 'app_package',
          width: MediaQuery.of(context).size.width,
        ),
        Column(children: [
          WisScrollView(
            onInit: () => store.getData(),
            onRefresh: () => store.getData(),
            slivers: <Widget>[
              ///用户详情卡片
              _UserInfoCardComponent(),
              // Wisdom(
              //   borderRadius: BorderRadius.circular(8.pt),
              //   border: Border.all(
              //     color: Color(0xffA5CFFE),
              //   ),
              //   margin: EdgeInsets.only(left: 15.pt, right: 15.pt),
              //   child: Gr
              // ).asSliver(),
              GradientCard(
                margin: EdgeInsets.fromLTRB(15.pt, 0, 15.pt, 0),
                elevation: 0,
                gradient: LinearGradient(
                  colors: [Color(0xffEBF7FF), Color(0xffE9FAFD)],
                  begin: Alignment.centerLeft,
                  end: Alignment.centerRight,
                ),
                border: Border.all(
                  color: Color(0xffA5CFFE),
                ),
                child: Wisdom.row(padding: EdgeInsets.all(15.pt), children: [
                  Image.asset(
                    'lib/assets/main/icon_zxkf.png',
                    package: 'app_package',
                    width: 22.pt,
                  ),
                  SizedBox(width: 15.pt),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      WisText(
                        '$entAndcompany',
                        size: 14.pt,
                        weight: FontWeight.bold,
                        color: colorScheme.mainTitle,
                      ),
                      SizedBox(height: 5.pt),
                      WisText(
                        '企业号：$entCode',
                        size: 13.pt,
                        color: colorScheme.grey,
                      ),
                    ],
                  ).asFlexible(flex: 1),
                ]),
              ).asSliver(),
              Wisdom(
                margin: EdgeInsets.only(top: 10.pt, left: 15.pt, right: 15.pt),
                child: Row(
                  children: [
                    GradientCard(
                      child: Wisdom.row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        onTap: () => store.route2notice(),
                        padding: EdgeInsets.all(15.pt),
                        children: [
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              WisText(
                                '我的消息',
                                size: 16.pt,
                                bold: true,
                                color: Colors.white,
                              ),
                              WisText(
                                count == 0
                                    ? '全部已读'
                                    : '${count > 99 ? '99+' : count}条未读',
                                color: Colors.white,
                                size: 13.pt,
                              ),
                            ],
                          ),
                          Image.asset(
                            'lib/assets/main/icon_bsbxtz2.png',
                            package: 'app_package',
                            width: 43.pt,
                          ),
                        ],
                      ),
                      gradient: LinearGradient(
                        colors: [Color(0xff7DA2FF), Color(0xff0049FF)],
                        begin: Alignment.centerLeft,
                        end: Alignment.centerRight,
                      ),
                    ).asFlexible(flex: 1),
                    SizedBox(width: 10.pt),
                    GradientCard(
                      child: Wisdom.row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        onTap: () => store.route2task(),
                        padding: EdgeInsets.all(15.pt),
                        children: [
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              WisText(
                                '我的任务',
                                size: 16.pt,
                                bold: true,
                                color: Colors.white,
                              ),
                              if (store.taskMsg() != null) ...[
                                Builder(builder: (_) {
                                  var count = store.taskMsg();
                                  return WisText(
                                    count == 0
                                        ? '已全部完成'
                                        : '${count! > 99 ? '99+' : count}项待完成',
                                    color: Colors.white,
                                    size: 13.pt,
                                  );
                                }),
                              ] else ...[
                                WisText(
                                  '点击查看',
                                  color: Colors.white,
                                  size: 13.pt,
                                ),
                              ]
                            ],
                          ),
                          Image.asset(
                            'lib/assets/main/bj17.png',
                            package: 'app_package',
                            width: 43.pt,
                          ),
                        ],
                      ),
                      gradient: LinearGradient(
                        colors: [Color(0xff73C3FF), Color(0xff1E9BF9)],
                        begin: Alignment.centerLeft,
                        end: Alignment.centerRight,
                      ),
                    ).asFlexible(flex: 1),
                  ],
                ),
              ).asSliver(),

              // WisCard(
              //   elevation: 0,
              //   onTap: () => _PassWordComponent.show(context),
              //   isSliver: true,
              //   margin: EdgeInsets.only(top: 15.pt, left: 15.pt, right: 15.pt),
              //   child:
              // ),

              ///微信绑定卡片
              // _WechatBindComponent(),
              WisCard(
                elevation: 0,
                isSliver: true,
                margin: EdgeInsets.only(top: 15.pt, left: 15.pt, right: 15.pt),
                child: Column(
                  children: [
                    WisCell(
                      onTap: () => _PassWordComponent.show(context),
                      title: WisText(
                        '修改密码',
                        size: 14.pt,
                        color: colorScheme.mainTitle,
                      ),
                      leading: Image(
                        image: AssetList.$main_psw_png_image,
                        height: 22.pt,
                      ),
                      padding: EdgeInsets.symmetric(
                        horizontal: 15.pt,
                        vertical: 16.pt,
                      ),
                      trailing: Icon(Icons.chevron_right),
                    ),
                    Divider(indent: 15.pt, endIndent: 15.pt, height: 1.pt),
                    WisCell(
                      onTap: () => store.callService('400-600-7709'),
                      leading: Image(
                        image: AssetList.$main_service_png_image,
                        height: 22.pt,
                      ),
                      padding: EdgeInsets.symmetric(
                        horizontal: 15.pt,
                        vertical: 16.pt,
                      ),
                      title: WisText(
                        '官方客服',
                        size: 14.pt,
                        color: colorScheme.mainTitle,
                      ),
                      trailing: Row(
                        children: <Widget>[
                          Text(
                            '400-600-7709',
                            style: TextStyle(
                              color: colorScheme.primary,
                              fontSize: 12.pt,
                            ),
                          ),
                          Icon(Icons.chevron_right),
                        ],
                      ),
                    ),
                    Divider(indent: 15.pt, endIndent: 15.pt, height: 1.pt),
                    WisCell(
                      padding: EdgeInsets.symmetric(
                        horizontal: 15.pt,
                        vertical: 16.pt,
                      ),
                      onTap: () => UpdateAppStore.instance.checkUpdate(),
                      leading: Image(
                        image: AssetList.$main_update_png_image,
                        height: 22.pt,
                      ),
                      title: WisText(
                        '检查更新',
                        size: 14.pt,
                        color: colorScheme.mainTitle,
                      ),
                      trailing: Row(
                        children: <Widget>[
                          Icon(Icons.chevron_right),
                        ],
                      ),
                    ),
                    Divider(indent: 15.pt, endIndent: 15.pt, height: 1.pt),
                    WisCell(
                      padding: EdgeInsets.symmetric(
                        horizontal: 15.pt,
                        vertical: 16.pt,
                      ),
                      onTap: () => store.route2version(),
                      leading: Image(
                        image: AssetList.$main_about_png_image,
                        height: 22.pt,
                      ),
                      title: WisText(
                        '版本介绍',
                        size: 14.pt,
                        color: colorScheme.mainTitle,
                      ),
                      trailing: Row(
                        children: <Widget>[
                          Text(
                            '${store.version}',
                            style: TextStyle(
                              color: colorScheme.minor,
                              fontSize: 12.pt,
                            ),
                          ),
                          Icon(Icons.chevron_right),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              SliverToBoxAdapter(
                child: Container(
                  decoration: BoxDecoration(boxShadow: [
                    BoxShadow(
                      color: Color(0x08000000),
                      offset: Offset(0, 2.pt),
                      blurRadius: 10.pt,
                      spreadRadius: 2.5.pt,
                    ),
                  ]),
                  child: WLoadingButton(
                    onPressed: () => store.logout(),
                    margin: EdgeInsets.all(15.pt),
                    color: Colors.white,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(12.pt),
                    ),
                    disabledColor: colorScheme.disabled,
                    child: Text('退出登录'),
                  ),
                ),
              ),
              SizedBox(height: 20.pt).asSliver(),

              Wisdom.row(
                mainAxisAlignment: MainAxisAlignment.center,
                onTap: () async => await launchUrl(
                  Uri.parse('https://app.huiguanjia.cn/privacy.html'),
                ),
                children: [
                  WisText(
                    '《绘服务个人信息保护政策》',
                    size: 11.pt,
                    color: colorScheme.primary,
                    // decoration: TextDecoration.underline,
                  ),
                ],
              ).asSliver(),
              // SizedBox(height: 5.pt).asSliver(),

              SliverToBoxAdapter(
                child: Column(
                  children: <Widget>[
                    Observer(
                      builder: (_) {
                        return WisText.c999(
                          '绘服务 ${store.version}',
                          size: 10.pt,
                        );
                      },
                    ),
                    WisText.c999('琼ICP备18001846号-4A', size: 10.pt),
                    WisText.c999('Copyright Ⓒ 2016-${DateTime.now().year} 海南智慧城科技有限公司',
                        size: 10.pt),
                  ],
                ),
              ),

              SizedBox(height: 50.pt).asSliver(),
            ],
          ).asFlexible(flex: 1),
          Opacity(
            opacity: 1,
            child: Container(
              color: Colors.transparent,
              height: 50.pt,
            ),
          ),
        ]),
      ]),
    );
  }
}
