import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/job/homework_details.dart';
import 'package:making_school_asignment_app/page/global_widget/MyEmptyWidget.dart';
import 'package:making_school_asignment_app/page/home_page/children/quick_data_check/widget/quick_student_data_table.dart';

class QuickDataCheckBottom extends StatefulWidget {
  final String jobId;
  final int kgCount;
  final int zgCount;
  final int subject;
  final List<Students>? jobData;

  const QuickDataCheckBottom({Key? key, required this.jobId, required this.jobData, required this.kgCount, required this.zgCount,required this.subject}) : super(key: key);

  @override
  State<QuickDataCheckBottom> createState() => _QuickDataCheckBottomState();
}

class _QuickDataCheckBottomState extends State<QuickDataCheckBottom> {
  RxList<Students> showList = RxList();
  RxList<Students> followList = RxList();
  RxBool sortType = true.obs;
  RxBool sortLevel = false.obs;
  RxBool sortCompleteRate = false.obs;

  @override
  void initState() {
    super.initState();
    showList.value = [...widget.jobData!];
    for (var e in widget.jobData!) {
      if (e.priorityAnnotate!) {
        followList.add(e);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 10.r, horizontal: 10.r),
      margin: EdgeInsets.symmetric(vertical: 10.r, horizontal: 14.r),
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(6.r))),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              InkWell(
                onTap: () {
                  if (widget.jobData == null) return;
                  showList.value = [...widget.jobData!];
                  if (!sortType.value) {
                    showList.sort((a, b) {
                      return b.state.compareTo(a.state);
                    });

                  } else {
                    showList.sort((a, b) {
                      return a.state.compareTo(b.state);
                    });
                  }

                  sortType.value = !sortType.value;
                  sortLevel.value = false;
                  sortCompleteRate.value = false;
                },
                child: Obx(() {
                  return Row(
                    children: [
                      if (!sortType.value)
                        Image.asset(
                          'assets/images/no_check_icon.png',
                          width: 16.r,
                          height: 16.r,
                        ),
                      if (sortType.value)
                        Image.asset(
                          'assets/images/check_icon.png',
                          width: 16.r,
                          height: 16.r,
                        ),
                      SizedBox(
                        width: 5.r,
                      ),
                      Text(
                        sortType.value ? '已提交排序' : '未提交排序',
                        style: TextStyle(fontSize: 12.sp, color: Color(0xFF707070)),
                      ),
                    ],
                  );
                }),
              ),
              SizedBox(
                width: 20.r,
              ),
              InkWell(
                onTap: () {
                  if (widget.jobData!.isEmpty) return;
                  if (!sortLevel.value) {
                    showList.value = followList.value;
                    sortType.value = false;
                  } else {
                    showList.value = [...widget.jobData!];
                    showList.sort((a, b) {
                      return b.state.compareTo(a.state);
                    });

                    sortType.value = true;
                  }
                  sortCompleteRate.value = false;
                  sortLevel.value = !sortLevel.value;
                },
                child: Obx(() {
                  return Row(
                    children: [
                      if (!sortLevel.value)
                        Image.asset(
                          'assets/images/no_check_icon.png',
                          width: 16.r,
                          height: 16.r,
                        ),
                      if (sortLevel.value)
                        Image.asset(
                          'assets/images/check_icon.png',
                          width: 16.r,
                          height: 16.r,
                        ),
                      SizedBox(
                        width: 5.r,
                      ),
                      Text(
                        '看关注学生',
                        style: TextStyle(fontSize: 12.sp, color: const Color(0xFF707070)),
                      ),
                    ],
                  );
                }),
              ),
              SizedBox(
                width: 20.r,
              ),
              InkWell(
                onTap: () {
                  if (widget.jobData!.isEmpty) return;
                  showList.value = [...widget.jobData!];
                  if (!sortCompleteRate.value) {
                    showList.sort((a, b) {
                      return b.answerRate!.toInt().compareTo(a.answerRate!.toInt());
                    });

                    sortType.value = false;
                  } else {
                    showList.value = [...widget.jobData!];
                    showList.sort((a, b) {
                      return b.state.compareTo(a.state);
                    });

                    sortType.value = true;
                  }
                  sortLevel.value = false;
                  sortCompleteRate.value = !sortCompleteRate.value;
                },
                child: Obx(() {
                  return Row(
                    children: [
                      if (!sortCompleteRate.value)
                        Image.asset(
                          'assets/images/no_check_icon.png',
                          width: 16.r,
                          height: 16.r,
                        ),
                      if (sortCompleteRate.value)
                        Image.asset(
                          'assets/images/check_icon.png',
                          width: 16.r,
                          height: 16.r,
                        ),
                      SizedBox(
                        width: 5.r,
                      ),
                      Text(
                        '完成率排序',
                        style: TextStyle(fontSize: 12.sp, color: const Color(0xFF707070)),
                      ),
                    ],
                  );
                }),
              ),
            ],
          ),
          SizedBox(
            height: 10.r,
          ),
          Row(
            children: [
              Text(
                '注：',
                style: TextStyle(fontSize: 8.sp, color: const Color(0xFF717171)),
              ),
              Container(
                width: 10.r,
                height: 10.r,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(5.r)),
                  color: const Color(0xFF4CC793),
                ),
              ),
              SizedBox(
                width: 2.r,
              ),
              Text(
                '正确',
                style: TextStyle(fontSize: 8.sp, color: Color(0xFF717171)),
              ),
              SizedBox(
                width: 15.r,
              ),
              Container(
                width: 10.r,
                height: 10.r,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(5.r)),
                  color: const Color(0xFFFF7474),
                ),
              ),
              SizedBox(
                width: 2.r,
              ),
              Text(
                '错误',
                style: TextStyle(fontSize: 8.sp, color: const Color(0xFF717171)),
              ),
              SizedBox(
                width: 15.r,
              ),
              Container(
                width: 10.r,
                height: 10.r,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(5.r)),
                  color: Colors.white,
                  // border: Border.all(width: 1.r,color: Colors.grey),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey,
                      offset: Offset(1.w, 1.h), //阴影y轴偏移量
                      blurRadius: 4, //阴影模糊程度
                      spreadRadius: 0.1, //阴影扩散程度
                    )
                  ],
                ),
              ),
              SizedBox(
                width: 2.r,
              ),
              Text(
                '已作答未批阅',
                style: TextStyle(fontSize: 8.sp, color: const Color(0xFF717171)),
              ),
              SizedBox(
                width: 15.r,
              ),
              Container(
                width: 10.r,
                height: 10.r,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(5.r)),
                  color: const Color(0xFFD3D3D3),
                ),
              ),
              SizedBox(
                width: 2.r,
              ),
              Text(
                '未做',
                style: TextStyle(fontSize: 8.sp, color: const Color(0xFF717171)),
              ),
            ],
          ),
          SizedBox(
            height: 10.r,
          ),
          Obx(() {
            return Column(
              children: [
                SizedBox(
                  height: showList.value.length > 5 ? 350.r : showList.value.length * 50.r + 60.r,
                  child: QuickStudentDataTable(
                    headList: ['学生姓名', '总用时','客观题', '主观题', '客观题状态', '主观题状态', '未批阅'],
                    bodyList: showList.value,
                    jobId: widget.jobId,
                    fixedRows: 1,
                    fixedCols: 0,
                    // hasUnrated: widget.jobData!.hasUnrated,
                    hasUnrated: false,
                    kgCount: widget.kgCount,
                    zgCount: widget.zgCount,
                    subject:widget.subject,
                  ),
                ),
                if(showList.isEmpty)
                  MyEmptyWidget(imgWidth: 100.r,imgHeight: 100.r,font: 10.sp,),
              ],
            );
          }),

        ],
      ),
    );
  }
}
