/*
 * @Author: ChenYaJin
 * @Date: 2023-01-12 15:10:47
 * @LastEditors: ChenYaJin
 * @LastEditTime: 2023-01-13 10:38:02
 * @Description: 任务动态
 */
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:task_package/src/impl/task_service_impl.dart';
import 'package:task_package/src/models/task_detail.dart';
import 'package:task_package/src/models/task_state.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class DynamicStore extends GetxController {
  final dynamicScrollViewKey = GlobalKey<WisScrollViewState>();
  ScrollController outerController = new ScrollController();

  var taskId = '';
  var status = <TaskStateModel>[].obs;
  var detail = TaskDetailModel().obs;

  onInitData() {
    taskId = Get.arguments['id'];
    if (taskId.isNotEmpty) {
      getDetail();
      getState();
    }
  }

  ///任务动态
  Future<void> getState() async {
    var res = await TaskServiceImpl.instance!.getState({'id': taskId});
    status.value = res.data ?? [];
    status.refresh();
  }

  /// 任务详情
  Future<void> getDetail() async {
    var res = await TaskServiceImpl.instance!.getTaskDetail(taskId);
    detail.value = res.data ?? TaskDetailModel();
    detail.refresh();
  }

  /// 删除动态
  deleteState(item) async {
    final boolean = await WModalUtil.showSync(
      '确定要删除此条动态吗？',
      title: '删除动态',
      cancelText: '取消',
      confirmText: '确认',
    );
    if (boolean == true) {
      final res = await TaskServiceImpl.instance!.deleteTaskState(item.id);
      if (res == 1) {
        WToastUtil.show('删除动态成功');
        getState();
      }
    }
  }

  /// 预览文件
  route2perview(url, name) {
    WisPreviewUtil.show(
      url: url,
      name: name,
    );
  }
}
