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

class DropdownSelection extends StatefulWidget {
  final List<AnnotatedClass>? involveClasses;
  final AnnotatedClass? classData;
  final void Function(AnnotatedClass) onClassSelected;
  final Color? bgColor;

  const DropdownSelection(
      {super.key, required this.involveClasses, required this.classData, required this.onClassSelected, this.bgColor});

  @override
  State<DropdownSelection> createState() => _DropdownSelectionState();
}

class _DropdownSelectionState extends State<DropdownSelection> {
  @override
  Widget build(BuildContext context) {
    return Container(
      // width: 200.r,
      padding: EdgeInsets.symmetric(horizontal: 10.r),
      decoration: BoxDecoration(
          color: widget.bgColor ?? const Color(0xFFF5F5F5),
          borderRadius: const BorderRadius.vertical(
            top: Radius.elliptical(10, 10),
            bottom: Radius.elliptical(10, 10),
          )),
      child: DropdownButton(
        value: widget.classData?.classId ?? '-1',
        style: TextStyle(color: const Color.fromRGBO(89, 89, 89, 1), fontSize: 12.sp),
        underline: Container(),
        // isExpanded:true,
        items: widget.involveClasses?.map((classItem) {
          return DropdownMenuItem<String>(
            value: classItem.classId,
            child: quickText(
                classItem.classId == '-1' ? '全部' : ' ${EnumUtils.formatGrade(classItem.grade)}${classItem.className}',
                size: 12.sp,
                color: Colors.black),
          );
        }).toList(),
        onChanged: (String? value) {
          if (value == null || widget.involveClasses == null) return;

          try {
            final selectedClass = widget.involveClasses!.firstWhere((element) => element.classId == value);
            widget.onClassSelected(selectedClass);
          } catch (e) {
            // 如果没有找到匹配的班级，不执行任何操作
          }
        },
      ),
    );
  }
}
