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/common/model/job/job_report_join_class.dart';
import 'package:marking_app/pages/homework_correction/widget/quick_student_data_table.dart';
import 'package:marking_app/utils/index.dart';
import 'package:marking_app/utils/request/rest_client.dart';
import 'package:percent_indicator/linear_percent_indicator.dart';

class QuickDataCheckPage extends StatefulWidget {
  final int jobId;
  final String className;

  const QuickDataCheckPage({Key? key, required this.jobId,required this.className}) : 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['jobid'] = '521646983660101';
    BaseStructureResult<JobDataReport?> data =
        await _client.getJobDataCenterReport(params);
    EasyLoading.dismiss();
    setState(() {
      jobData = data.data;
    });

  }

  @override
  Widget build(BuildContext context) {
    if (jobData == null) {
      return Container();
    }
    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(
              children: [
                IconButton(
                  icon: Icon(Icons.arrow_back_ios, color: Colors.white),
                  onPressed: () => Navigator.of(context).pop(),
                ),
                Expanded(
                    child: Center(
                        child: Text(
                  '数据快查',
                  style: TextStyle(fontSize: 16.sp, color: Colors.white),
                ))),
              ],
            ),
            SizedBox(height:10.r),
            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.className,
                          style: TextStyle(fontSize: 14.r, color: Colors.white),
                        )
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(vertical: 10.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: [
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Container(
                              width: 14.r,
                              height: 14.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: 14.r,
                              height: 14.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: MediaQuery.of(context).size.width * 0.5,
                          child: PieChart(
                            PieChartData(
                              borderData: FlBorderData(show: false),
                              sectionsSpace: 0,
                              centerSpaceRadius: MediaQuery.of(context).size.width * 0.1,
                              sections: [
                                PieChartSectionData(
                                  color: Color(0xFF4CC793),
                                  value: jobData!.validCount/(jobData!.validCount+jobData!.noAnswerCount) * 100,
                                  radius: MediaQuery.of(context).size.width * 0.1 + 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.1,
                                  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),
                                )),
                          ],
                        ),
                      ],
                    ),
                  ),
                  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: [
                        InkWell(
                          onTap: (){
                            jobData!.studentDetails.sort((a, b) {
                              int num1 = a.kgValidRate + a.zgValidRate;
                              int num2 = b.kgValidRate + b.zgValidRate;
                              return  num2.compareTo(num1);
                            });
                            setState(() {
                              jobData!.studentDetails;
                            });
                          },
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              Text(
                                '未提交排序',
                                style: TextStyle(
                                    fontSize: 12.sp, color: Color(0xFF6888FD)),
                              ),
                              SizedBox(
                                width: 10.r,
                              ),
                              Image.asset(
                                'assets/images/sort_icon.png',
                                width: 14.r,
                                height: 14.r,
                              ),
                            ],
                          ),
                        ),
                        SizedBox(height: 10.r,),
                        SizedBox(
                          height: jobData!.studentDetails.length>5?350.r:jobData!.studentDetails.length * 50.r + 40.r,
                          child: QuickStudentDataTable(
                            headList: ['学生姓名','客观题','主观题','客观题错题','主观题错题'],
                            bodyList: jobData!.studentDetails,
                            jobId: widget.jobId,
                            fixedRows: 1,
                            fixedCols: 0,
                          ),
                        )
                      ],
                    ),
                  ),
                ],
              ),
            ))
          ],
        ),
      ),
    );
  }
}
