import 'package:flutter/material.dart';
import 'package:qhd_prevention/customWidget/custom_alert_dialog.dart';
import 'package:qhd_prevention/customWidget/toast_util.dart';
import 'package:qhd_prevention/pages/home/study/face_ecognition_page.dart';
import 'package:qhd_prevention/pages/login_page.dart';
import 'package:qhd_prevention/pages/mine/mine_first_sign_page.dart';
import 'package:qhd_prevention/pages/mine/mine_set_pwd_page.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';
import 'package:qhd_prevention/services/auth_service.dart';
import 'package:qhd_prevention/tools/h_colors.dart';

import '../../tools/tools.dart';

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

  @override
  State<MineSetPage> createState() => _MineSetPageState();
}

class _MineSetPageState extends State<MineSetPage> {

  /// 检查更新
  Future<void> _checkUpdate() async {
    LoadingDialogHelper.show();
    final result = await AuthService.checkUpdate();
    LoadingDialogHelper.hide();
    if (FormUtils.hasValue(result, 'pd')) {
      // 有更新 提示更新
      Map pd = result['pd'];
      CustomAlertDialog.showConfirm(
          context,
          title: '更新通知',
          cancelText: '',
          confirmText: '我知道了',
          content: pd['UPLOAD_CONTENT'] ?? '',
          onConfirm: () {
            ToastUtil.showNormal(context, '更新去吧！');
          }
      );
    }else{
      ToastUtil.showNormal(context, '已经是最新版本！');

    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppbar(title: "设置"),
      backgroundColor: h_backGroundColor(),
      body: Column(
        children: [
          GestureDetector(
            child: _setItemWidget("修改密码"),
            onTap: () {
              pushPage(const MineSetPwdPage(), context);
            },
          ),
          const Divider(height: 1, color: Colors.black12),

          GestureDetector(
            child: _setItemWidget("更新人脸信息"),
            onTap: () {
              pushPage(
                const FaceRecognitionPage(studentId: '', mode: FaceMode.manual),
                context,
              );
            },
          ),
          const Divider(height: 1, color: Colors.black12),

          GestureDetector(
            child: _setItemWidget("更新签字信息"),
            onTap: () {
              pushPage(const FirstSignPage(), context);
              // pushPage(MineSignPage(), context);
            },
          ),
          const Divider(height: 1, color: Colors.black12),

          GestureDetector(
            child: _setItemWidget("检查更新"),
            onTap: () {
              _checkUpdate();
            },
          ),

          const SizedBox(height: 15),

          GestureDetector(
            child: Container(
              padding: const EdgeInsets.symmetric(vertical: 15),
              color: Colors.white,
              child: const Center(
                child: Text("退出当前账户", style: TextStyle(fontSize: 16)),
              ),
            ),
            onTap: () async {
              CustomAlertDialog.showConfirm(
                context,
                title: '确认退出',
                content: '确定要退出当前账号吗',
                onConfirm: () async {
                  await AuthService.logout(); // ✅ 等待登出完成
                  if (!mounted) return;
                  Navigator.pushAndRemoveUntil(
                    context,
                    MaterialPageRoute(builder: (context) => const LoginPage()),
                        (Route<dynamic> route) => false,
                  );
                },
              );
            },
          ),
        ],
      ),
    );
  }

  Widget _setItemWidget(final String text) {
    return Container(
      height: 55,
      color: Colors.white,
      child: Padding(
        padding: const EdgeInsets.all(15),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(text, style: const TextStyle(fontSize: 16)),
            const Icon(Icons.chevron_right),
          ],
        ),
      ),
    );
  }
}
