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:marking_app/common/mixin/common.dart';
import 'package:marking_app/common/model/common/base_structure_result.dart';
import 'package:marking_app/common/model/job/job_level_set_params.dart';
import 'package:marking_app/common/model/job/job_review_submission.dart';
import 'package:marking_app/common/model/job/job_student_level.dart';
import 'package:marking_app/components/ReturnToHomepage.dart';
import 'package:marking_app/routes/RouterManager.dart';
import 'package:marking_app/utils/easy_refresh/MyEmptyWidget.dart';
import 'package:marking_app/utils/index.dart';
import 'package:marking_app/utils/request/rest_client.dart';

class JobPriorityReviewSet extends StatefulWidget {
  final String groupId;
  final String title;
  final String? page;

  const JobPriorityReviewSet({Key? key, required this.groupId,required this.title,this.page = ''})
      : super(key: key);

  @override
  State<JobPriorityReviewSet> createState() => _JobPriorityReviewSetState();
}

class _JobPriorityReviewSetState extends State<JobPriorityReviewSet>
    with CommonMixin, SingleTickerProviderStateMixin {
  late final EasyRefreshController refreshController;
  late TabController tabController;
  int tabIndex = 0;
  List list1 = [];
  List list2 = [];
  List levelList = [];
  bool isClicking = false;

  @override
  void initState() {
    super.initState();
    EasyLoading.show(status: 'loading...');
    refreshController = EasyRefreshController();
    tabController =
        TabController(initialIndex: tabIndex, length: 2, vsync: this);
    getList();
  }

  void getList() async {
    await getReadLevel(1);
    await getReadLevel(0);

    setState(() {
      levelList = [...list1, ...list2];
      isClicking = false;
    });
    EasyLoading.dismiss();
    refreshController.finishRefresh();
  }

  getReadLevel(int readLevel) async {
    RestClient _client = await getClient();
    BaseStructureResult<List<JobStudentLevel>> res =
        await _client.getJobReadLevel(widget.groupId, readLevel);
    if (res.success) {
      if (readLevel == 1) {
        list1 = res.data!;
      } else {
        list2 = res.data!;
      }
    } else {
      if (readLevel == 1) {
        list1 = [];
      } else {
        list2 = [];
      }
    }
  }

  void setJobReadLevel(int studentId, int level) async {
    RestClient _client = await getClient();
    JobLevelSetParams params = JobLevelSetParams(studentId, level);
    BaseStructureResult res = await _client.getSetJobReadLevel(params);
    if (res.code == 200) {
      getList();
    }
  }

  @override
  void dispose() {
    super.dispose();
    refreshController.dispose();
    tabController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    bool isPadFlag = isPad();
    return Scaffold(
      backgroundColor: Color.fromRGBO(245, 245, 245, 1),
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text(
          widget.title,
          style: TextStyle(fontSize: 14.sp, color: Color(0xFF333333)),
        ),
        centerTitle: true,
        leading: IconButton(
          icon: Icon(Icons.arrow_back_ios, color: Colors.black),
          onPressed: () => Navigator.of(context).pop(),
        ),
        actions: [
          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(
              onTap: (int val) {
                print(val);
                setState(() {
                  tabIndex = val;
                });
                EasyLoading.show(status: 'loading...');
                getReadLevel();
              },
              tabs: [
                SizedBox(
                  width: (MediaQuery.of(context).size.width - 28.r) /2,
                  child: Tab(
                    text: '优先批阅',
                  ),
                ),
                SizedBox(
                  width: (MediaQuery.of(context).size.width - 28.r) /2,
                  child: Tab(
                    text: '默认批阅',
                  ),
                )
              ],
              controller: tabController,
              unselectedLabelStyle: TextStyle(
                  fontSize: 14.sp, color:Color(0xFF666666)),
              labelStyle: TextStyle(
                fontSize: 14.sp,
                color: Color(0xFF6888FD),
              ),
              isScrollable: true,
              labelColor: Color(0xFF6888FD),
              unselectedLabelColor: Color(0xFF666666),
              indicatorSize: TabBarIndicatorSize.label,
                labelPadding: const EdgeInsets.all(0),
            ),
          ),*/

          Expanded(
            child: Padding(
              padding: EdgeInsets.symmetric(vertical: 14.r, horizontal: 14.r),
              child: EasyRefresh(
                firstRefresh: true,
                taskIndependence: true,
                controller: refreshController,
                header: MaterialHeader(),
                footer: TaurusFooter(),
                onRefresh: () async {
                  getList();
                },
                child: levelList.length > 0
                    ? isPadFlag
                        ? GridView(
                            gridDelegate:
                                SliverGridDelegateWithFixedCrossAxisCount(
                              crossAxisCount: 2,
                              mainAxisSpacing: 10.r,
                              crossAxisSpacing: 10.r,
                              childAspectRatio: 556 / 112,
                            ),
                            children: List.generate(levelList.length, (index) {
                              JobStudentLevel item = levelList[index];
                              return InkWell(
                                onTap: (){
                                  RouterManager.router.navigateTo(context,
                                      '${RouterManager.jobPersonalDetailPath}?studentId=${item.studentId}&studentName=${Uri.encodeComponent(item.studentName)}');
                                },
                                child: Container(
                                  padding: EdgeInsets.symmetric(horizontal: 10.r),
                                  decoration: BoxDecoration(
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(10.r)),
                                    color: Colors.white,
                                  ),
                                  child: Row(
                                    mainAxisAlignment:
                                        MainAxisAlignment.spaceBetween,
                                    children: [
                                      Expanded(
                                          child: Text(
                                        item.studentName,
                                        style: TextStyle(
                                            fontSize: 12.sp,
                                            color: Color(0xFF6888FD)),
                                      )),
                                      item.readLevel == 1
                                          ?
                                      widget.page == 'history'?Container(
                                        height: 20.r,
                                        width: 70.r,
                                        decoration: BoxDecoration(
                                            color: Color(0xFF6888FD),
                                            borderRadius: BorderRadius.all(Radius.circular(20.r))
                                        ),
                                        child: Center(child: Text('历史作业',style: TextStyle(fontSize: 10.r,color: Colors.white),)),
                                      ):InkWell(
                                              onTap: () {
                                                setState(() {
                                                  isClicking = true;
                                                });
                                                setJobReadLevel(
                                                    item.studentGroupDetailId, 0);
                                                EasyLoading.show(
                                                    status: 'loading...');
                                              },
                                              child:Container(
                                                height: 20.r,
                                                width: 70.r,
                                                decoration: BoxDecoration(
                                                  borderRadius: BorderRadius.all(
                                                      Radius.circular(4.r)),
                                                  color: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: 2.r,left: 4.r),
                                                      child: Text(
                                                        '优先批阅',
                                                        style: TextStyle(
                                                            fontSize: 10.sp,
                                                            color: Color(0xFF4CC793)),
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                            )
                                          : widget.page == 'history'?Container(
                                        height: 20.r,
                                        width: 70.r,
                                        decoration: BoxDecoration(
                                            color: Color(0xFF6888FD),
                                            borderRadius: BorderRadius.all(Radius.circular(20.r))
                                        ),
                                        child: Center(child: Text('历史作业',style: TextStyle(fontSize: 10.r,color: Colors.white),)),
                                      ):InkWell(
                                              onTap: () {
                                                setState(() {
                                                  isClicking = true;
                                                });
                                                setJobReadLevel(
                                                    item.studentGroupDetailId, 1);
                                                EasyLoading.show(
                                                    status: 'loading...');
                                              },
                                              child: Container(
                                                height: 20.r,
                                                width: 70.r,
                                                decoration: BoxDecoration(
                                                  borderRadius: BorderRadius.all(
                                                      Radius.circular(4.r)),
                                                  color: 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: Color(0xFF8A9691)),
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                            ),
                                     /* SizedBox(
                                        width: 5.r,
                                      ),
                                      InkWell(
                                        onTap: () {
                                          RouterManager.router.navigateTo(context,
                                              '${RouterManager.jobPersonalDetailPath}?studentId=${item.studentId}&studentName=${Uri.encodeComponent(item.studentName)}');
                                        },
                                        child: Container(
                                          height: 20.r,
                                          width: 70.r,
                                          decoration: BoxDecoration(
                                              borderRadius: BorderRadius.all(
                                                  Radius.circular(20.r)),
                                              color: Colors.white,
                                              border: Border.all(
                                                  width: 1.r,
                                                  color: Color(0xFFFCA017))),
                                          child: Center(
                                            child: Text(
                                              '详情',
                                              style: TextStyle(
                                                  fontSize: 10.sp,
                                                  color: Color(0xFFFCA017)),
                                            ),
                                          ),
                                        ),
                                      )*/
                                    ],
                                  ),
                                ),
                              );
                            }),
                          )
                        : ListView.builder(
                            itemBuilder: (context, index) {
                              JobStudentLevel item = levelList[index];
                              return InkWell(
                                onTap: (){
                                  RouterManager.router.navigateTo(context,
                                      '${RouterManager.jobPersonalDetailPath}?studentId=${item.studentId}&studentName=${Uri.encodeComponent(item.studentName)}');
                                },
                                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.studentName,
                                        style: TextStyle(
                                            fontSize: 12.sp,
                                            color: Color(0xFF6888FD)),
                                      )),
                                      item.readLevel == 1
                                          ? widget.page == 'history'?Container(
                                        height: 24.r,
                                        width: 82.r,
                                        decoration: BoxDecoration(
                                            color: Color(0xFF6888FD),
                                            borderRadius: BorderRadius.all(Radius.circular(20.r))
                                        ),
                                        child: Center(child: Text('历史作业',style: TextStyle(fontSize: 10.r,color: Colors.white),)),
                                      ):InkWell(
                                              onTap: () {
                                                setState(() {
                                                  isClicking = true;
                                                });
                                                setJobReadLevel(
                                                    item.studentGroupDetailId, 0);
                                                EasyLoading.show(
                                                    status: 'loading...');
                                              },
                                              child: Container(
                                                height: 24.r,
                                                width: 82.r,
                                                decoration: BoxDecoration(
                                                  borderRadius: BorderRadius.all(
                                                      Radius.circular(4.r)),
                                                  color: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: 2.r,left: 4.r),
                                                      child: Text(
                                                        '优先批阅',
                                                        style: TextStyle(
                                                            fontSize: 10.sp,
                                                            color: Color(0xFF4CC793)),
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                            )
                                          : widget.page == 'history'?Container(
                                        height: 24.r,
                                        width: 82.r,
                                        decoration: BoxDecoration(
                                            color: Color(0xFF6888FD),
                                            borderRadius: BorderRadius.all(Radius.circular(20.r))
                                        ),
                                        child: Center(child: Text('历史作业',style: TextStyle(fontSize: 10.r,color: Colors.white),)),
                                      ):InkWell(
                                              onTap: () {
                                                setJobReadLevel(
                                                    item.studentGroupDetailId, 1);
                                                EasyLoading.show(
                                                    status: 'loading...');
                                              },
                                              child: Container(
                                                height: 24.r,
                                                width: 82.r,
                                                decoration: BoxDecoration(
                                                  borderRadius: BorderRadius.all(
                                                      Radius.circular(4.r)),
                                                  color: 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: Color(0xFF8A9691)),
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                            ),
                                    /*  SizedBox(
                                        width: 5.r,
                                      ),
                                      InkWell(
                                        onTap: () {
                                          RouterManager.router.navigateTo(context,
                                              '${RouterManager.jobPersonalDetailPath}?studentId=${item.studentId}&studentName=${Uri.encodeComponent(item.studentName)}');
                                        },
                                        child: Container(
                                          height: 20.r,
                                          width: 70.r,
                                          decoration: BoxDecoration(
                                              borderRadius: BorderRadius.all(
                                                  Radius.circular(20.r)),
                                              color: Colors.white,
                                              border: Border.all(
                                                  width: 1.r,
                                                  color: Color(0xFFFCA017))),
                                          child: Center(
                                            child: Text(
                                              '详情',
                                              style: TextStyle(
                                                  fontSize: 10.sp,
                                                  color: Color(0xFFFCA017)),
                                            ),
                                          ),
                                        ),
                                      )*/
                                    ],
                                  ),
                                ),
                              );
                            },
                            itemCount: levelList.length,
                          )
                    : MyEmptyWidget(),
              ),
            ),
          )
        ],
      ),
    );
  }
}
