import 'package:flutter/material.dart';
import 'package:qhd_prevention/customWidget/toast_util.dart';
import 'package:qhd_prevention/http/ApiService.dart';
import 'package:qhd_prevention/pages/mine/mine_feedback_page.dart';
import 'package:qhd_prevention/pages/mine/webViewPage.dart';
import 'package:qhd_prevention/pages/mine/mine_about_page.dart';
import 'package:qhd_prevention/pages/mine/mine_set_page.dart';
import 'package:qhd_prevention/tools/tools.dart';

import 'mine_departure_List.dart';

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

  @override
  State<MinePage> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> {

  String name="";
  String phone="";
  String headPath="assets/images/avatar.png";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();


    name=SessionService.instance.username!;
    phone=SessionService.instance.loginUser?["USERNAME"]??""!;
    _getUserPhoto(phone);
  }

  Future<void> _getUserPhoto(String phone )async {
    try {
      final raw = await ApiService.getUserHeadPhoto(phone);
      // final hidCount = raw['hidCount'] as Map<String, dynamic>;
      // print(hidCount);
      if (raw['result'] == 'success') {
        headPath=raw['userPhoto'];
        setState(() {
          if("assets/images/user/avatar-2.jpg"==headPath){
            headPath="assets/images/avatar.png";
          }else{
            headPath=ApiService.baseImgPath +headPath;
          }
        });

      }

    } catch (e) {
      // 出错时可以 Toast 或者在页面上显示错误状态
      print('加载头像数据失败：$e');

    }
  }


  Future<bool> _getIsRest( )async {
    try {
      final raw = await ApiService.getIsRest();
      if (raw['result'] == 'success') {
        if(  raw['ISREST']=="1"){
          return true;
        }else{
          return false;
        }
      }else{
        return false;
      }
    } catch (e) {
      // 出错时可以 Toast 或者在页面上显示错误状态
      print('加载头像数据失败：$e');
      return false;
    }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(children: [
        Container(
          height: 120,
          color: Colors.white,
          padding: EdgeInsets.all(0),
          child: Padding(
            padding: EdgeInsets.only(left: 30),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              spacing: 10,
              children: [

                ClipRRect(
                  borderRadius: BorderRadius.circular(30),
                  child: Image.asset(
                    headPath,
                    height: 60,
                    width: 60,
                  ),
                ),
                SizedBox(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    spacing: 10,
                    children: [
                      Text(name, style: TextStyle(fontSize: 17, fontWeight: FontWeight.bold),),
                      Text("手机号:$phone", style: TextStyle(color: Colors.black54),)],
                  ),
                ),
              ],
            ),
          ),
        ),

        SizedBox(height: 10,),
        GestureDetector(
          child: _setItemWidget("离岗"),
          onTap: () async {

            bool isRest= await _getIsRest();
            if(isRest){
              ToastUtil.showNormal(context, "您已经处于离岗状态中");
              return;
            }
            pushPage(MineDepartureListPage(), context);
          },
        ),
        Divider(height: 1,color: Colors.black12,),
        GestureDetector(
          child: _setItemWidget("设置"),
          onTap: () {
            pushPage(MineSetPage(), context);
          },
        ),

        Divider(height: 1,color: Colors.black12,),
        GestureDetector(
          child: _setItemWidget("问题反馈"),
          onTap: () {
            pushPage(FeedbackPage(), context);
          },
        ),

        Divider(height: 1,color: Colors.black12,),
        GestureDetector(
          child: _setItemWidget("用户服务协议"),
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => const WebViewPage(
                  name: "用户服务协议",
                  url: 'http://47.92.102.56:7811/file/xieyi/zsyhxy.htm', // 替换为目标网址
                ),
              ),
            );
          },
        ),

        Divider(height: 1,color: Colors.black12,),
        GestureDetector(
          child: _setItemWidget("用户隐私协议"),
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => const WebViewPage(
                  name: "用户隐私协议",
                  url: 'http://47.92.102.56:7811/file/xieyi/zsysq.htm', // 替换为目标网址
                ),
              ),
            );
          },
        ),

        Divider(height: 1,color: Colors.black12,),
        GestureDetector(
          child: _setItemWidget("帮助中心"),
          onTap: () {
            Navigator.push(
              context,
              MaterialPageRoute(
                builder: (context) => const WebViewPage(
                  name: "帮助中心",
                  url: 'http://47.92.102.56:7811/file/help/help/', // 替换为目标网址
                ),
              ),
            );
          },
        ),

        Divider(height: 1,color: Colors.black12,),
        GestureDetector(
          child: _setItemWidget("关于"),
          onTap: () {
            pushPage(MineAboutPage(), context);

          },
        ),

      ],),
    );
  }
  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)
          ],
        ),
      ),
    );
  }

}

