import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:making_school_asignment_app/common/job/student_history.dart';
import 'package:making_school_asignment_app/common/job/student_history_params.dart';
import 'package:making_school_asignment_app/common/mixins/request_tool_mixin.dart';

import 'student_work_detail_state.dart';

class StudentWorkDetailLogic extends GetxController with RequestToolMixin, GetSingleTickerProviderStateMixin {
  final StudentWorkDetailState state = StudentWorkDetailState();
  late TabController tabController;
  late final EasyRefreshController refreshController;

  @override
  void onInit() {
    super.onInit();
    state.studentName.value = Get.arguments['studentName'] ?? '';
    state.studentId = Get.arguments['studentId'] ?? -1;
    tabController = TabController(length: 3, vsync: this);
    refreshController = EasyRefreshController();
    EasyLoading.show(status: 'loading...');
    getList();
  }

  void getList() async {
    StudentHistoryParams params = StudentHistoryParams(
      assessType: state.isJob.value ? 0 : 1,
      studentId: state.studentId,
      dateStart: state.dateStart,
      dateEnd: state.dateEnd,
      pageNumber: state.page,
      pageSize: 10,
    );
    StudentHistory data = await getClient().getStudentHistory(params);
    state.studentData.value = data;
    for (var element in state.studentData.value.items.items) {
      int num = 0;
      for (var item in element.kgtList) {
        if (item.state == 0) {
          num = num + 1;
        }
      }
      for (var subject in element.zgtList) {
        if (subject.state == 0) {
          num = num + 1;
        }
      }
      if (num == (element.kgtList.length + element.zgtList.length)) {
        element.allNotDone = true;
      }
    }
    EasyLoading.dismiss();
  }

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