import 'dart:convert';

import 'package:flutter/material.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_student_goups.dart';
import 'package:marking_app/common/model/user/user_info.dart';
import 'package:marking_app/components/ReturnToHomepage.dart';
import 'package:marking_app/pages/homework_correction/widget/student_group_list.dart';
import 'package:marking_app/routes/RouterManager.dart';
import 'package:marking_app/utils/easy_refresh/MyEmptyWidget.dart';
import 'package:marking_app/utils/fast_data.dart';
import 'package:marking_app/utils/index.dart';
import 'package:marking_app/utils/request/rest_client.dart';

class JobStudentGroup extends StatefulWidget {
  final String page;
  const JobStudentGroup({Key? key,required this.page}) : super(key: key);

  @override
  State<JobStudentGroup> createState() => _JobStudentGroupState();
}

class _JobStudentGroupState extends State<JobStudentGroup> with CommonMixin {
  late final EasyRefreshController refreshController;
  late String loginName;
  List studentGroups = [];

  @override
  void initState() {
    super.initState();
    refreshController = EasyRefreshController();
    FastData fastData = FastData.getInstance();
    fastData.getUser().then((value) {
      if (value == null || value == '') return;
      Map<String, dynamic> userInfo = json.decode(value);
      setState(() {
        loginName = userInfo['loginName'];
      });
      getStudentGroups();
      print(userInfo);
    });
  }

  void getStudentGroups() async {
    RestClient _client = await getClient();
    BaseStructureResult<List<JobStudentGroups>> res =
        await _client.getJobLevelStudentGroups(loginName);
    setState(() {
      if(res.code == 200){
        studentGroups = res.data!;
      }else{
        studentGroups = [];
      }

    });
    refreshController.finishRefresh();
  }

  void goNextPage(id,title){
    RouterManager.router.navigateTo(context, '${RouterManager.jobPriorityReviewSetPath}?&groupId=$id&title=${Uri.encodeComponent(title)}&page=${widget.page}',transition: getTransition());
  }

  @override
  void dispose() {
    super.dispose();
    refreshController.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(
          '我的学生分组',
          style: TextStyle(fontSize: 16.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: Padding(
        padding: EdgeInsets.only(top: 15.r, left: 14.r, right: 14.r),
        child: EasyRefresh(
          firstRefresh: false,
          taskIndependence: true,
          controller: refreshController,
          header: MaterialHeader(),
          footer: TaurusFooter(),
          onRefresh: () async{
            getStudentGroups();
          },
          child: StudentGroupList(studentGroups,goNextPage),
        ),
      ),
    );
  }
}
