/*
 * @Author       : WuWei
 * @LastEditors  : WuWei
 * @Date         : 2021-05-18 15:43:44
 * @LastEditTime : 2022-01-19 14:40:41
 * @FilePath     : /wisdom-huiguanjia-app/task_package/lib/src/view_models/plan_detail_store.dart
 */

import 'package:app_package/app_package.dart';
import 'package:flutter/widgets.dart';
import '../../src/impl/task_service_impl.dart';
import '../../src/models/task_plan_detail.dart';
import '../../src/models/task_state.dart';
import 'package:wisdom_cli/wisdom_cli.dart';

class PlanTaskDetailStore with ChangeNotifier {
  final BuildContext context;
  late Map args;

  PlanTaskDetailStore(this.context) {
    args =
        ModalRoute.of(context)!.settings.arguments as Map<dynamic, dynamic>? ??
            {};
  }

  final detailScrollviewKey = GlobalKey<WisScrollViewState>();

  final formKey = GlobalKey<FormState>();

  /// 详情id
  String? get id => args['id'];
  String? get executionTime => args['executionTime'];
  String? get limitedTime => args['limitedTime'];
  // bool get isChild => args['isChild'];

  TaskPlanDetailModel? data;

  List<TaskStateModel>? status;

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

  ///合并
  Future getData() async {
    await Future.wait([
      getDetail(),
    ]);
    notifyListeners();
  }

  String? planDateTime;

  ///任务详情
  ///  .pdf, .doc, .docx, .xls, .xlsx, .pptx, .ppt, image/*
  final List<String> fileArr = [
    'pdf',
    'docx',
    'xlsx',
    'xls',
    'doc',
    'pptx',
    'ppt'
  ];
  Future getDetail() async {
    var res = await TaskServiceImpl.instance!
        .getPlanDetail({'id': id, 'isFindOrgan': false});
    if (res.data?.files != null && res.data!.files!.length > 0) {
      res.data!.files!.forEach((item) {
        if (fileArr.indexWhere((element) =>
                element ==
                item.fileName!
                    .substring(item.fileName!.lastIndexOf('.') + 1)) !=
            -1) {
          item.type = 'file';
        } else {
          item.type = 'img';
        }
      });
    }
    data = res.data ?? TaskPlanDetailModel();
    planDateTime = CronUtil.cron2String(res.data!.cronExpression!);
  }

  /// 预览详情图片
  void onShowImage(image) {
    List<ImageProvider> images = [];
    images.add(CachedNetworkImageProvider(AppParseUtil.image(image)));
    WisPhotoViewUtil.show(images, 0);
  }
}
