/*
 * @Author: XianKaiQun
 * @LastEditors  : WuWei
 * @Date: 2020-11-26 14:21:19
 * @LastEditTime : 2022-01-19 11:16:09
 */
import 'package:app_package/app_package.dart';
import 'package:oa_package/src/models/drafts_model.dart';
import 'package:oa_package/src/models/task_detail_model.dart';
import 'package:oa_package/src/models/task_list_model.dart';
import "package:oa_package/src/service/oa_service.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/models/menu_detail_model.dart';
import 'package:oa_package/src/models/flows_detail_model.dart';
import 'package:oa_package/src/models/tast_history_list_model.dart';

class OaServiceImpl implements OaIndexService {
  static OaServiceImpl? get instance => _getInstance();
  static OaServiceImpl? _instance;
  OaServiceImpl._internal() {
    // 初始化
  }
  static OaServiceImpl? _getInstance() {
    if (_instance == null) {
      _instance = new OaServiceImpl._internal();
    }
    return _instance;
  }

  static String prefix = '/app/admin/oa';

  ///流程列表
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/11507
  Future<ResModel<List<ProcessModel>?>> getProcessList() async {
    var res = await HttpUtil.instance.get(
      '/app/admin/oa/process',
    );

    return ResModel<List<ProcessModel>?>(
      json: res.data,
      to: (v) => (v ?? []).map((f) => f.toJson()).toList(),
      from: (v) =>
          (v ?? []).map<ProcessModel>((f) => ProcessModel.fromJson(f)).toList(),
    );
  }

  /// 流程统计
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/13892
  Future<ResModel<StatModel>> getStatistic() async {
    var res = await HttpUtil.instance.get('/app/admin/oa/statistic');

    return ResModel<StatModel>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => StatModel.fromJson(v),
    );
  }

  /// 流程菜单
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/13892
  Future<ResModel<List<OaMenuListModel>?>> getOaMenuList() async {
    var res = await HttpUtil.instance.get(
      '$prefix/menu',
    );

    return ResModel<List<OaMenuListModel>?>(
      json: res.data,
      to: (v) => (v ?? []).map((f) => f.toJson()).toList(),
      from: (v) => (v ?? [])
          .map<OaMenuListModel>((f) => OaMenuListModel.fromJson(f))
          .toList(),
    );
  }

  /// 表单详情
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/14333
  /// data {id:}
  Future<ResModel<MenuDetailModel>> getMenuFormDetail(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/flows/start/forms/${data['id']}',
    );

    return ResModel<MenuDetailModel>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => MenuDetailModel.fromJson(v),
    );
  }

  /// 流程详情
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/14288
  /// data {id:}
  Future<ResModel<FlowsDetailModel>> getMenuFlowsDetail(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/flows/${data['id']}',
    );

    return ResModel<FlowsDetailModel>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => FlowsDetailModel.fromJson(v),
    );
  }

  /// 发起任务列表
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/13955;
  /// data.processName	否	流程名称;
  /// data.createDateStart	否	发起日期start;
  /// data.createDateEnd	否	发起日期end;
  /// data.processId	否	流程ID（只查询某个流程下的实例）;
  Future<ResModel<PagingModel<TaskHistoryListModel>>> getTaskHistoryList(
      data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/tasks/start/list',
      queryParameters: data,
    );

    return ResModel<PagingModel<TaskHistoryListModel>>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => PagingModel(
        json: v,
        to: (v) => v.toJson(),
        from: (v) => TaskHistoryListModel.fromJson(v),
      ),
    );
  }

  ///任务类型列表
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/13955
  Future<ResModel<PagingModel<TaskListModel>>> getTaskTypeList(
      data, String type) async {
    var res = await HttpUtil.instance.get(
      '$prefix/tasks/$type/list',
      queryParameters: data,
    );

    return ResModel<PagingModel<TaskListModel>>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => PagingModel(
        json: v,
        to: (v) => v.toJson(),
        from: (v) => TaskListModel.fromJson(v),
      ),
    );
  }

  ///流程实例详情（历史、活跃）
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/13946
  ////app/admin/oa/orders/{type}/{id}/detail
  Future<ResModel<TaskDetailModel>> getTaskDetaiil(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/orders/${data['type']}/${data['id']}/detail',
      queryParameters: data,
    );

    return ResModel<TaskDetailModel>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => TaskDetailModel.fromJson(v),
    );
  }

  ///流程任务处理（通过、驳回、提交表单）
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/13937
  Future<bool?> patchTaskDetail(data, content) async {
    var res = await HttpUtil.instance.patch(
      '$prefix/task/${data['id']}/${data['action']}',
      data: content,
    );

    if (res.data['data'] != null && res.data['data'] is bool) {
      return res.data['data'];
    } else {
      return false;
    }
  }

  ///流程任务驳回
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/13937
  Future<bool?> patchTaskTerminate(data) async {
    var res = await HttpUtil.instance.patch(
      '$prefix/orders/${data['id']}/terminate',
    );

    if (res.data['data'] != null && res.data['data'] is bool) {
      return res.data['data'];
    } else {
      return false;
    }
  }

  ///流程发起
  ///http://yapi.wisdomcity.com.cn/project/40/interface/api/13919
  Future<String?> postFlowsStart(id, data) async {
    var res = await HttpUtil.instance.post(
      '$prefix/flows/$id/start',
      data: data,
    );
    if (res.data['data'] != null && res.data['data'] is String) {
      return res.data['data'];
    } else {
      return null;
    }
  }

  /// 获取流程草稿
  /// http://yapi.wisdomcity.com.cn/project/40/interface/api/13910

  Future<ResModel<DraftsModel>> getDraftsDetail(data) async {
    var res = await HttpUtil.instance.get(
      '$prefix/drafts/${data['flowId']}/${data['formId']}',
      // queryParameters: data,
    );

    return ResModel<DraftsModel>(
      json: res.data,
      to: (v) => v.toJson(),
      from: (v) => DraftsModel.fromJson(v),
    );
  }
}
