import 'package:flutter/material.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';

import '../../http/ApiService.dart';

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

  @override
  State<UserinfoPage> createState() => _UserinfoPageState();
}

class _UserinfoPageState extends State<UserinfoPage> {

  Map<String, dynamic> user = {};

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

    _getUserInfo();
  }

  Future<void> _getUserInfo() async {
    try {

      final result = await ApiService.getUserInfo();
      if (result['result'] == 'success') {
        setState(() {
           user= result['pd'];
        });

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppbar(title: "人员信息"),
      body: SafeArea(
        child: ListView(
          children: [
            _userItemCell("姓名", user["NAME"]??"", false),
            Divider(height: 1),
            _userItemCell("性别", user["SEX_NAME"]??"", false),
            Divider(height: 1),
            _userItemCell("部门", user["DEPARTMENT_NAME"]??"", false),
            Divider(height: 1),
            _userItemCell("岗位(工种)", user["POST_NAME"]??"", true),
            Divider(height: 1),
            _userItemCell("人员类型", user["PERSONNEL_TYPE_NAME"]??"", false),
            Divider(height: 1),
            _userItemCell("入职时间", user["ENTRY_DATE"]??"", false),
            Divider(height: 1),
            // _userItemCell("工种", user["TYPE_OF_WORK_NAME"]??"", false),
          ],
        ),
      ),
    );
  }

  Widget _userItemCell(final String title, final String detail, bool isLast) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 15),
      decoration:
          isLast
              ? const BoxDecoration(
                color: Colors.white,
                border: Border(
                  bottom: BorderSide(color: Colors.grey, width: 0),
                ),
              )
              : const BoxDecoration(
                color: Colors.white,

                border: Border(
                  bottom: BorderSide(color: Colors.grey, width: 0.5),
                ),
              ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(title, style: const TextStyle(fontSize: 16, )),
          Flexible(
            child: Text(
              detail,
              style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500,color: Colors.grey),
              textAlign: TextAlign.right,
              maxLines: 2,
              overflow: TextOverflow.ellipsis,
            ),
          ),
        ],
      ),
    );
  }

  void _showMessage(String msg) {
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(msg)));
  }

}
