import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/job/user_info_detail.dart';
import 'package:making_school_asignment_app/common/store/app_storage_key.dart';
import 'package:making_school_asignment_app/common/store/user_store.dart';
import 'package:making_school_asignment_app/common/utils/storage.dart';
import 'package:making_school_asignment_app/page/global_widget/my_text.dart';
import 'package:making_school_asignment_app/routes/app_pages.dart';

class MyInfo extends StatefulWidget {
  const MyInfo({super.key});

  @override
  State<MyInfo> createState() => _MyInfoState();
}

class _MyInfoState extends State<MyInfo> with AutomaticKeepAliveClientMixin {
  late Rx<UserInfoDetail?> userInfo = UserStore.to.userDetailInfo;

  @override
  void initState() {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarIconBrightness: Brightness.light,
      statusBarColor: Colors.transparent, //状态栏背景颜色
      // systemStatusBarContrastEnforced: false,
    ));
    super.initState();
  }

  // 确认对话框
  _showAlertDialog(context1) async {
    await showDialog(
        // 表示点击灰色背景的时候是否消失弹出框
        barrierDismissible: false,
        context: context1,
        builder: (context) {
          return AlertDialog(title: quickText("提示信息"), content: quickText("您确定要退出登录吗?"), actions: <Widget>[
            TextButton(
              child: quickText("取消"),
              onPressed: () {
                Navigator.pop(context, 'Cancle');
              },
            ),
            TextButton(
                child: quickText("确定"),
                onPressed: () async {
                  try {
                    var pwd = StorageService.to.read(AppStorageKey.pwd.value);
                    var account = StorageService.to.read(AppStorageKey.account.value);

                    UserStore.to.erase();
                    await StorageService.to.erase();
                    StorageService.to.write(AppStorageKey.privacyAgreement.value, true);
                    StorageService.to.write(AppStorageKey.account.value, account);
                    StorageService.to.write(AppStorageKey.pwd.value, pwd);
                    Navigator.pop(context, "Ok");
                    Get.offAllNamed(Routes.login);
                  } catch (e) {
                    print(e);
                  }
                })
          ]);
        });
  }

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);

    final personalInfoTitleStly = TextStyle(
      color: const Color.fromRGBO(80, 87, 103, 1),
      fontSize: 13.sp,
    );
    final personalInfoValStly = TextStyle(
      color: const Color.fromRGBO(148, 163, 182, 1),
      fontSize: 13.sp,
    );

    return Stack(
      children: [
        SizedBox(
          height: double.infinity,
          child: Column(
            children: [
              Container(
                height: 220.h,
                width: double.infinity,
                decoration: const BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('assets/images/personal_bg.png'),
                    fit: BoxFit.cover,
                  ),
                ),
              ),
              Expanded(
                  child: Container(
                color: const Color.fromRGBO(248, 248, 248, 1),
              ))
            ],
          ),
        ),
        SafeArea(
          child: Scaffold(
            backgroundColor: Colors.transparent,
            body: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  height: 150.h,
                  padding: EdgeInsets.only(left: 20.r, right: 20.r),
                  // alignment: Alignment.center,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Image.asset(
                        'assets/images/default_user_dead.png',
                      ),
                      SizedBox(
                        width: 5.w,
                      ),
                      InkWell(
                        onTap: () {
                          /*if (tokenState == '' || userState.id == '') {
                                    toLoginPage(context);
                                  }*/
                        },
                        child: Container(
                          margin: EdgeInsets.only(top: 0.h),
                          child: Text(
                            userInfo.value?.name ?? '请前往登录',
                            style:
                                TextStyle(fontSize: 16.sp, color: const Color(0xFF332A2A), fontWeight: FontWeight.w500),
                          ),
                        ),
                      ),
                      const Spacer(),
                      InkWell(
                        onTap: () {
                          _showAlertDialog(context);
                        },
                        child: Container(
                          padding: EdgeInsets.all(2.r),
                          decoration: BoxDecoration(
                            color: const Color.fromRGBO(255, 255, 255, 1),
                            borderRadius: BorderRadius.circular(50.r),
                          ),
                          child: Image.asset('assets/images/out_icon.png', fit: BoxFit.cover),
                        ),
                      )
                    ],
                  ),
                ),
                SizedBox(height: 14.h),
                Container(
                  margin: EdgeInsets.symmetric(vertical: 5.h, horizontal: 16.w),
                  padding: EdgeInsets.symmetric(vertical: 15.h, horizontal: 16.w),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(6.w)),
                    color: Colors.white,
                    boxShadow: const [
                      BoxShadow(
                        color: Color.fromRGBO(46, 91, 255, 0.1),
                        offset: Offset.zero, //阴影y轴偏移量
                        blurRadius: 20, //阴影模糊程度
                        spreadRadius: 10, //阴影扩散程度
                      )
                    ],
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text('账号', style: personalInfoTitleStly),
                      Text(userInfo.value?.name ?? '请前往登录', style: personalInfoValStly)
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.symmetric(vertical: 5.h, horizontal: 16.w),
                  padding: EdgeInsets.symmetric(vertical: 15.h, horizontal: 16.w),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(6.w)),
                    color: Colors.white,
                    boxShadow: const [
                      BoxShadow(
                        color: Color.fromRGBO(46, 91, 255, 0.1),
                        offset: Offset.zero, //阴影y轴偏移量
                        blurRadius: 20, //阴影模糊程度
                        spreadRadius: 10, //阴影扩散程度
                      )
                    ],
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text('所在学校', style: personalInfoTitleStly),
                      Text(userInfo.value?.schoolName ?? '', style: personalInfoValStly)
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.symmetric(vertical: 5.h, horizontal: 16.w),
                  padding: EdgeInsets.symmetric(vertical: 15.h, horizontal: 16.w),
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.all(Radius.circular(6.w)),
                    color: Colors.white,
                    boxShadow: const [
                      BoxShadow(
                        color: Color.fromRGBO(46, 91, 255, 0.1),
                        offset: Offset.zero, //阴影y轴偏移量
                        blurRadius: 20, //阴影模糊程度
                        spreadRadius: 10, //阴影扩散程度
                      )
                    ],
                  ),
                  child: InkWell(
                    onTap: () {
                      Get.toNamed(Routes.otherPage);
                    },
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text('其他', style: personalInfoTitleStly),
                        Icon(
                          Icons.arrow_forward_ios,
                          color: const Color.fromRGBO(80, 87, 103, 1),
                          size: 13.sp,
                        )
                      ],
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
