/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-03-15 15:36:56
 * @LastEditTime : 2023-05-18 15:58:53
 * @FilePath     : /wisdom-huiguanjia-app/oa_package/lib/src/view_models/index_store.dart
 * @Description  : Do not edit
 */
import 'package:flutter/material.dart';
import 'package:mobx/mobx.dart';
import 'package:app_package/app_package.dart';
import 'package:oa_package/src/impl/oa_service_impl.dart';
import 'package:oa_package/src/models/process_model.dart';
import 'package:oa_package/src/models/stat_model.dart';
import 'package:oa_package/src/models/oa_menu_list_model.dart';
import 'package:oa_package/src/pages/index/index.dart';
import 'package:oa_package/src/pages/management/index.dart';
import 'package:wisdom_cli/wisdom_cli.dart';
part 'index_store.g.dart';

class IndexStore = _IndexStore with _$IndexStore, ChangeNotifier;

abstract class _IndexStore with Store {
  _IndexStore(this.context1) {
//可在此处理进入页面前的操作，比如响应路由的传参进行赋值。
  }

  ///上下文（请勿修改）
  final BuildContext context1;

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

  final scrollViewKey = GlobalKey<WisScrollViewState>();
  final appGlobalStore = GetIt.I.get<AppGlobalStore>();

  /// 首页OA菜单
  @observable
  ObservableList<OaMenuListModel> oaMenuList = ObservableList.of([]);
  @action
  Future<void> getOaMenuList() async {
    final res = await OaServiceImpl.instance!.getOaMenuList();
    oaMenuList = ObservableList.of(res.data!);
  }

  ///个性化设置OA菜单
  @observable
  ObservableList pageMenuList = ObservableList.of([]);
  @observable
  var pageDetail;

  Future getSettingMenuList() async {
    var res = await appGlobalStore.getPageSetting('512672712577089540');
    pageMenuList = ObservableList.of(res?.groupData ?? []);
    pageDetail = res;
  }

  ///列表数据
  @observable
  ObservableList<ProcessModel> processList = ObservableList.of([]);
  @action
  Future<void> getList() async {
    final res = await OaServiceImpl.instance!.getProcessList();
    processList = ObservableList.of(res.data ?? []);
  }

  ///统计
  @observable
  StatModel? stat;
  @observable
  num waitingCount = 0;
  @observable
  num startCount = 0;
  @observable
  num acceptCount = 0;
  @action
  Future<void> getStat() async {
    final res = await OaServiceImpl.instance!.getStatistic();
    stat = res.data;
    waitingCount = stat?.waitingCount ?? 0;
    startCount = stat?.startCount ?? 0;
    acceptCount = stat?.acceptCount ?? 0;
  }

  ///整合在一起请求
  Future<void> getData() async {
    if (appGlobalStore.isPageSet == true) {
      await Future.wait([
        getSettingMenuList(),
        getStat(),
      ]);
    } else {
      await Future.wait([getOaMenuList(), getStat()]);
    }
  }

  void toSettingProcess(item) {
    WNavUtil.instance!.pushNamed(
      'oa/process',
      arguments: {
        'id': '${item.id}',
        'title': '${item.name}',
      },
    ).then((value) => {
          // 返回刷新
          getData(),
        });
  }

  void toProcess(item, index) {
    WNavUtil.instance!.pushNamed(
      'oa/process',
      arguments: {
        'id': '${item.oaMenus[index].processId}',
        'title': '${item.oaMenus[index].processName}',
      },
    ).then((value) => {
          // 返回刷新
          getData(),
        });
  }

  /// 跳转到审批管理列表
  void toManagement(index) {
    WNavUtil.instance!
        .pushNamed(
          'oa/management',
          arguments: ManagementStoreArguments(
            indexStack: index,
            stat: stat,
          ).toJson(),
        )
        .then((value) => {
              // 返回刷新
              getData(),
            });
  }
}
