import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/utils/toast_utils.dart';
import 'package:making_school_asignment_app/page/global_widget/ReturnToHomepage.dart';
import 'package:making_school_asignment_app/page/home_page/children/homework_review/components/original_manuscript_handwriting/answer_handwriting_view.dart';
import 'package:making_school_asignment_app/page/home_page/children/student_personal/widget/student_kg_table.dart';
import 'package:making_school_asignment_app/page/home_page/children/student_personal/widget/student_zg_table.dart';
import 'package:making_school_asignment_app/routes/app_pages.dart';

import 'student_personal_logic.dart';

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

  @override
  State<StudentPersonalPage> createState() => _StudentPersonalPageState();
}

class _StudentPersonalPageState extends State<StudentPersonalPage> {
  final logic = Get.find<StudentPersonalLogic>();
  final state = Get.find<StudentPersonalLogic>().state;

  @override
  Widget build(BuildContext context) {
    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.studentInfo.value.studentName,
              style: TextStyle(fontSize: 14.sp, color: const Color(0xFF000000)),
            );
          }),
          centerTitle: true,
          leading: IconButton(
            icon: const Icon(Icons.arrow_back_ios, color: Colors.black),
            onPressed: () => Get.back(),
          ),
          actions: const [
            ReturnToHomepage(),
          ],
        ),
        body: SingleChildScrollView(
          child: Column(
            children: [
              Padding(
                padding: EdgeInsets.only(top: 14.r, left: 14.r),
                child: Row(
                  children: [
                    InkWell(
                      onTap: () {
                        // RouterManager.router.navigateTo(context,
                        //     '${RouterManager.jobPersonalDetailPath}?studentId=${widget.studentId}&studentName=${Uri.encodeComponent(state.studentInfo.studentName!)}');
                        Get.toNamed(Routes.studentWorkDetailPage, arguments: {
                          'studentId': state.studentInfo.value.studentId,
                          'studentName': state.studentInfo.value.studentName,
                          'subject': state.subject
                        });
                      },
                      child: Container(
                        width: 93.r,
                        height: 28.r,
                        decoration: BoxDecoration(
                          color: const Color(0xFFEAF3FF),
                          borderRadius: BorderRadius.circular(4.r),
                        ),
                        child: Center(
                          child: Text(
                            '历史作业',
                            style: TextStyle(fontSize: 10.r, color: const Color(0xFF2080F7)),
                          ),
                        ),
                      ),
                    ),
                    SizedBox(
                      width: 10.r,
                    ),
                    InkWell(
                      onTap: () =>
                          showAnswerHandwriting(context, homeworkId: state.homeworkId, studentId: state.studentId).then((e) => ToastUtils.dismiss()),
                      child: Container(
                        width: 93.r,
                        height: 28.r,
                        decoration: BoxDecoration(
                          color: const Color(0xFFEDFFF7),
                          borderRadius: BorderRadius.circular(4.r),
                        ),
                        child: Center(
                          child: Text(
                            '原稿笔迹',
                            style: TextStyle(fontSize: 10.r, color: const Color(0xFF4CC793)),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              //客观题
              Container(
                padding: EdgeInsets.symmetric(vertical: 14.r, horizontal: 10.r),
                margin: EdgeInsets.symmetric(vertical: 14.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: const Color(0xFF5C5C5C), fontWeight: FontWeight.w600),
                        ),
                        SizedBox(
                          width: 10.r,
                        ),
                        Obx(() {
                          return Text(
                            '${state.studentInfo.value.kgtCorrectRate}%',
                            style: TextStyle(fontSize: 14.sp, color: const Color(0xFF4CC793), fontWeight: FontWeight.w600),
                          );
                        }),
                      ],
                    ),
                    SizedBox(
                      height: 10.r,
                    ),
                    Obx(() {
                      return SizedBox(
                        height: state.studentInfo.value.kgtList.length > 8 ? 300.r : state.studentInfo.value.kgtList.length * 40.r + 40.r,
                        child: StudentKgTable(
                          headList: const ['题号', '学生答案', '标准答案'],
                          bodyList: state.studentInfo.value.kgtList,
                          questionNumCall: (questionNo, templateId) {
                            showAnswerHandwriting(context,
                                    homeworkId: state.homeworkId, studentId: state.studentId, questionNo: questionNo, templateId: templateId)
                                .then((e) => ToastUtils.dismiss());
                            // showAnswerHandwriting(context, jobId: widget.jobId, studentId: widget.studentId, questionNo: int.parse(no)).then((value) {
                            //   ref.read(jobHandwritingDrawingTrajectoryProvider.notifier).setVal([]);
                            // });
                          },
                        ),
                      );
                    })
                  ],
                ),
              ),
              SizedBox(
                height: 15.r,
              ),
              //主观题
              Container(
                padding: EdgeInsets.symmetric(vertical: 14.r, horizontal: 10.r),
                margin: EdgeInsets.symmetric(vertical: 14.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: const Color(0xFF5C5C5C), fontWeight: FontWeight.w600),
                        ),
                        SizedBox(
                          width: 10.r,
                        ),
                        Obx(() {
                          return Text(
                            '${state.studentInfo.value.zgtCorrectRate}%',
                            style: TextStyle(fontSize: 14.sp, color: const Color(0xFF4CC793), fontWeight: FontWeight.w600),
                          );
                        }),
                      ],
                    ),
                    SizedBox(
                      height: 10.r,
                    ),
                    Obx(() {
                      return SizedBox(
                        height: state.studentInfo.value.zgtList.length > 8 ? 300.r : state.studentInfo.value.zgtList.length * 40.r + 40.r,
                        child: StudentZgTable(
                          headList: const ['题号', '用时', '批注结果', '答案'],
                          bodyList: state.studentInfo.value.zgtList,
                          questionNumCall: (questionNo, templateId) {
                            /* showAnswerHandwriting(context, jobId: widget.jobId, studentId: widget.studentId, questionNo: int.parse(no)).then((value) {
                          ref.read(jobHandwritingDrawingTrajectoryProvider.notifier).setVal([]);
                        });*/
                            showAnswerHandwriting(context,
                                    homeworkId: state.homeworkId, studentId: state.studentId, questionNo: questionNo, templateId: templateId)
                                .then((e) => ToastUtils.dismiss());
                          },
                        ),
                      );
                    })
                  ],
                ),
              ),
            ],
          ),
        ),
      );
    });
  }

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