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/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 {
  const JobStudentGroup({Key? key}) : 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();
  }

  @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(),
        ),
      ),
      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: studentGroups != null && studentGroups.length > 0
              ? isPadFlag?GridView(
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    mainAxisSpacing: 10.r,
                    crossAxisSpacing: 10.r,
                    childAspectRatio: 556 / 112,
                  ),
                  children: List.generate(studentGroups.length, (index) {
                    JobStudentGroups item = studentGroups[index];
                    String classNames = item.classNames.join("  ");
                    return InkWell(
                      onTap: (){
                        RouterManager.router.navigateTo(context, '${RouterManager.jobPriorityReviewSetPath}?&groupId=${item.groupId}',transition: getTransition());
                      },
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: 10.r),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(10.r)),
                          color: Colors.white,
                        ),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            Padding(
                              padding: EdgeInsets.only(right: 8.r),
                              child: Text(
                                item.groupName,
                                style: TextStyle(
                                    fontSize: 10.sp, color: Color(0xFF6888FD)),
                              ),
                            ),
                            Expanded(
                              child: Text(
                                classNames,
                                style: TextStyle(
                                  fontSize: 10.sp,
                                  color: Color(0xFF999999),
                                  overflow: TextOverflow.ellipsis,
                                ),
                                textAlign: TextAlign.end,
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.only(left: 5.r),
                              height: 20.r,
                              width: 55.r,
                              decoration: BoxDecoration(
                                borderRadius:
                                    BorderRadius.all(Radius.circular(20.r)),
                                color: Color(0xFF6888FD),
                              ),
                              child: Center(
                                child: Text(
                                  '设置',
                                  style: TextStyle(
                                      fontSize: 10.sp, color: Colors.white),
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                    );
                  }),
                ):ListView.builder(
            itemBuilder: (context,index){
              JobStudentGroups item = studentGroups[index];
              String classNames = item.classNames.join("  ");
              return Container(
                padding: EdgeInsets.symmetric(vertical:15.r,horizontal: 10.r),
                margin: EdgeInsets.only(bottom: 10.r),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(10.r)),
                  color: Colors.white,
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Padding(
                      padding: EdgeInsets.only(right: 8.r),
                      child: Text(
                        item.groupName,
                        style: TextStyle(
                            fontSize: 14.sp, color: Color(0xFF6888FD)),
                      ),
                    ),
                    Expanded(
                      child: Text(
                        classNames,
                        style: TextStyle(
                          fontSize: 12.sp,
                          color: Color(0xFF999999),
                          overflow: TextOverflow.ellipsis,
                        ),
                        textAlign: TextAlign.end,
                      ),
                    ),
                    InkWell(
                      onTap: (){
                        RouterManager.router.navigateTo(context, '${RouterManager.jobPriorityReviewSetPath}?&groupId=${item.groupId}',transition: getTransition());
                      },
                      child: Container(
                        margin: EdgeInsets.only(left: 5.r),
                        height: 24.r,
                        width: 55.r,
                        decoration: BoxDecoration(
                          borderRadius:
                          BorderRadius.all(Radius.circular(20.r)),
                          color: Color(0xFF6888FD),
                        ),
                        child: Center(
                          child: Text(
                            '设置',
                            style: TextStyle(
                                fontSize: 10.sp, color: Colors.white),
                          ),
                        ),
                      ),
                    )
                  ],
                ),
              );
            },
            itemCount: studentGroups.length,
          )
              : Padding(
                padding: EdgeInsets.only(top: MediaQuery.of(context).size.height/2 - 200.r),
                child: MyEmptyWidget(),
              ),
        ),
      ),
    );
  }
}
