import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/config/request_config.dart';
import 'package:percent_indicator/percent_indicator.dart';
import 'package:making_school_asignment_app/common/job/knowledge_report_detail.dart';
import 'package:making_school_asignment_app/common/utils/toast_utils.dart';
import 'package:making_school_asignment_app/page/global_widget/MyEmptyWidget.dart';
import 'package:making_school_asignment_app/page/global_widget/ReturnToHomepage.dart';
import 'package:making_school_asignment_app/page/global_widget/my_text.dart';
import 'package:making_school_asignment_app/page/global_widget/show_student_list.dart';

import 'knowledge_points_grasp_detail_logic.dart';

class KnowledgePointsGraspDetailPage extends StatefulWidget {
  const KnowledgePointsGraspDetailPage({super.key});

  @override
  State<KnowledgePointsGraspDetailPage> createState() => _KnowledgePointsGraspDetailPageState();
}

class _KnowledgePointsGraspDetailPageState extends State<KnowledgePointsGraspDetailPage> {
  final logic = Get.find<KnowledgePointsGraspDetailLogic>();
  final state = Get.find<KnowledgePointsGraspDetailLogic>().state;

  @override
  Widget build(BuildContext context) {
    void showImg(imgUrl, context) {
      if (imgUrl != null && imgUrl != '') {
        showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              insetPadding: EdgeInsets.symmetric(vertical: 55.r, horizontal: 45.r),
              contentPadding: EdgeInsets.all(0),
              shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(15.r))),
              content: Container(
                width: MediaQuery.of(context).size.width,
                // height: MediaQuery.of(context).size.height,
                child: Image.network(RequestConfig.imgUrl + imgUrl),
              ),
            );
          },
        );
      } else {
        ToastUtils.showError('暂无图片');
      }

      EasyLoading.dismiss();
    }

    void showStudent(homeworkId, templateId, questionNo, title) async {
      logic.getStudents(homeworkId, templateId, questionNo);

      showDialog(
        context: context,
        builder: (BuildContext context) {
          return Obx(() {
            return ShowStudentList(
              title: title,
              studentList: state.studentList.value,
              homeworkId: homeworkId,
              subject: state.subject,
            );
          });
        },
      );
      EasyLoading.dismiss();
    }

    return OrientationBuilder(builder: (BuildContext context, Orientation orientation) {
      return Scaffold(
        backgroundColor: const Color.fromRGBO(245, 245, 245, 1),
        appBar: AppBar(
          backgroundColor: Colors.white,
          title: Obx(() {
            return Text(state.knowledgeName.value, style: TextStyle(fontSize: 14.sp, color: const Color(0xFF333333)));
          }),
          centerTitle: true,
          leading: IconButton(
            icon: const Icon(Icons.arrow_back_ios, color: Colors.black),
            onPressed: () => Get.back(),
          ),
          actions: const [
            ReturnToHomepage(),
          ],
          elevation: 0,
        ),
        body: Padding(
          padding: EdgeInsets.symmetric(vertical: 10.r),
          child: Obx(() {
            return EasyRefresh(
              firstRefresh: false,
              taskIndependence: true,
              controller: logic.refreshController,
              header: MaterialHeader(),
              footer: TaurusFooter(),
              onRefresh: () async {
                logic.getList();
              },
              onLoad: () async {
                // getList();
              },
              child: state.dataList.isNotEmpty
                  ? ListView.builder(
                      itemCount: state.dataList.length,
                      itemBuilder: (context, index) {
                        KnowledgeReportDetail item = state.dataList[index];
                        return InkWell(
                          onTap: () {
                            /*  RouterManager.router.navigateTo(
                      context,
                      RouterManager.quickCheckPersonalPath +
                          '?jobId=${item.jobName}&studentId=$studentId',
                      transition: getTransition(),
                    );*/
                          },
                          child: Container(
                            margin: EdgeInsets.symmetric(vertical: 5.r, horizontal: 14.r),
                            padding: EdgeInsets.symmetric(vertical: 14.r, horizontal: 10.r),
                            decoration: BoxDecoration(borderRadius: BorderRadius.all(Radius.circular(10.r)), color: Colors.white),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  mainAxisAlignment: MainAxisAlignment.start,
                                  crossAxisAlignment: CrossAxisAlignment.end,
                                  children: [
                                    Text(
                                      item.publishTime.substring(0, 10),
                                      style: TextStyle(fontSize: 14.sp, color: const Color(0xFF505050)),
                                    ),
                                    SizedBox(
                                      width: 10.r,
                                    ),
                                    Expanded(
                                        child: Text(
                                      item.homeworkName,
                                      style: TextStyle(fontSize: 14.sp, color: const Color(0xFF505050)),
                                    )),
                                    InkWell(
                                      onTap: () {
                                        EasyLoading.show(status: 'loading...');
                                        showImg(item.questionPicture, context);
                                      },
                                      child: Container(
                                        height: 22.r,
                                        padding: EdgeInsets.symmetric(horizontal: 4.w),
                                        decoration: BoxDecoration(
                                          borderRadius: BorderRadius.all(Radius.circular(20.r)),
                                          border: Border.all(width: 1.r, color: const Color(0xFF8B8B8B)),
                                        ),
                                        child: Center(
                                          child: Text(
                                            '第${item.questionNo}题',
                                            style: TextStyle(fontSize: 10.sp, color: const Color(0xFF8B8B8B)),
                                          ),
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                                SizedBox(
                                  height: 10.r,
                                ),
                                Container(
                                  margin: EdgeInsets.only(top: 8.h),
                                  padding: EdgeInsets.zero,
                                  child: Row(
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      InkWell(
                                        onTap: () {
                                          EasyLoading.show(status: 'loading...');
                                          showStudent(item.homeworkId, item.templateId, item.questionNo, item.homeworkName);
                                        },
                                        child: Container(
                                            margin: EdgeInsets.only(right: 6.r),
                                            width: 56.r,
                                            height: 20.r,
                                            decoration: BoxDecoration(
                                              color: const Color(0xFFE9FFD9),
                                              borderRadius: BorderRadius.circular(20.r),
                                            ),
                                            child: Row(
                                              mainAxisAlignment: MainAxisAlignment.center,
                                              children: [
                                                quickText('正确率', color: const Color(0xFF313131), size: 10.sp),
                                                Image.asset(
                                                  'assets/images/job_data_right_icon.png',
                                                  width: 8.r,
                                                  height: 8.r,
                                                )
                                              ],
                                            )),
                                      ),
                                      Expanded(
                                        flex: 1,
                                        child: Container(
                                          child: Row(
                                            children: [
                                              Expanded(
                                                child: Container(
                                                  decoration: BoxDecoration(
                                                    borderRadius: BorderRadius.circular(10.r),
                                                  ),
                                                  child: LinearPercentIndicator(
                                                    padding: EdgeInsets.zero,
                                                    animation: true,
                                                    lineHeight: 8.h,
                                                    animationDuration: 2500,
                                                    percent: item.correctRate / 100,
                                                    progressColor: Color(0xFFB2DA93),
                                                    backgroundColor: Color(0xFFE8E8E8),
                                                    barRadius: Radius.circular(10.r),
                                                  ),
                                                ),
                                              ),
                                              SizedBox(width: 4.w),
                                              quickText('${item.correctRate.toStringAsFixed(0)}%', size: 10.sp, color: Color(0xFF606060))
                                            ],
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                /* progressBar(context,
                            title: '正确率 >',
                            color: Color(0xFF90E0BE),
                            percent: item.correctRate / 100,
                            padingEdg: EdgeInsets.zero,
                            marginEdg: EdgeInsets.only(top: 8.h),
                        studentCall:showStudent(item.questionId,item.jobName),
                        ),*/
                              ],
                            ),
                          ),
                        );
                      })
                  : const MyEmptyWidget(),
            );
          }),
        ),
      );
    });
  }

  @override
  void dispose() {
    Get.delete<KnowledgePointsGraspDetailLogic>();
    super.dispose();
  }
}
