/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-07-22 16:48:16
 * @LastEditTime : 2023-05-25 15:52:16
 * @Description  : 任务大厅
 */


import 'package:flutter/material.dart';
import 'package:mobx/mobx.dart';
import 'package:task_package/src/impl/qrcode_impl.dart';
import 'package:task_package/src/impl/task_service_impl.dart';
import 'package:task_package/src/pages/hall/index.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

import '../models/task_inventory_tree_model.dart';
part 'hall_store.g.dart';

class HallPageStore = _HallPageStore with _$HallPageStore, ChangeNotifier;

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

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

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

  ScrollController viewController0 = new ScrollController();
  GlobalKey dropdownKey1 = new GlobalKey();

  final hallScrViewKey = GlobalKey<WisScrollViewState>();

  Map<String, dynamic> hallTaskPrs = {
    'pageNum': 1,
    'pageSize': 20,
  };

  // 搜索标题
  @observable
  String? taskTitle;

  /// 获取任务类型
  @observable
  List<TaskInventoryTreeModel> taskItemList = [];
  // ObservableList<TaskInventoryModel> taskItemList = ObservableList.of([]);
  @action
  Future onGetTaskListItem() async {
    var res =
        await TaskServiceImpl.instance!.getTaskInventoryTree({"preset": null});
    taskItemList = res.data ?? [];
  }

  @observable
  List checkListItem = [];
  @action
  onChooseListItem(data) {
    checkListItem = data;
    onInitList();
  }

  @observable
  ObservableList taskList = ObservableList.of([]);

  Future<void> onInitList() async {
    hallTaskPrs['pageNum'] = 1;
    viewController0.animateTo(0,
        duration: Duration(milliseconds: 200), curve: Curves.ease);
    hallScrViewKey.currentState?.refreshTrigger();
    onGetList();
  }

  Future<void> onGetList() async {
    var res = await QrcodeServiceImpl.instance!.getTaskList({
      ...hallTaskPrs,
      'inventoryIds': checkListItem.length > 0
          ? checkListItem.map((item) => item.id).join(',')
          : null,
      'title': taskTitle,
      'type': 'wait_receive',
    });
    final _data = res.data;
    hallTaskPrs['pageNum'] == 1
        ? taskList = ObservableList.of(_data?.list ?? [])
        : taskList.addAll(ObservableList.of(_data!.list!));

    if ((_data?.total ?? 0) == 0) {
      hallScrViewKey.currentState?.isEmpty();
    } else if ((_data?.pages ?? 0) <= (_data?.pageNum ?? 0) &&
        _data!.total! > 0) {
      hallScrViewKey.currentState?.isLast();
    } else {
      hallTaskPrs['pageNum']++;
    }
  }

  /// 根据标题搜索任务
  @action
  Future onSearchTaskTitles(title) async {
    FocusScope.of(context1).requestFocus(FocusNode());
    taskTitle = title;
    onInitList();
  }

  /// 跳转详情
  toTaskDetail(item) async {
    await Navigator.pushNamed(
      context1,
      'task/detail',
      arguments: {
        'id': item.id,
        'isChild': item.isChild,
      },
    );
    onInitList();
  }
}
