import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:marking_app/common/mixin/common.dart';
import 'package:marking_app/common/model/common/base_structure_result.dart';
import 'package:marking_app/common/model/job/job_data_report.dart';
import 'package:marking_app/components/ReturnToHomepage.dart';
import 'package:marking_app/pages/homework_correction/widget/quick_data_check_bottom.dart';
import 'package:marking_app/pages/homework_correction/widget/quick_student_data_table.dart';
import 'package:marking_app/pages/homework_correction/widget/report_table.dart';
import 'package:marking_app/utils/easy_refresh/MyEmptyWidget.dart';
import 'package:marking_app/utils/index.dart';
import 'package:marking_app/utils/request/rest_client.dart';
import 'package:marking_app/utils/right_home_icon.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';

class QuickDataCheckPage extends StatefulWidget {
  final int jobId;
  final String className;
  final String gradeName;
  final int? schoolId;
  final int? gradeId;

  const QuickDataCheckPage(
      {Key? key,
      required this.jobId,
      required this.className,
      required this.gradeName,
      this.schoolId,
      this.gradeId})
      : super(key: key);

  @override
  State<QuickDataCheckPage> createState() => _QuickDataCheckPageState();
}

class _QuickDataCheckPageState extends State<QuickDataCheckPage>
    with CommonMixin {
  JobDataReport? jobData;



  void initState() {
    super.initState();
    EasyLoading.show(status: 'loading...');
    getJobDataReport();
  }

  void getJobDataReport() async {
    RestClient _client = await getClient();
    Map<String, dynamic> params = {};
    params['jobid'] = widget.jobId;
    params['className'] = widget.className;
    if (widget.schoolId != null) {
      params['schoolId'] = widget.schoolId;
    }
    if (widget.gradeId != null) {
      params['gradeId'] = widget.gradeId;
    }

    BaseStructureResult<JobDataReport?> data =
        await _client.getJobDataCenterReport(params);
    EasyLoading.dismiss();
    if (data.code == 200) {
      int totalUnrated = 0;

      data.data!.studentDetails.forEach((element) {
        element.kgError = 0;
        element.kgCorrect = 0;
        element.zgError = 0;
        element.zgCorrect = 0;
        int num = 0;
        element.kgDetails.forEach((item) {
          if (item.state == 0) {
            num = num + 1;
          }else if (item.state == 1) {
            element.kgError = element.kgError + 1;
          }else if (item.state == 2) {
            element.kgCorrect = element.kgCorrect + 1;
          }
        });
        element.zgDetails.forEach((item) {
          if (item.state == 0) {
            num = num + 1;
          }else if (item.state == 1) {
            element.zgError = element.zgError + 1;
          }else if (item.state == 2) {
            element.zgCorrect = element.zgCorrect + 1;
          }else if (item.state == 3) {
            element.unrated = element.unrated + 1;
            totalUnrated = element.unrated;
          }
        });

        if(num == (element.kgDetails.length+ element.zgDetails.length)){
          element.allNotDone = true;
        }
      });
      if (totalUnrated > 0) {
        data.data!.hasUnrated = true;
      }

      setState(() {
        jobData = data.data;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion(
      value: const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        systemNavigationBarIconBrightness: Brightness.light,
        statusBarIconBrightness: Brightness.light,
        statusBarBrightness: Brightness.dark,
      ),
      child: Container(
        padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(
            gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
              Color(0xFF6889FD),
              Color(0xFFF5F5F5),
            ],
                stops: [
              0.09,
              0.3
            ])),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                IconButton(
                  icon: Icon(Icons.arrow_back_ios, color: Colors.white),
                  onPressed: () => Navigator.of(context).pop(),
                ),
                Expanded(
                    child: Padding(
                  padding: EdgeInsets.only(right: 8.r),
                  child: Center(
                      child: Text(
                    '数据快查',
                    style: TextStyle(fontSize: 14.sp, color: Colors.white),
                  )),
                )),
                ReturnToHomepage(bgColor: Colors.white,),
              ],
            ),
            SizedBox(height: 10.r),
            jobData != null
                ? Expanded(
                    child: SingleChildScrollView(
                    child: Column(
                      children: [
                        Padding(
                          padding: EdgeInsets.only(left: 14.r, top: 2.r),
                          child: Row(
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Image.asset(
                                'assets/images/job_report_class_icon.png',
                                width: 22.r,
                                height: 22.r,
                              ),
                              SizedBox(
                                width: 6.r,
                              ),
                              Text(
                               widget.gradeName + widget.className,
                                style: TextStyle(
                                    fontSize: 14.r, color: Colors.white),
                              )
                            ],
                          ),
                        ),
                        Container(
                          padding: EdgeInsets.symmetric(
                              vertical: 15.r, horizontal: 15.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: [
                              SizedBox(
                                height: 2.r,
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Container(
                                    width: 12.r,
                                    height: 12.r,
                                    decoration: BoxDecoration(
                                        color: Color(0xFF4CC793),
                                        borderRadius: BorderRadius.all(
                                            Radius.circular(7.r))),
                                  ),
                                  SizedBox(
                                    width: 6.r,
                                  ),
                                  Text(
                                    '已提交',
                                    style: TextStyle(
                                        fontSize: 12.sp,
                                        color: Color(0xFF333333)),
                                  ),
                                  SizedBox(
                                    width: 35.r,
                                  ),
                                  Container(
                                    width: 12.r,
                                    height: 12.r,
                                    decoration: BoxDecoration(
                                        color: Color(0xFF6888FD),
                                        borderRadius: BorderRadius.all(
                                            Radius.circular(7.r))),
                                  ),
                                  SizedBox(
                                    width: 6.r,
                                  ),
                                  Text(
                                    '未提交',
                                    style: TextStyle(
                                        fontSize: 12.sp,
                                        color: Color(0xFF333333)),
                                  )
                                ],
                              ),
                              SizedBox(
                                height: 2.r,
                              ),
                              //环形图
                              SizedBox(
                                height: MediaQuery.of(context).size.width * 0.3,
                                child: PieChart(
                                  PieChartData(
                                    borderData: FlBorderData(show: false),
                                    sectionsSpace: 0,
                                    centerSpaceRadius:
                                        MediaQuery.of(context).size.width * 0.06,
                                    sections: [
                                      PieChartSectionData(
                                        color: Color(0xFF4CC793),
                                        value: jobData!.validCount /
                                            (jobData!.validCount +
                                                jobData!.noAnswerCount) *
                                            100,
                                        radius:
                                            MediaQuery.of(context).size.width *
                                                0.07 +
                                                5,
                                        title: '${jobData!.validCount}人',
                                        titleStyle: TextStyle(
                                          fontSize: 14.sp,
                                          color: Colors.white,
                                        ),
                                      ),
                                      PieChartSectionData(
                                        color: Color(0xFF6888FD),
                                        value: jobData!.noAnswerCount /
                                            (jobData!.validCount +
                                                jobData!.noAnswerCount) *
                                            100,
                                        radius:
                                            MediaQuery.of(context).size.width *
                                                0.07,
                                        title: '${jobData!.noAnswerCount}人',
                                        titleStyle: TextStyle(
                                          fontSize: 14.sp,
                                          color: Colors.white,
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                              ),
                              //  客观进度条
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    '客观题答题进度',
                                    style: TextStyle(
                                        fontSize: 10.sp,
                                        color: Color(0xFF8B8B8B)),
                                  ),
                                  Text(
                                    '${doubleToStringAsFixed(jobData!.kgValidRate)}%',
                                    style: TextStyle(
                                        fontSize: 10.sp,
                                        color: Color(0xFF333333)),
                                  ),
                                ],
                              ),
                              SizedBox(height: 6.r),
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Expanded(
                                      flex: 10,
                                      child: LinearPercentIndicator(
                                        padding: EdgeInsets.zero,
                                        animation: true,
                                        lineHeight: 9.h,
                                        animationDuration: 2500,
                                        percent: jobData!.kgValidRate / 100,
                                        progressColor: Color(0xFFFF7F22),
                                        backgroundColor: Color(0xFFEAEAEA),
                                        barRadius: Radius.circular(10.r),
                                      )),
                                ],
                              ),
                              SizedBox(height: 20.r),
                              //  主观进度条
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Text(
                                    '主观题答题进度',
                                    style: TextStyle(
                                        fontSize: 10.sp,
                                        color: Color(0xFF8B8B8B)),
                                  ),
                                  Text(
                                    '${doubleToStringAsFixed(jobData!.zgValidRate)}%',
                                    style: TextStyle(
                                        fontSize: 10.sp,
                                        color: Color(0xFF333333)),
                                  ),
                                ],
                              ),
                              SizedBox(height: 6.r),
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Expanded(
                                      flex: 10,
                                      child: LinearPercentIndicator(
                                        padding: EdgeInsets.zero,
                                        animation: true,
                                        lineHeight: 9.h,
                                        animationDuration: 2500,
                                        percent: jobData!.zgValidRate / 100,
                                        progressColor: Color(0xFFFF7F22),
                                        backgroundColor: Color(0xFFEAEAEA),
                                        barRadius: Radius.circular(10.r),
                                      )),
                                ],
                              ),
                            ],
                          ),
                        ),
                        QuickDataCheckBottom(jobData:jobData,jobId: widget.jobId,),
                        //客观题
                        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(
                            children: [
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.end,
                                children: [
                                  Text(
                                    '客观题',
                                    style: TextStyle(fontSize: 14.sp, color: Color(0xFF5C5C5C), fontWeight: FontWeight.w600),
                                  ),
                                  SizedBox(
                                    width: 10.r,
                                  ),
                                  Text(
                                    '${jobData!.kgReport.correctRate}%',
                                    style: TextStyle(fontSize: 14.sp, color: Color(0xFF6888FD), fontWeight: FontWeight.w600),
                                  ),
                                ],
                              ),
                              SizedBox(
                                height: 6.r,
                              ),
                              SizedBox(
                                height: jobData!.kgReport.details.length > 10 ? 300.r : jobData!.kgReport.details.length * 40.r + (isPad() == true ?40.r:65.r),
                                child: ReportTable(
                                  headList: ['题', '作答率', '作答人数', '正确率', '标准答案', '优先批阅概况'],
                                  bodyList: jobData!.kgReport.details,
                                  fixedCols: 1,
                                  fixedRows: 1,
                                  jobId: widget.jobId,
                                ),
                              )
                            ],
                          ),
                        ),
                        //主观题
                        Container(
                          padding: EdgeInsets.symmetric(vertical: 10.r, horizontal: 14.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,
                                crossAxisAlignment: CrossAxisAlignment.end,
                                children: [
                                  Text(
                                    '主观题',
                                    style: TextStyle(fontSize: 14.sp, color: Color(0xFF5C5C5C), fontWeight: FontWeight.w600),
                                  ),
                                  SizedBox(
                                    width: 6.r,
                                  ),
                                  Text(
                                    '${jobData!.zgReport.correctRate}%',
                                    style: TextStyle(fontSize: 14.sp, color: Color(0xFF6888FD), fontWeight: FontWeight.w600),
                                  ),
                                ],
                              ),
                              SizedBox(
                                height: 10.r,
                              ),
                              SizedBox(
                                height: jobData!.zgReport.details.length > 10 ? 300.r : jobData!.zgReport.details.length * 40.r +(isPad() == true ?40.r:65.r),
                                child: ReportTable(
                                  headList: ['题', '作答率', '作答人数', '正确率', '查看原题', '优先批阅概况'],
                                  bodyList: jobData!.zgReport.details,
                                  fixedCols: 1,
                                  fixedRows: 1,
                                  isKG: true,
                                  jobId: widget.jobId,
                                ),
                              )
                            ],
                          ),
                        ),
                      ],
                    ),
                  ))
                : Padding(
                    padding: EdgeInsets.only(
                        top: MediaQuery.of(context).size.height / 2 - 200.r),
                    child: MyEmptyWidget(),
                  ),


          ],
        ),
      ),
    );
  }
}
