import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/job/annotated_class.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/page/global_widget/my_text.dart';
import 'package:making_school_asignment_app/page/home_page/children/fav_student/widget/favorite_student_dialog.dart';
import 'package:making_school_asignment_app/page/home_page/children/job_report/widget/dropdown_selection.dart';

import 'fav_student_logic.dart';

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

  @override
  State<FavStudentPage> createState() => _FavStudentPageState();
}

class _FavStudentPageState extends State<FavStudentPage> {
  final logic = Get.find<FavStudentLogic>();
  final state = Get.find<FavStudentLogic>().state;

  void showStudentDialog(BuildContext context, HomeworkFavs item, List groups) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          insetPadding: EdgeInsets.all(25.r),
          content: FavoriteStudentDialog(
            item: item,
            group: groups,
            deleteFav: logic.getDelete,
            confirmDialog: confirmDialog,
          ),
          contentPadding: const EdgeInsets.all(0),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(15.r))),
        );
      },
    );
  }

  Future<bool> confirmDialog() async {
    return await showDialog(
      context: context,
      builder: (context) => AlertDialog(
        actionsPadding: EdgeInsets.symmetric(vertical: 10.r, horizontal: 10.r),
        content: Text(
          '确定删除吗？',
          style: TextStyle(fontSize: 14.sp, color: const Color(0xFF505E6E)),
        ),
        actionsAlignment: MainAxisAlignment.center,
        actions: <Widget>[
          InkWell(
            child: Container(
                width: 97.r,
                height: 27.r,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(20.r)),
                  color: Theme.of(context).primaryColor,
                ),
                child: const Center(
                    child: Text(
                  '确定',
                  style: TextStyle(color: Colors.white),
                ))),
            onTap: () {
              // 执行操作
              // Navigator.of(context).pop(true);
              Get.back(result: true);
            },
          ),
          InkWell(
            onTap: () {
              // Navigator.of(context).pop(false);
              Get.back(result: false);
            },
            child: Container(
                width: 97.r,
                height: 27.r,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(20.r)),
                  color: const Color(0xFFF4F4F4),
                ),
                child: const Center(
                    child: Text(
                  '取消',
                  style: TextStyle(color: Color(0xFF666666)),
                ))),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: const Color.fromRGBO(245, 245, 245, 1),
        appBar: AppBar(
          // titleSpacing: 0,
          elevation: 0.0,
          leading: IconButton(
              icon: const Icon(Icons.arrow_back_ios),
              onPressed: () {
                Get.back();
              }),
          iconTheme: const IconThemeData(color: Colors.black),
          title: quickText('收藏夹'),
          centerTitle: true,
          backgroundColor: Colors.white,
          actions: const [
            ReturnToHomepage(),
          ],
        ),
        body: OrientationBuilder(builder: (BuildContext context, Orientation orientation) {
          return Column(
            children: [
              Padding(
                padding: EdgeInsets.symmetric(vertical: 0.r, horizontal: 14.r),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Expanded(
                      child: Obx(() {
                        return Text(
                          state.homeworkName.value,
                          style: TextStyle(
                            fontSize: 14.sp,
                            color: const Color(0xFF3C3C3C),
                          ),
                          overflow: TextOverflow.ellipsis,
                        );
                      }),
                    ),
                    // 下拉框
                    Container(
                      padding: EdgeInsets.symmetric(vertical: 10.h, horizontal: 10.w),
                      child: Row(
                        children: [
                          Obx(() {
                            return DropdownSelection(
                                bgColor: Colors.white,
                                involveClasses: state.involveClasses.value,
                                classData: state.classData.value,
                                onClassSelected: (AnnotatedClass item) {
                                  state.classData.value = item;
                                  if (item.grade == -1) state.classData.value = state.defaultClass;
                                  logic.getList();
                                });
                          }),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
              Container(
                width: MediaQuery.of(context).size.width,
                height: 1.r,
                decoration: const BoxDecoration(
                  color: Color(0xFFEEEEEE),
                ),
              ),
              Obx(() {
                return state.favList.isNotEmpty
                    ? Expanded(
                        child: Utils.isPad()
                            ? Padding(
                                padding: EdgeInsets.only(top: 10.r, bottom: 8.r, left: 14.r, right: 14.r),
                                child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: List.generate(state.favList.length, (index) {
                                      var item = state.favList[index];
                                      return Padding(
                                        padding: EdgeInsets.only(bottom: 8.r),
                                        child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Padding(
                                              padding: EdgeInsets.only(bottom: 5.r),
                                              child: Text(
                                                '第${item['questionPage']}页',
                                                style:
                                                    TextStyle(fontSize: 12.sp, color: Theme.of(context).primaryColor),
                                              ),
                                            ),
                                            GridView(
                                                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                                                  crossAxisCount: 2,
                                                  mainAxisSpacing: 8.r,
                                                  crossAxisSpacing: 10.r,
                                                  childAspectRatio: 556 / 112,
                                                ),
                                                shrinkWrap: true,
                                                children: List.generate(item['list'].length, (i) {
                                                  HomeworkFavs student = item['list'][i];
                                                  return Container(
                                                    padding: EdgeInsets.symmetric(vertical: 5.r, horizontal: 10.r),
                                                    decoration: BoxDecoration(
                                                      color: Colors.white,
                                                      borderRadius: BorderRadius.all(Radius.circular(6.r)),
                                                    ),
                                                    child: InkWell(
                                                      onTap: () {
                                                        showStudentDialog(context, student, state.favList);
                                                      },
                                                      child: Row(
                                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                        children: [
                                                          quickText(student.studentName,
                                                              color: const Color(0xFF333333), size: 12.sp),
                                                          Expanded(child: Container()),
                                                          Padding(
                                                            padding: EdgeInsets.only(right: 8.r),
                                                            child: Text(
                                                              student.className!,
                                                              style: TextStyle(
                                                                  fontSize: 12.sp, color: const Color(0xFF666666)),
                                                            ),
                                                          ),
                                                          InkWell(
                                                            onTap: () async {
                                                              var confim = await confirmDialog();
                                                              if (confim) {
                                                                logic.getDelete(student);
                                                              }
                                                            },
                                                            child: Image.asset(
                                                              'assets/images/favorite_delete_icon.png',
                                                              width: 24.r,
                                                              height: 24.r,
                                                            ),
                                                          ),
                                                        ],
                                                      ),
                                                    ),
                                                  );
                                                })),
                                          ],
                                        ),
                                      );
                                    })),
                              )
                            : ListView.builder(
                                shrinkWrap: true,
                                itemBuilder: (context, index) {
                                  var item = state.favList[index];
                                  return Padding(
                                    padding: EdgeInsets.only(top: 10.r, bottom: 8.r, left: 14.r, right: 14.r),
                                    child: Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Padding(
                                          padding: EdgeInsets.only(bottom: 5.r),
                                          child: Text(
                                            '第${item['questionPage']}页',
                                            style: TextStyle(fontSize: 14.sp, color: Theme.of(context).primaryColor),
                                          ),
                                        ),
                                        ListView.builder(
                                          itemBuilder: (context, i) {
                                            HomeworkFavs student = item['list'][i];
                                            return InkWell(
                                              onTap: () {
                                                showStudentDialog(context, student, state.favList);
                                              },
                                              child: Container(
                                                padding: EdgeInsets.symmetric(vertical: 5.r, horizontal: 10.r),
                                                margin: EdgeInsets.only(top: 5.r),
                                                decoration: BoxDecoration(
                                                  color: Colors.white,
                                                  borderRadius: BorderRadius.all(Radius.circular(6.r)),
                                                ),
                                                child: Row(
                                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                  children: [
                                                    quickText(student.studentName,
                                                        color: const Color(0xFF333333), size: 14.sp),
                                                    Expanded(child: Container()),
                                                    Padding(
                                                      padding: EdgeInsets.only(right: 8.r),
                                                      child: Text(
                                                        student.className!,
                                                        style:
                                                            TextStyle(fontSize: 14.sp, color: const Color(0xFF666666)),
                                                      ),
                                                    ),
                                                    InkWell(
                                                      onTap: () async {
                                                        var confim = await confirmDialog();
                                                        if (confim) {
                                                          logic.getDelete(student);
                                                        }
                                                      },
                                                      child: Image.asset(
                                                        'assets/images/favorite_delete_icon.png',
                                                        width: 32.r,
                                                        height: 32.r,
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                            );
                                          },
                                          itemCount: item['list'].length,
                                          shrinkWrap: true,
                                        ),
                                      ],
                                    ),
                                  );
                                },
                                itemCount: state.favList.length,
                              ),
                      )
                    : Padding(
                        padding: EdgeInsets.only(top: MediaQuery.of(context).size.height / 2 - 200.r),
                        child: const MyEmptyWidget(),
                      );
              }),
            ],
          );
        }));
  }

  @override
  void dispose() {
    Get.delete<FavStudentLogic>();
    super.dispose();
    logic.controller.getList();
  }
}
