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/job/student_item.dart';
import 'package:making_school_asignment_app/common/utils/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/routes/app_pages.dart';

import 'class_student_logic.dart';

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

  @override
  State<ClassStudentPage> createState() => _ClassStudentPageState();
}

class _ClassStudentPageState extends State<ClassStudentPage> {
  final logic = Get.find<ClassStudentLogic>();
  final state = Get.find<ClassStudentLogic>().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.title.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: Obx(() {
          return EasyRefresh(
            firstRefresh: false,
            taskIndependence: true,
            controller: logic.refreshController,
            header: MaterialHeader(),
            footer: TaurusFooter(),
            onRefresh: () async {
              logic.getList();
            },
            child: state.studentList.isNotEmpty
                ? Utils.isPad()
                    ? GridView(
                        shrinkWrap: true,
                        physics: const NeverScrollableScrollPhysics(),
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                          crossAxisCount: 2,
                          mainAxisSpacing: 0.r,
                          crossAxisSpacing: 0.r,
                          childAspectRatio: 556 / 90,
                        ),
                        children: List.generate(state.studentList.length, (index) {
                          StudentItem item = state.studentList[index];
                          return InkWell(
                            onTap: () {
                              // RouterManager.router.navigateTo(context,
                              //     '${RouterManager.jobPersonalDetailPath}?studentId=${item.studentId}&studentName=${Uri.encodeComponent(item.studentName)}');
                              Get.toNamed(Routes.studentWorkDetailPage, arguments: {
                                'studentName': item.name,
                                'studentId': item.id,
                                'subject': state.subject,
                              });
                            },
                            child: Container(
                              padding: EdgeInsets.symmetric(horizontal: 10.r),
                              decoration: BoxDecoration(
                                  borderRadius: BorderRadius.all(Radius.circular(0.r)),
                                  color: Colors.transparent,
                                  border: Border(
                                      left: BorderSide(
                                          width: (index + 1) % 2 == 0 ? 1.r : 0, color: const Color(0xFFA5A5A5)),
                                      bottom: BorderSide(width: 1.r, color: const Color(0xFFA5A5A5)))),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Expanded(
                                      child: Text(
                                    item.name,
                                    style: TextStyle(fontSize: 12.sp, color: Theme.of(context).primaryColor),
                                  )),
                                  state.page == 'answerTrajectory'
                                      ? Container(
                                          height: 20.r,
                                          width: 70.r,
                                          decoration: BoxDecoration(
                                            border: Border.all(width: 1.r, color: const Color(0xFFB2DA93)),
                                            borderRadius: BorderRadius.all(Radius.circular(20.r)),
                                          ),
                                          child: Center(
                                            child: Text('详情',
                                                style: TextStyle(fontSize: 10.r, color: const Color(0xFFB2DA93))),
                                          ))
                                      : state.page == 'history'
                                          ? Container(
                                              height: 20.r,
                                              width: 70.r,
                                              decoration: BoxDecoration(
                                                  color: Theme.of(context).primaryColor,
                                                  borderRadius: BorderRadius.all(Radius.circular(20.r))),
                                              child: Center(
                                                  child: Text(
                                                '历史作业',
                                                style: TextStyle(fontSize: 10.r, color: Colors.white),
                                              )),
                                            )
                                          : item.priorityAnnotate
                                              ? InkWell(
                                                  onTap: () {
                                                    logic.setJobReadLevel(item.id, false);
                                                    EasyLoading.show(status: 'loading...');
                                                  },
                                                  child: Container(
                                                    height: 20.r,
                                                    width: 80.r,
                                                    decoration: BoxDecoration(
                                                      borderRadius: BorderRadius.all(Radius.circular(4.r)),
                                                      color: const Color(0xFFEBE4FF),
                                                    ),
                                                    child: Row(
                                                      crossAxisAlignment: CrossAxisAlignment.start,
                                                      children: [
                                                        Padding(
                                                          padding: EdgeInsets.only(left: 3.r),
                                                          child: Image.asset(
                                                            'assets/images/youx_icon_active.png',
                                                            width: 14.r,
                                                            height: 14.r,
                                                          ),
                                                        ),
                                                        Padding(
                                                          padding: EdgeInsets.only(top: 2.r, left: 4.r),
                                                          child: Text(
                                                            '优先批阅',
                                                            style: TextStyle(
                                                                fontSize: 10.sp, color: Theme.of(context).primaryColor),
                                                          ),
                                                        ),
                                                      ],
                                                    ),
                                                  ),
                                                )
                                              : InkWell(
                                                  onTap: () {
                                                    logic.setJobReadLevel(item.id, true);
                                                    EasyLoading.show(status: 'loading...');
                                                  },
                                                  child: Container(
                                                    height: 20.r,
                                                    width: 80.r,
                                                    decoration: BoxDecoration(
                                                      borderRadius: BorderRadius.all(Radius.circular(4.r)),
                                                      color: const Color(0xFFE1E1E1),
                                                    ),
                                                    child: Row(
                                                      crossAxisAlignment: CrossAxisAlignment.start,
                                                      children: [
                                                        Padding(
                                                          padding: EdgeInsets.only(left: 3.r),
                                                          child: Image.asset(
                                                            'assets/images/youx_icon_default.png',
                                                            width: 14.r,
                                                            height: 14.r,
                                                          ),
                                                        ),
                                                        Padding(
                                                          padding: EdgeInsets.only(top: 2.r, left: 4.r),
                                                          child: Text(
                                                            '优先批阅',
                                                            style: TextStyle(
                                                                fontSize: 10.sp, color: const Color(0xFF8A9691)),
                                                          ),
                                                        ),
                                                      ],
                                                    ),
                                                  ),
                                                ),
                                ],
                              ),
                            ),
                          );
                        }),
                      )
                    : Padding(
                        padding: EdgeInsets.symmetric(vertical: 14.r, horizontal: 14.r),
                        child: ListView.builder(
                          shrinkWrap: true,
                          physics: const NeverScrollableScrollPhysics(),
                          itemCount: state.studentList.length,
                          itemBuilder: (context, index) {
                            StudentItem item = state.studentList[index];
                            return InkWell(
                              onTap: () {
                                // RouterManager.router.navigateTo(context,
                                //     '${RouterManager.jobPersonalDetailPath}?studentId=${item.studentId}&studentName=${Uri.encodeComponent(item.studentName)}');
                                Get.toNamed(Routes.studentWorkDetailPage, arguments: {
                                  'studentName': item.name,
                                  'studentId': item.id,
                                  'subject': state.subject,
                                });
                              },
                              child: Container(
                                padding: EdgeInsets.symmetric(vertical: 20.r, horizontal: 15.r),
                                margin: EdgeInsets.only(bottom: 15.r),
                                decoration: BoxDecoration(
                                  borderRadius: BorderRadius.all(Radius.circular(10.r)),
                                  color: Colors.white,
                                ),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                  children: [
                                    Expanded(
                                        child: Text(
                                      item.name,
                                      style: TextStyle(fontSize: 12.sp, color: const Color(0xFF4CC793)),
                                    )),
                                    state.page == 'answerTrajectory'
                                        ? Container(
                                            height: 24.r,
                                            width: 72.r,
                                            decoration: BoxDecoration(
                                              border: Border.all(width: 1.r, color: const Color(0xFFB2DA93)),
                                              borderRadius: BorderRadius.all(Radius.circular(20.r)),
                                            ),
                                            child: Center(
                                              child: Text('详情',
                                                  style: TextStyle(fontSize: 10.r, color: const Color(0xFFB2DA93))),
                                            ))
                                        : state.page == 'history'
                                            ? Container(
                                                height: 24.r,
                                                width: 82.r,
                                                decoration: BoxDecoration(
                                                    color: Theme.of(context).primaryColor,
                                                    borderRadius: BorderRadius.all(Radius.circular(20.r))),
                                                child: Center(
                                                    child: Text(
                                                  '历史作业',
                                                  style: TextStyle(fontSize: 10.r, color: Colors.white),
                                                )),
                                              )
                                            : item.priorityAnnotate
                                                ? InkWell(
                                                    onTap: () {
                                                      logic.setJobReadLevel(item.id, false);
                                                      EasyLoading.show(status: 'loading...');
                                                    },
                                                    child: Container(
                                                      height: 24.r,
                                                      width: 82.r,
                                                      decoration: BoxDecoration(
                                                        borderRadius: BorderRadius.all(Radius.circular(4.r)),
                                                        color: const Color(0xFFB7FFE0),
                                                      ),
                                                      child: Row(
                                                        crossAxisAlignment: CrossAxisAlignment.start,
                                                        children: [
                                                          Padding(
                                                            padding: EdgeInsets.only(left: 3.r),
                                                            child: Image.asset(
                                                              'assets/images/youx_icon_active.png',
                                                              width: 14.r,
                                                              height: 14.r,
                                                            ),
                                                          ),
                                                          Padding(
                                                            padding: EdgeInsets.only(top: 5.r, left: 4.r),
                                                            child: Text(
                                                              '优先批阅',
                                                              style: TextStyle(
                                                                  fontSize: 10.sp, color: const Color(0xFF4CC793)),
                                                            ),
                                                          ),
                                                        ],
                                                      ),
                                                    ),
                                                  )
                                                : InkWell(
                                                    onTap: () {
                                                      logic.setJobReadLevel(item.id, true);
                                                      EasyLoading.show(status: 'loading...');
                                                    },
                                                    child: Container(
                                                      height: 24.r,
                                                      width: 82.r,
                                                      decoration: BoxDecoration(
                                                        borderRadius: BorderRadius.all(Radius.circular(4.r)),
                                                        color: const Color(0xFFE1E1E1),
                                                      ),
                                                      child: Row(
                                                        crossAxisAlignment: CrossAxisAlignment.start,
                                                        children: [
                                                          Padding(
                                                            padding: EdgeInsets.only(left: 3.r),
                                                            child: Image.asset(
                                                              'assets/images/youx_icon_default.png',
                                                              width: 14.r,
                                                              height: 14.r,
                                                            ),
                                                          ),
                                                          Padding(
                                                            padding: EdgeInsets.only(top: 5.r, left: 4.r),
                                                            child: Text(
                                                              '优先批阅',
                                                              style: TextStyle(
                                                                  fontSize: 10.sp, color: const Color(0xFF8A9691)),
                                                            ),
                                                          ),
                                                        ],
                                                      ),
                                                    ),
                                                  ),
                                  ],
                                ),
                              ),
                            );
                          },
                        ),
                      )
                : const MyEmptyWidget(),
          );
        }),
      );
    });
  }

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