/*
 * @Author: ChenYaJin
 * @Date: 2023-01-13 14:39:24
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-01-16 14:40:54
 * @Description: 任务筛选页
 */

import 'package:get/get.dart';
import 'package:task_package/src/impl/task_service_impl.dart';
import '../models/task_inventory_tree_model.dart';
class FilterStore extends GetxController {
  var statusList = [
    {'code': '', 'name': '全部'},
    {'code': 'no_open', 'name': '未开始'},
    {'code': 'doing', 'name': '进行中'},
    {'code': 'finished', 'name': '已完成'},
    {'code': 'outTime', 'name': '超时'},
    {'code': 'overdue', 'name': '逾期'},
  ];
  var timeList = [
    {'code': '', 'name': '全部'},
    {'code': 'asc', 'name': '时间升序'},
    {'code': 'desc', 'name': '时间降序'},
  ];
  var taskTypeList = [
    {'code': 'to_me', 'name': '我的任务'},
    {'code': 'create', 'name': '我创建的'},
    {'code': 'transfer', 'name': '抄送我的'},
    {'code': 'organ', 'name': '本部门'},
    {'code': 'me_transfer', 'name': '我转派的'},
    {'code': 'to_others', 'name': '我指派的'},
  ];

  /// 任务状态
  var filterStatusCode = ''.obs;

  /// 时间排序
  var sortCreatedAt = ''.obs;

  /// 任务分类
  var type = 'to_me'.obs;
  var taskItemList = [].obs;
  var taskItemSecondList = [].obs;

  /// 任务类型
  var inventoryFirstId = ''.obs;
  var inventoryIds = [].obs;
  var inventoryItem = TaskInventoryTreeModel().obs;

  bool get isFilterStatus => true;

  onInitData() {
    onGetTaskListItem();
    final codeArg = Get.arguments['code'];
    final typeArg = Get.arguments['type'];
    // final inventorFirstIdArg = Get.arguments['inventoryFirstId'];
    // final inventorySecondIdArg = Get.arguments['inventoryIds'] ?? [];
    filterStatusCode.value = codeArg;
    type.value = typeArg;
    // inventoryFirstId.value = inventorFirstIdArg;
    // inventoryIds.value = inventorySecondIdArg;
    // inventoryIds.refresh();
  }

  void setInventoryFirstItem(item) {
    inventoryItem.value = item;
  }

  void onGetTaskListItem() async {
    if (taskItemList.length != 0) {
      return;
    }
    var res =
        await TaskServiceImpl.instance!.getTaskInventoryTree({"preset": null});
    // var res =
    //     await TaskServiceImpl.instance!.getInventoryList({"preset": null});

    taskItemList.value = res.data ?? [];
    taskItemList.refresh();
  }

  void back() {
    final params = [
      {
        'isRefresh': false,
      }
    ];
    backByParams(params);
  }

  void backByParams(data) {
    Get.back(result: data);
  }

  /// 选择任务状态
  void changeStateValue(String code) {
    filterStatusCode.value = code;
  }

  /// 选择时间排序
  void changeSortValue(String code) {
    sortCreatedAt.value = code;
  }

  /// 选择任务分类
  void changeTypeValue(String code) {
    type.value = code;
  }

  /// 选择任务类型
  void changeFirstInventoryValue(String code) {
    inventoryFirstId.value = code;
  }

  void changeSecondInventoryValue (String code) {
    // inventoryIds.value = code;
    if (inventoryIds.contains(code)) {
      inventoryIds.remove(code);
    }else {
      inventoryIds.add(code);
    }
    inventoryIds.refresh();
  }

  void setInventorySecondList(list) {
    taskItemSecondList.value = list;
    if (list.length == 0) {
      inventoryIds.value = [];
      inventoryIds.refresh();
    }
    // inventoryIds.value = list.map((i) => i.id).toList();
    // inventoryIds.refresh();
    taskItemSecondList.refresh();
  }

  onResetInner() {
    changeFirstInventoryValue('');
    changeSecondInventoryValue('');
    setInventorySecondList([]);
    changeSortValue('');
    changeStateValue('');
    changeTypeValue('to_me');
    final params = 
      {
        'code': '',
        'sortCreatedAt': '',
        'type': 'to_me',
        'inventoryFirstId': null,
        'inventoryIds': [],
        'isRefresh': true,
      }
    ;
    backByParams(params);
  }

  submit() {
    final params = 
      {
        'code': filterStatusCode.value,
        'sortCreatedAt': sortCreatedAt.value,
        'type': type.value,
        'inventoryfirstId': inventoryFirstId.value,
        'inventoryIds': inventoryIds,
        'isRefresh': true,
      }
   ;
    backByParams(params);
  }
}
