import 'package:flutter/material.dart';
import 'package:qhd_prevention/pages/login_page.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';
import 'package:qhd_prevention/tools/h_colors.dart';
import 'package:shared_preferences/shared_preferences.dart';

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

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

  Future<void> _clearUserSession() async {
    final prefs = await SharedPreferences.getInstance();
    await prefs.remove('isLoggedIn');     // 清除登录状态
  }

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


          Divider(height: 1, color: Colors.black12),
          GestureDetector(child: _setItemWidget("检查更新"), onTap: () {}),

          SizedBox(height: 15),

         GestureDetector(
           child:  Container(
             padding: EdgeInsets.symmetric(vertical: 15),
             color: Colors.white,
             child: Center(child: Text("退出当前账户", style: TextStyle(fontSize: 16),)),
           ),
           onTap: () async {
             // 显示确认对话框
             bool? confirm = await showDialog(
               context: context,
               builder: (context) => AlertDialog(
                 title: Text("确认退出"),
                 content: Text("确定要退出当前账号吗？"),
                 actions: [
                   TextButton(
                     onPressed: () => Navigator.pop(context, false),
                     child: Text("取消"),
                   ),
                   TextButton(
                     onPressed: () => Navigator.pop(context, true),
                     child: Text("确定", style: TextStyle(color: Colors.red)),
                   ),
                 ],
               ),
             );

             if (confirm == true) {
               // 清除用户登录状态
               await _clearUserSession();

               // 跳转到登录页并清除所有历史路由
               Navigator.pushAndRemoveUntil(
                 context,
                 MaterialPageRoute(builder: (context) => LoginPage()),
                     (Route<dynamic> route) => false, // 移除所有历史路由
               );
             }
           },
         ),
        ],
      ),
    );
  }

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