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:percent_indicator/percent_indicator.dart';
import 'package:making_school_asignment_app/common/job/work_student.dart';
import 'package:making_school_asignment_app/common/mixins/request_tool_mixin.dart';
import 'package:making_school_asignment_app/common/utils/enum_untils.dart';
import 'package:making_school_asignment_app/common/utils/toast_utils.dart';
import 'package:making_school_asignment_app/page/global_widget/my_text.dart';
import 'package:making_school_asignment_app/common/utils/utils.dart';
import 'package:making_school_asignment_app/page/home_page/children/read_over/read_over_logic.dart';
import 'package:making_school_asignment_app/routes/app_pages.dart';

class TaskListItem extends StatefulWidget {
  final bool completed;
  final Items jobTaskItem;
  final int type;
  final int tabIndex;
  final Function(Items item) collectFun;

  const TaskListItem({Key? key, required this.completed, required this.jobTaskItem, required this.type, required this.collectFun, required this.tabIndex}) : super(key: key);

  @override
  State<TaskListItem> createState() => _TaskListItemState();
}

class _TaskListItemState extends State<TaskListItem> {
  @override
  Widget build(BuildContext context) {
    return widget.completed
        ? InkWell(
            onTap: () {
              Get.toNamed(Routes.annotateClassPage, arguments: {'id': widget.jobTaskItem.id, 'name': widget.jobTaskItem.name, 'grade': widget.jobTaskItem.grade, 'subject': widget.jobTaskItem.subject, 'completed': true, 'tabIndex': widget.tabIndex});
            },
            child: Container(
              width: double.infinity,
              padding: EdgeInsets.only(top: 20.h),
              margin: EdgeInsets.only(bottom: 12.h),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(6.r),
                color: const Color.fromRGBO(255, 255, 255, 1),
                boxShadow: const [
                  BoxShadow(
                    color: Color.fromRGBO(210, 216, 241, 1),
                    offset: Offset.zero, //阴影y轴偏移量
                    blurRadius: 5.8, //阴影模糊程度
                    spreadRadius: 0, //阴影扩散程度
                  )
                ],
              ),
              child: Column(
                children: [
                  // 顶部任务名称
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 10.w),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Container(
                          width: Utils.isPad() ? 32.w : 36.w,
                          height: 18.h,
                          alignment: Alignment.center,
                          padding: EdgeInsets.only(left: Utils.isPad() ? 2.w : 3.w),
                          decoration: BoxDecoration(
                            color: const Color(0xFFB2DA93),
                            borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(14.r),
                              topRight: Radius.circular(3.r),
                              bottomLeft: Radius.circular(4.r),
                              bottomRight: Radius.circular(4.r),
                            ),
                          ),
                          margin: EdgeInsets.only(right: 4.w),
                          child: quickText(widget.jobTaskItem.assessType == 0 ? '作业' : '考试', color: Colors.white, size: 10.sp),
                        ),
                        Expanded(
                          child: quickText(
                            widget.jobTaskItem.name,
                            maxLines: 2,
                            size: 16.sp,
                            color: const Color.fromRGBO(70, 70, 70, 1),
                            fontWeight: FontWeight.bold,
                          ),
                        )
                      ],
                    ),
                  ),

                  SizedBox(height: 12.h),
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 10.w),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        quickText(
                          DateTime.parse(widget.jobTaskItem.publishTime).toString().substring(0, 10),
                          color: const Color.fromRGBO(97, 97, 97, 1),
                          size: 12.sp,
                          fontWeight: FontWeight.w500,
                        ),
                        quickText(' / ', color: const Color.fromRGBO(76, 199, 147, 1), size: 12.sp, fontWeight: FontWeight.w500),
                        quickText(
                          '参与班级：${widget.jobTaskItem.classCount}',
                          color: const Color.fromRGBO(76, 199, 147, 1),
                          size: 12.sp,
                          fontWeight: FontWeight.w600,
                        ),
                        quickText(' / ', color: const Color.fromRGBO(116, 145, 253, 1), size: 12.sp, fontWeight: FontWeight.w500),
                        quickText(
                          '科目：${EnumUtils.formatSubject(widget.jobTaskItem.subject)}',
                          color: const Color.fromRGBO(116, 145, 253, 1),
                          size: 12.sp,
                          fontWeight: FontWeight.w600,
                        ),
                        const Expanded(child: SizedBox()),
                        /* FavoriteButton(
                    jobTaskItem.id,
                    jobTaskItem.title,
                    margin: EdgeInsets.zero,
                    isRow: false,
                  ),*/
                      ],
                    ),
                  ),

                  SizedBox(height: 20.h),
                  Container(
                    padding: EdgeInsets.symmetric(vertical: 10.h),
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.only(bottomLeft: Radius.circular(6.r), bottomRight: Radius.circular(6.r)),
                      color: Colors.white,
                      boxShadow: const [
                        BoxShadow(
                          color: Color.fromRGBO(0, 0, 0, 0.15),
                          offset: Offset(0, -0.0001), //阴影y轴偏移量
                          blurRadius: 4, //阴影模糊程度
                          spreadRadius: 0, //阴影扩散程度
                        )
                      ],
                    ),
                    child: Row(children: [
                      Expanded(
                          child: InkWell(
                        onTap: () {
                          Get.toNamed(Routes.jobReportPage, arguments: {
                            'title': widget.jobTaskItem.name,
                            'homeworkId': widget.jobTaskItem.id,
                            'grade': widget.jobTaskItem.grade,
                          });
                        },
                        child: Container(
                          alignment: Alignment.center,
                          child: quickText('查看报告', color: const Color.fromRGBO(118, 118, 118, 1), size: 13.sp),
                        ),
                      )),
                    ]),
                  ),
                ],
              ),
            ),
          )
        : Padding(
            padding: EdgeInsets.only(top: 10.r),
            child: InkWell(
              onTap: () {
                Get.toNamed(Routes.annotateClassPage, arguments: {'id': widget.jobTaskItem.id, 'name': widget.jobTaskItem.name, 'grade': widget.jobTaskItem.grade, 'subject': widget.jobTaskItem.subject, 'tabIndex': widget.tabIndex});
              },
              child: Container(
                margin: EdgeInsets.only(bottom: 16.h),
                width: Get.width,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(6.r),
                  color: const Color.fromRGBO(255, 255, 255, 1),
                  boxShadow: const [
                    BoxShadow(
                      color: Color.fromRGBO(210, 216, 241, 1),
                      offset: Offset.zero, //阴影y轴偏移量
                      blurRadius: 5.8, //阴影模糊程度
                      spreadRadius: 0, //阴影扩散程度
                    )
                  ],
                ),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Expanded(
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Container(
                                width: Utils.isPad() ? 32.w : 38.w,
                                height: 18.h,
                                alignment: Alignment.center,
                                padding: EdgeInsets.only(left: Utils.isPad() ? 2.w : 3.w),
                                decoration: BoxDecoration(
                                  color: widget.type == 1 ? const Color(0xFFB2DA93) : const Color(0xFFEF8714),
                                  borderRadius: BorderRadius.only(
                                    topLeft: Radius.circular(6.r),
                                    topRight: Radius.circular(0.r),
                                    bottomLeft: Radius.circular(0.r),
                                    bottomRight: Radius.circular(12.r),
                                  ),
                                ),
                                margin: EdgeInsets.only(top: 0.h, right: 4.w),
                                child: quickText(widget.jobTaskItem.assessType == 0 ? '作业' : '考试', color: Colors.white, size: 10.sp),
                              ),
                              Expanded(
                                child: Padding(
                                  padding: EdgeInsets.only(top: 2.r),
                                  child: quickText(
                                    widget.jobTaskItem.name,
                                    maxLines: 2,
                                    size: Utils.isPad() ? 14.sp : 16.sp,
                                    color: const Color.fromRGBO(70, 70, 70, 1),
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              )
                            ],
                          ),
                          SizedBox(height: 10.h),
                          Padding(
                            padding: EdgeInsets.only(left: 20.r),
                            child: Row(
                              crossAxisAlignment: CrossAxisAlignment.end,
                              children: [
                                quickText(
                                  EnumUtils.formatSubject(widget.jobTaskItem.subject),
                                  color: const Color.fromRGBO(97, 97, 97, 1),
                                  size: 12.sp,
                                ),
                                quickText(' / ', color: const Color.fromRGBO(130, 130, 130, 1), size: 11.sp, fontWeight: FontWeight.w500),
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.end,
                                  children: [
                                    quickText('题量：', color: const Color.fromRGBO(130, 130, 130, 1), size: 11.sp),
                                    quickText(
                                      '${widget.jobTaskItem.questionCount! - widget.jobTaskItem.annotateCount!}',
                                      color: const Color.fromRGBO(97, 97, 97, 1),
                                      size: 13.sp,
                                    ),
                                  ],
                                ),
                                quickText(' / ', color: const Color.fromRGBO(130, 130, 130, 1), size: 11.sp, fontWeight: FontWeight.w500),
                                quickText(DateTime.parse(widget.jobTaskItem.publishTime).toString().substring(0, 10), color: const Color.fromRGBO(97, 97, 97, 1), size: 12.sp),
                              ],
                            ),
                          ),
                          SizedBox(height: 10.h),
                          InkWell(
                            onTap: () {
                              if (!widget.jobTaskItem.isFixed!) {
                                EasyLoading.show(status: 'loading...');
                                widget.collectFun(widget.jobTaskItem);
                              }
                            },
                            child: Container(
                              padding: EdgeInsets.symmetric(vertical: 4.r, horizontal: 20.r),
                              margin: EdgeInsets.only(left: 20.r),
                              decoration: BoxDecoration(
                                color: widget.jobTaskItem.isFixed! ? Color(0xFFF4F4F4) : Color(0xFFF2EEFF),
                                border: Border.all(width: 1.r, color: widget.jobTaskItem.isFixed! ? Colors.transparent : const Color(0xFF8C68FF)),
                                borderRadius: BorderRadius.all(Radius.circular(20.r)),
                              ),
                              child: Text(
                                widget.jobTaskItem.isFixed! ? '已订正' : '收集订正',
                                style: TextStyle(fontSize: 10.sp, color: widget.jobTaskItem.isFixed! ? const Color(0xFFF16262) : const Color(0xFF8C68FF)),
                              ),
                            ),
                          ),
                          // FavoriteButton(jobTaskItem.id, jobTaskItem.title),
                        ],
                      ),
                    ),
                    SizedBox(
                      width: 10.r,
                    ),
                    Container(
                      padding: EdgeInsets.symmetric(vertical: 10.r, horizontal: 9.r),
                      decoration: const BoxDecoration(shape: BoxShape.circle, color: Colors.white),
                      child: CircularPercentIndicator(
                        radius: 50.r,
                        lineWidth: 10.r,
                        animation: true,
                        reverse: true,
                        percent: widget.jobTaskItem.annotateRate == null ? 0 : widget.jobTaskItem.annotateRate! / 100,
                        center: Text.rich(TextSpan(children: [
                          TextSpan(
                            text: Utils.getDoubleRemoveZero(widget.jobTaskItem.annotateRate, '0'),
                            style: TextStyle(
                              fontWeight: FontWeight.bold,
                              fontSize: 16.sp,
                              color: Theme.of(context).primaryColor,
                            ),
                          ),
                          TextSpan(
                            text: "%",
                            style: TextStyle(color: Theme.of(context).primaryColor, fontSize: 12.sp, fontWeight: FontWeight.bold),
                          ),
                        ])),
                        circularStrokeCap: CircularStrokeCap.round,
                        // progressColor: Theme.of(context).primaryColor,
                        linearGradient: LinearGradient(
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          tileMode: TileMode.clamp,
                          stops: const [0.0, 1.0],
                          colors: [
                            Theme.of(context).primaryColor.withOpacity(0.1),
                            Theme.of(context).primaryColor,
                          ],
                        ),
                        backgroundColor: const Color.fromRGBO(244, 244, 244, 1),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          );
  }
}
