/*
 * @Author: wangyang 1147192855@qq.com
 * @Date: 2022-07-21 15:14:10
 * @LastEditors: wangyang 1147192855@qq.com
 * @LastEditTime: 2022-07-28 18:12:08
 * @FilePath: \marking_app\lib\pages\marking\progress_abnormal.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:dio/dio.dart';
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/utils/easy_refresh/MyEmptyWidget.dart';
import 'package:marking_app/utils/request/rest_client.dart';
import 'package:marking_app/common/model/common/base_page_data.dart';
import 'package:marking_app/common/model/review/review_item.dart';
import 'package:marking_app/common/model/review/review_page_params.dart';
import 'package:marking_app/components/review_item_view.dart';
import 'package:marking_app/utils/easy_refresh/mixin/refresh_data_handle.dart';

class ProgressAbnormal extends StatefulWidget {
  final String markingId;
  final String name;
  final String questionNum;
  final int examSubjectId;
  const ProgressAbnormal(
      this.examSubjectId, this.markingId, this.name, this.questionNum,
      {Key? key})
      : super(key: key);

  @override
  State<ProgressAbnormal> createState() =>
      _ProgressAbnormalState(markingId, questionNum);
}

class _ProgressAbnormalState extends State<ProgressAbnormal>
    with RefreshDataHandle<ReviewItem, ReviewPageParams>, CommonMixin {
  final ReviewPageParams params;
  _ProgressAbnormalState(String markingId, String questionNum)
      : params = ReviewPageParams(
          qNum: questionNum == '' ? null : questionNum,
          isException: true,
          page: 1,
          limit: 10,
        );

  late final EasyRefreshController controller;
  List<ReviewItem> listData = [];
  void toGetPageData({bool isReFresh = false}) async {
    RestClient client = await getClient();
    BasePageData<ReviewItem>? results = await toRefreshDataWithPath(
      controller,
      api: client.getReviewPage,
      params: params,
      pahtVal: widget.markingId,
      isReFresh: isReFresh,
      context: context,
    );
    if (results != null && results.items.isNotEmpty) {
      if (isReFresh) {
        listData.clear();
        setState(() => listData = results.items);
      } else {
        setState(() => listData.addAll(results.items));
      }
    }
  }

  @override
  void initState() {
    controller = EasyRefreshController();
    super.initState();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('${widget.name}的异常详情'),
      ),
      body: EasyRefresh(
        firstRefresh: true,
        taskIndependence: true,
        enableControlFinishLoad: true,
        enableControlFinishRefresh: true,
        header: MaterialHeader(),
        footer: TaurusFooter(),
        emptyWidget: listData.isEmpty ? const MyEmptyWidget() : null,
        controller: controller,
        child: ListView.builder(
          padding: EdgeInsets.only(left: 16.w, right: 16.w, bottom: 16.h),
          itemBuilder: (context, index) => ReviewItemView(
              int.parse(widget.markingId),
              listData[index],
              exceptional:true,
              widget.examSubjectId),
          itemCount: listData.length,
        ),
        onRefresh: () async => toGetPageData(isReFresh: true),
        onLoad: () async {
          params.page += 1;
          toGetPageData();
        },
      ),
    );
  }
}
