import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:functional_widget_annotation/functional_widget_annotation.dart';
import 'package:marking_app/common/model/job/job_report_model.dart';
import 'package:marking_app/routes/RouterManager.dart';
import 'package:marking_app/utils/easy_refresh/MyEmptyWidget.dart';
import 'package:marking_app/utils/index.dart';

part 'top_count.g.dart';

class TopCount extends StatelessWidget {
  final JobReportModel data;
  final String className;
  final int jobId;

  const TopCount(this.data, this.className, this.jobId, {Key? key}) : super(key: key);

  void showStudentListDialog({required BuildContext context, required String title, required List arr}) {
    showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            // insetPadding: EdgeInsets.symmetric(vertical: 20.r,horizontal: 20.r),
            contentPadding: EdgeInsets.all(20.r),
            shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(15.r))),
            content: SizedBox(
              width: MediaQuery.of(context).size.width * 0.7,
              height: MediaQuery.of(context).size.height * 0.7,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Center(
                    child: Text(
                      className + title,
                      style: TextStyle(fontSize: 15.sp, color: Color(0xFF3C3C3C), fontWeight: FontWeight.w500),
                    ),
                  ),
                  SizedBox(
                    height: 5.r,
                  ),
                  SizedBox(
                    height: 15.r,
                  ),
                  arr.length > 0
                      ? Expanded(
                          child: ListView.builder(
                            shrinkWrap: true,
                            itemBuilder: (context, index) {
                              AnswerOkStudents item = arr[index];
                              return InkWell(
                                onTap: () {
                                  RouterManager.router.navigateTo(
                                    context,
                                    RouterManager.quickCheckPersonalPath + '?jobId=$jobId&studentId=${item.id}',
                                    transition: getTransition(),
                                  );
                                },
                                child: Container(
                                  padding: EdgeInsets.symmetric(vertical: 5.r, horizontal: 15.r),
                                  color: index.isOdd ? Colors.white : Color(0xFFF0F0F0),
                                  child: Text(
                                    item.name,
                                    style: TextStyle(fontSize: 12.sp, color: Color(0xFF323232)),
                                  ),
                                ),
                              );
                            },
                            itemCount: arr.length,
                          ),
                        )
                      : MyEmptyWidget()
                ],
              ),
            ),
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    double leftWidth = (MediaQuery.of(context).size.width - 40.r) * 0.7 / 3;
    double rightWidth = (MediaQuery.of(context).size.width - 40.r) * 0.3 / 2;
    return Container(
      padding: EdgeInsets.symmetric(vertical: 10.r, horizontal: 10.r),
      margin: EdgeInsets.symmetric(vertical: 10.r, horizontal: 10.r),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(6.r)),
      ),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              InkWell(
                onTap: () {
                  showStudentListDialog(context: context, title: '未提交作业学生', arr: data.noAnswerStudents!);
                },
                child: leftContainer(context, count: data.noAnswerCount, name: '未提交', nameColor: Color(0xFFD92F2F), bgColor: Color(0xFFEEEEEE)),
              ),
              InkWell(
                onTap: () {
                  showStudentListDialog(context: context, title: '已提交作业学生', arr: data.validStudents!);
                },
                child: leftContainer(context, count: data.validCount, name: '已提交', nameColor: Color(0xFF4CC793), bgColor: Color(0xFFF5F5F5)),
              ),
              InkWell(
                onTap: () {
                  showStudentListDialog(context: context, title: '全对作业学生', arr: data.allCorrectStudents!);
                },
                child: leftContainer(context, count: data.allCorrect, name: '全对', nameColor: Color(0xFFFF9800), bgColor: Color(0xFFEEEEEE)),
              ),
              Container(
                height: 92.r,
                width: rightWidth,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(
                        child: InkWell(
                      onTap: () {
                        showStudentListDialog(context: context, title: '优等作业学生', arr: data.overallInfos['优']!.students);
                      },
                      child: rightContainer(
                        count: data.overallInfos['优']!.count,
                        bgColor: Color(0xFF56FFB8),
                        nameColor: Color(0xFF009254),
                        name: '优',
                      ),
                    )),
                    Expanded(
                        child: InkWell(
                      onTap: () {
                        showStudentListDialog(context: context, title: '中等作业学生', arr: data.overallInfos['中']!.students);
                      },
                      child:
                          rightContainer(count: data.overallInfos['中']!.count, bgColor: Color(0xFFD3FF93), nameColor: Color(0xFF3F6605), name: '中'),
                    )),
                  ],
                ),
              ),
              Container(
                height: 92.r,
                width: rightWidth,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(
                        child: InkWell(
                      onTap: () {
                        showStudentListDialog(context: context, title: '良等作业学生', arr: data.overallInfos['良']!.students);
                      },
                      child:
                          rightContainer(count: data.overallInfos['良']!.count, bgColor: Color(0xFFFFC38C), nameColor: Color(0xFFD36500), name: '良'),
                    )),
                    Expanded(
                        child: InkWell(
                      onTap: () {
                        showStudentListDialog(context: context, title: '差等作业学生', arr: data.overallInfos['差']!.students);
                      },
                      child:
                          rightContainer(count: data.overallInfos['差']!.count, bgColor: Color(0xFFFF9D94), nameColor: Color(0xFFD12616), name: '差'),
                    )),
                  ],
                ),
              )
            ],
          ),
          /* Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              InkWell(
                onTap: () {
                  showStudentListDialog(
                      context: context,
                      title: '未提交作业学生',
                      arr: data.noAnswerStudentNames);
                },
                child: Container(
                  width: 81.r,
                  padding:
                      EdgeInsets.symmetric(vertical: 10.r, horizontal: 6.r),
                  decoration: BoxDecoration(
                    border: Border.all(
                        width: 1.r,
                        color: Color(0xFF4CC793),
                        style: BorderStyle.solid),
                    borderRadius: BorderRadius.all(Radius.circular(6.r)),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      SizedBox(
                        height: 5.r,
                      ),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          Text(
                            data.noAnswerCount.toString(),
                            style: TextStyle(
                                fontSize: 24.sp,
                                color: Color(0xFF4CC793),
                                fontWeight: FontWeight.w600),
                          ),
                          Text(
                            '人',
                            style: TextStyle(
                                fontSize: 16.sp,
                                color: Color(0xFF4CC793),
                                fontWeight: FontWeight.w600),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 20.r,
                      ),
                      Text(
                        '未提交',
                        style: TextStyle(
                            fontSize: 12.sp, color: Color(0xFF818181)),
                      )
                    ],
                  ),
                ),
              ),
              InkWell(
                onTap: () {
                  showStudentListDialog(
                      context: context,
                      title: '已提交作业学生',
                      arr: data.validStudentNames);
                },
                child: Container(
                  width: 81.r,
                  padding:
                      EdgeInsets.symmetric(vertical: 10.r, horizontal: 6.r),
                  decoration: BoxDecoration(
                    border: Border.all(
                        width: 1.r,
                        color: Color(0xFF4CC793),
                        style: BorderStyle.solid),
                    borderRadius: BorderRadius.all(Radius.circular(6.r)),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      SizedBox(
                        height: 5.r,
                      ),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          Text(
                            data.validCount.toString(),
                            style: TextStyle(
                                fontSize: 24.sp,
                                color: Color(0xFF4CC793),
                                fontWeight: FontWeight.w600),
                          ),
                          Text(
                            '人',
                            style: TextStyle(
                                fontSize: 16.sp,
                                color: Color(0xFF4CC793),
                                fontWeight: FontWeight.w600),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 20.r,
                      ),
                      Text(
                        '已提交',
                        style: TextStyle(
                            fontSize: 12.sp, color: Color(0xFF818181)),
                      )
                    ],
                  ),
                ),
              ),
              InkWell(
                onTap: () {
                  showStudentListDialog(
                      context: context,
                      title: '全对作业学生',
                      arr: data.allCorrectStudentNames);
                },
                child: Container(
                  width: 81.r,
                  padding:
                      EdgeInsets.symmetric(vertical: 10.r, horizontal: 6.r),
                  decoration: BoxDecoration(
                    border: Border.all(
                        width: 1.r,
                        color: Color(0xFF4CC793),
                        style: BorderStyle.solid),
                    borderRadius: BorderRadius.all(Radius.circular(6.r)),
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      SizedBox(
                        height: 5.r,
                      ),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          Text(
                            data.allCorrect.toString(),
                            style: TextStyle(
                                fontSize: 24.sp,
                                color: Color(0xFF4CC793),
                                fontWeight: FontWeight.w600),
                          ),
                          Text(
                            '人',
                            style: TextStyle(
                                fontSize: 16.sp,
                                color: Color(0xFF4CC793),
                                fontWeight: FontWeight.w600),
                          ),
                        ],
                      ),
                      SizedBox(
                        height: 20.r,
                      ),
                      Text(
                        '全对',
                        style: TextStyle(
                            fontSize: 12.sp, color: Color(0xFF818181)),
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
          SizedBox(
            height: 15.r,
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 15.r),
            child: GridView.builder(
              shrinkWrap: true,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                childAspectRatio: 150 / 46,
                crossAxisSpacing: 2.r, //水平子 Widget 之间间距
                mainAxisSpacing: 2.r, //垂直子 Widget 之间间距
                crossAxisCount: 2, //一行的 Widget 数量
              ),
              itemCount: data.overallTitles.length,
              itemBuilder: (BuildContext context, index) {
                var item = data.overallTitles[index];
                Color bgColor = Color(0xFF4CC793);
                if (item.title == '优') {
                  bgColor = Color(0xFF4CC793);
                } else if (item.title == '良') {
                  bgColor = Color(0xFFF8700D);
                } else if (item.title == '中') {
                  bgColor = Color(0xFF4CC7B8);
                } else if (item.title == '差') {
                  bgColor = Color(0xFFFF6F6F);
                }
                return InkWell(
                  onTap: () {
                    showStudentListDialog(context: context, title: item.title + '等作业学生', arr: item.studentNames);
                  },
                  child: Container(
                    padding: EdgeInsets.symmetric(vertical: 10.r),
                    width: (MediaQuery.of(context).size.width - 55.r) / 2,
                    color: Color(0xFFF5F5F5),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        SizedBox(
                          width: (MediaQuery.of(context).size.width - 55.r) / 4,
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Container(
                                width: 28.r,
                                height: 28.r,
                                decoration: BoxDecoration(
                                    color: Colors.white,
                                    border: Border.all(color: bgColor, width: 1.r, style: BorderStyle.solid),
                                    borderRadius: BorderRadius.circular(14.r)),
                                child: Center(
                                    child: Text(
                                  item.title,
                                  style: TextStyle(color: bgColor, fontSize: 12.r),
                                )),
                              ),
                            ],
                          ),
                        ),
                        SizedBox(
                          width: 10.r,
                        ),
                        Text(
                          item.count.toString(),
                          style: TextStyle(fontSize: 20.sp, color: Color(0xFF595959)),
                        ),
                        Text(
                          '人',
                          style: TextStyle(fontSize: 14.sp, color: Color(0xFF595959)),
                        ),
                      ],
                    ),
                  ),
                );
              },
            ),
          ),*/
        ],
      ),
    );
  }
}

@swidget
Widget leftContainer(context, {required int count, required String name, required Color nameColor, required Color bgColor}) {
  double leftWidth = (MediaQuery.of(context).size.width - 40.r) * 0.7 / 3;
  return Container(
    width: leftWidth,
    height: 92.r,
    padding: EdgeInsets.symmetric(vertical: 10.r, horizontal: 6.r),
    color: bgColor,
    child: Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        SizedBox(
          height: 5.r,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              count.toString(),
              style: TextStyle(fontSize: 18.sp, color: Color(0xFF595959), fontWeight: FontWeight.w600),
            ),
            Text(
              '人',
              style: TextStyle(fontSize: 10.sp, color: Color(0xFF595959), fontWeight: FontWeight.w600),
            ),
          ],
        ),
        SizedBox(
          height: 20.r,
        ),
        Text(
          name,
          style: TextStyle(fontSize: 12.sp, color: nameColor, fontWeight: FontWeight.w500),
        )
      ],
    ),
  );
}

@swidget
Widget rightContainer({required int count, required Color bgColor, required Color nameColor, required String name}) {
  return Container(
    color: bgColor,
    child: Column(
      children: [
        SizedBox(
          height: 5.r,
        ),
        Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              count.toString(),
              style: TextStyle(fontSize: 12.sp, color: nameColor, fontWeight: FontWeight.w600),
            ),
            Text(
              '人',
              style: TextStyle(fontSize: 8.sp, color: nameColor, fontWeight: FontWeight.w600),
            ),
          ],
        ),
        SizedBox(
          height: 5.r,
        ),
        Text(
          name,
          style: TextStyle(fontSize: 12.sp, color: nameColor, fontWeight: FontWeight.w500),
        )
      ],
    ),
  );
}
