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/page/global_widget/ReturnToHomepage.dart';
import 'package:making_school_asignment_app/page/home_page/children/answer_trajectory/widget/answer_trajectory_job.dart';
import 'package:making_school_asignment_app/page/home_page/widget/student_group_list.dart';

import 'answer_trajectory_logic.dart';

class AnswerTrajectoryPage extends StatefulWidget {
  const AnswerTrajectoryPage({Key? key}) : super(key: key);

  @override
  State<AnswerTrajectoryPage> createState() => _AnswerTrajectoryPageState();
}

class _AnswerTrajectoryPageState extends State<AnswerTrajectoryPage> {
  final logic = Get.find<AnswerTrajectoryLogic>();
  final state = Get
      .find<AnswerTrajectoryLogic>()
      .state;

  @override
  Widget build(BuildContext context) {
    return OrientationBuilder(
        builder: (BuildContext context, Orientation orientation){
          return  Scaffold(
            backgroundColor: Color.fromRGBO(245, 245, 245, 1),
            appBar: AppBar(
              backgroundColor: Colors.white,
              title: Text(
                '答题轨迹',
                style: TextStyle(fontSize: 14.sp, color: Color(0xFF333333)),
              ),
              centerTitle: true,
              leading: IconButton(
                icon: Icon(Icons.arrow_back_ios, color: Colors.black),
                onPressed: () => Get.back(),
              ),
              actions: const [
                ReturnToHomepage(),
              ],
            ),
            body: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                SizedBox(
                  height: 10.r,
                ),
                Container(
                  padding: EdgeInsets.symmetric(horizontal: 14.r),
                  decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(width: 1.r, color: Color(0xFFCCCCCC)))),
                  child: TabBar(
                    dividerHeight: 0,
                    tabAlignment: TabAlignment.start,
                    indicator:  const UnderlineTabIndicator(
                      borderSide: BorderSide(
                        color: Color(0xFF8C68FF),
                      ),

                    ),
                    onTap: (int val) {
                      state.tabIndex.value = val;
                      // EasyLoading.show(status: 'loading...');
                      /* if(val == 0){
                  logic.getStudentGroups();
                }else{
                  logic.getWorkList();
                }*/
                    },
                    tabs: [
                      SizedBox(
                        width: (MediaQuery
                            .of(context)
                            .size
                            .width - 28.r) / 2,
                        child: const Tab(
                          text: '按学生',
                        ),
                      ),
                      SizedBox(
                        width: (MediaQuery
                            .of(context)
                            .size
                            .width - 28.r) / 2,
                        child: const Tab(
                          text: '按作业',
                        ),
                      )
                    ],
                    controller: logic.tabController,
                    unselectedLabelStyle:
                    TextStyle(fontSize: 14.sp, color: Color(0xFF666666)),
                    labelStyle: TextStyle(
                      fontSize: 14.sp,
                      color:  Color(0xFF8C68FF),
                    ),
                    isScrollable: true,
                    labelColor:  Color(0xFF8C68FF),
                    unselectedLabelColor: Color(0xFF666666),
                    indicatorSize: TabBarIndicatorSize.label,
                    labelPadding: const EdgeInsets.all(0),
                  ),
                ),
                Obx(() {
                  return Expanded(
                    child: Padding(
                      padding:
                      EdgeInsets.only(top: 15.r, left: 14.r, right: 14.r),
                      child:
                      state.tabIndex.value == 0
                          ?
                      EasyRefresh(
                        firstRefresh: false,
                        taskIndependence: true,
                        controller: logic.refreshController,
                        header: MaterialHeader(),
                        footer: TaurusFooter(),
                        onRefresh: () async {
                          logic.getStudentGroups();
                        },
                        child: StudentGroupList(
                            state.studentGroups, logic.goNextPage,
                            rightBtn: Container(
                              margin: EdgeInsets.only(left: 5.r),
                              height: 20.r,
                              width: 55.r,
                              decoration: BoxDecoration(
                                borderRadius:
                                BorderRadius.all(Radius.circular(20.r)),
                                border: Border.all(
                                    width: 1.r, color: Color(0xFF7EB752)),
                              ),
                              child: Center(
                                child: Text(
                                  '详情',
                                  style: TextStyle(
                                      fontSize: 10.sp, color: Color(0xFF7EB752)),
                                ),
                              ),
                            )),
                      ) : EasyRefresh(
                          firstRefresh: false,
                          taskIndependence: true,
                          controller: logic.refreshController2,
                          header: MaterialHeader(),
                          footer: TaurusFooter(),
                          onRefresh: () async {
                            state.page = 1;
                            logic.getWorkList();
                          },
                          onLoad: () async {
                            if (state.jobList.length < state.total) {
                              EasyLoading.show(status: 'loading...');
                              state.page = state.page + 1;
                              logic.getWorkList();
                            }
                          },
                          child: AnswerTrajectoryJob(state.jobList.value)),
                    ),
                  );
                })
              ],
            ),
          );
        }
    );


  }

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