import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:making_school_asignment_app/common/job/homework_details.dart';
import 'package:making_school_asignment_app/page/global_widget/my_text.dart';

class PersonnelDataOverview extends StatefulWidget {
  final List<Students> studentList;
  const PersonnelDataOverview({super.key, required this.studentList});

  @override
  State<PersonnelDataOverview> createState() => _PersonnelDataOverviewState();
}

class _PersonnelDataOverviewState extends State<PersonnelDataOverview> {
  List<String> names = [];
  List<String> useTimes = [];
  List<String> correctRates = [];
  List<String> noAnswerCounts = [];
  List<String> rankings = [];
  Map<String, List<dynamic>> mapData = {};
  final scrollController = ScrollController();

  @override
  void initState() {
    super.initState();

    for (var i = 0; i < widget.studentList.length; i++) {
      var student = widget.studentList[i];
    /*TimeUnits timeUnits = convertMilliseconds(student.useTime!);
      String timeerStr = '';
      if (timeUnits.hours > 0) timeerStr = '${timeUnits.hours}:';
      if (timeUnits.minutes > 0) timeerStr += '${timeUnits.minutes}:';
      timeerStr += timeUnits.seconds.toString();*/

      names.add(student.studentName);
      // useTimes.add(timeerStr);
      useTimes.add(student.ttlSec!);

      correctRates.add('${student.okRate!.toStringAsFixed(0)}%');
      noAnswerCounts.add(student.noAnswerCount.toString());
      rankings.add('${i + 1}名');


    }
    mapData = {
      '姓名': names,
      '答题时长': useTimes,
      '正确率': correctRates,
      '未答题数': noAnswerCounts,
      '班级排名': rankings,
    };
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      // height: isPad() ? 290.h : 264.h,
      margin: EdgeInsets.only(top: 20.h),
      padding: EdgeInsets.symmetric(vertical: 16.h, horizontal: 12.w),
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10.r)),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            margin: EdgeInsets.only(bottom: 20.h),
            child: quickText('人员数据概况', color: const Color.fromRGBO(92, 92, 92, 1), size: 14.sp, fontWeight: FontWeight.bold),
          ),
          Scrollbar(
            thickness: 8.w,
            thumbVisibility: true,
            // trackVisibility: true,
            radius: Radius.circular(10.r),
            controller: scrollController, //滑动条使用的控制器
            child: SingleChildScrollView(
              controller: scrollController,
              scrollDirection: Axis.horizontal,
              // primary: true,
              // physics: const BouncingScrollPhysics(),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  ...mapData.entries.map((entrie) {
                    bool isTransparentChinese = ['答题时长', '正确率', '未答题数'].contains(entrie.key); // 透明中文

                    return Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          width: 100.r,
                          height: 40.h,
                          alignment: Alignment.center,
                          color: const Color.fromRGBO(230, 230, 230, 1),
                          margin: EdgeInsets.only(bottom: 1.h, right: 1.w),
                          // padding: EdgeInsets.symmetric(vertical: 10.h, horizontal: 8.w),
                          child: quickText(entrie.key, color: const Color.fromRGBO(24, 35, 77, 1), size: 12.sp, maxLines: 2),
                        ),
                        ...entrie.value.map((e) {
                          bool isTransparentChineseNew = isTransparentChinese && (e?.length ?? 0) == 0;
                          return Container(
                            width: 100.r,
                            height: 40.h,
                            alignment: Alignment.center,
                            margin: EdgeInsets.only(bottom: 1.h, right: 1.w),
                            // padding: EdgeInsets.symmetric(vertical: 10.h, horizontal: 8.w),
                            color: const Color.fromRGBO(245, 245, 245, 1),
                            child: isTransparentChineseNew
                                ? quickText('透明', color: Colors.transparent, size: 12.sp)
                                : RegExp(r'^\d+$').hasMatch(e) || e.contains(' %')
                                    ? Row(
                                        mainAxisSize: MainAxisSize.min,
                                        children: [
                                          quickText(e, color: const Color.fromRGBO(82, 82, 82, 1), size: 12.sp, maxLines: 2),
                                          quickText('透', color: Colors.transparent, size: 12.sp),
                                        ],
                                      )
                                    : quickText(e, color: const Color.fromRGBO(82, 82, 82, 1), size: 12.sp, maxLines: 2),
                          );
                        }),
                      ],
                    );
                  }),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

// 定义一个类来保存转换后的时间单位
class TimeUnits {
  int hours;
  int minutes;
  int seconds;

  TimeUnits(this.hours, this.minutes, this.seconds);
}

// 毫秒转小时、分钟、秒的函数
TimeUnits convertMilliseconds(int totalSeconds) {
  int hours = totalSeconds ~/ 3600; // 整除得到小时数
  int remainingSeconds = totalSeconds % 3600; // 求余得到剩余的秒数
  int minutes = remainingSeconds ~/ 60; // 整除得到分钟数
  int seconds = remainingSeconds % 60; // 求余得到秒数

  return TimeUnits(hours, minutes, seconds);
}
