/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-03-23 17:05:53
 * @LastEditTime : 2023-06-13 16:59:03
 * @FilePath     : /wisdom-huiguanjia-app/oa_package/lib/src/view_models/management_store.dart
 * @Description  : Do not edit
 */
import 'package:flutter/material.dart';
import 'package:mobx/mobx.dart';
import 'package:oa_package/src/impl/oa_service_impl.dart';
import 'package:oa_package/src/models/task_list_model.dart';
import 'package:oa_package/src/pages/management/index.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
part 'management_store.g.dart';

class ManagementStore = _ManagementStore with _$ManagementStore, ChangeNotifier;

abstract class _ManagementStore with Store {
  ///上下文（请勿修改）
  final BuildContext context1;

  ///路由参数（请勿修改）
  ManagementStoreArguments get arguments =>
      ManagementStoreArguments.fromRouteArguments(context1);

  _ManagementStore(this.context1) {
    ///可在此处理进入页面前的操作，比如响应路由的传参进行赋值。
  }
  final names = [
    {'name': '待处理', 'type': 'waiting'},
    {'name': '我发起的', 'type': 'start'},
    {'name': '已处理', 'type': 'accept'},
    {'name': '抄送我的', 'type': 'cc'},
  ];

  get statistic => arguments.stat;

  /// 点击tab
  @observable
  String taskTabType = 'waiting';
  @action
  Future<void> setindexStack(String type) async {
    taskTabType = type;
    taskParams['pageNum'] = 1;
    viewController.animateTo(.0,
        duration: Duration(milliseconds: 200), curve: Curves.ease);
    onGetTaskTypeList();
  }

  late Map<String, dynamic> taskParams = {
    'pageNum': 1,
    'pageSize': 20,
  };
  final taskScrollViewKey = GlobalKey<WisScrollViewState>();
  ScrollController viewController = new ScrollController();

  /// 获取任务列表
  /// type
  @observable
  ObservableList<TaskListModel> waitingList = ObservableList.of([]);
  ObservableList<TaskListModel> startList = ObservableList.of([]);
  ObservableList<TaskListModel> acceptList = ObservableList.of([]);
  ObservableList<TaskListModel> ccList = ObservableList.of([]);
  @observable
  ObservableList<TaskListModel> taskTypeList = ObservableList.of([]);
  @action
  Future<void> onGetTaskTypeList() async {
    WLoadingUtil.show();
    if (!isListFresh) return;
    try {
      final res = await OaServiceImpl.instance!
          .getTaskTypeList(taskParams, taskTabType);
      final _data = res.data;
      taskParams['pageNum'] == 1
          ? taskTypeList = ObservableList.of(_data?.list ?? [])
          : taskTypeList.addAll(ObservableList.of(_data!.list!));
      switch (taskTabType) {
        case 'waiting':
          waitingList = ObservableList.of(_data?.list ?? []);
          break;
        default:
          break;
      }

      if ((_data?.total ?? 0) == 0) {
        taskScrollViewKey.currentState?.isEmpty();
      } else if ((_data?.pages ?? 0) <= (_data?.pageNum ?? 0) &&
          _data!.total! > 0) {
        taskScrollViewKey.currentState?.isLast();
      } else {
        taskParams['pageNum']++;
      }
    } finally {
      WLoadingUtil.hide();
    }
  }

  @observable
  bool isListFresh = true;
  @action
  void onSetFresh(isFresh) {
    isListFresh = isFresh;
  }

  /// 跳转详情
  Future<void> onToTaskDetail(TaskListModel taskItem) async {
    WNavUtil.instance!.pushNamed(
      'oa/detail',
      arguments: {
        'id': taskItem.orderId,
        'type': taskItem.type,
      },
    ).then((value) => {
          // 返回刷新
          if (value is bool && value)
            {
              taskParams['pageNum'] = 1,
              onGetTaskTypeList(),
            }
        });
  }
}
