import axios from '@/common/services/axios-instance'
import PageService from '@/common/services/page-service'
class BusinesstypeService {
  // 获取业务类型管理树
  getYwlxTree(params) {
    return axios({
      url: 'api/framework/v1/we-app-business-type-group/businesstype-tree',
      method: 'get',
      params,
    })
  }

  getYwlxList(parentCatId, status = 1, maxResults = -1) {
    const params = {
      startPosition: 0,
      maxResults: maxResults,
      expressions: [
        {
          dataType: 'int',
          name: 'status',
          op: 'eq',
          intValue: status, // 启用的
        },
        {
          dataType: 'str',
          name: 'categoryId',
          op: 'eq',
          stringValue: parentCatId,
        },
      ],
      formId: 'oaBaseBusinessType',
      orderBy: 'creationTime asc',
      columns: 'id,businessTypeName',
    }
    return PageService.getList(params).then((res) => {
      return res.data
    })
  }

  // 新的获取业务类型子节点的接口，支持权限控制
  getYwlxListByCode(params) {
    return axios
      .get('api/framework/v1/businesstype-manage/find-businesstypes-by-catcode', {
        params,
      })
      .then((res) => {
        return res
      })
  }

  // 流程
  getFlowsList(params) {
    return axios({
      url: `api/framework/v1/flow-manage/flows/list`,
      method: 'get',
      params,
    })
  }

  // 获取红头模板
  getRedhead(params) {
    return axios({
      url: `api/framework/v1/redhead-manager/doctype/redheads/${params}`,
      method: 'get',
    })
  }

  // 获取类型
  getCategoryName() {
    return axios({
      url: `api/framework/v1/doc/businesstype-manage/businesstypes/list-authorized`,
      method: 'get',
    })
  }

  // 业务类型分类树
  getCategoryTree(params) {
    return axios({
      url: 'api/framework/v1/businesstype-manage/categories/categoryTree',
      method: 'get',
      params,
    }).then((res) => {
      res.data.forEach((item) => (item.isLeaf = item.leaf))
      return res
    })
  }

  // 获取级别
  getOrganizeLevel() {
    const params = {
      expressions: [
        {
          dataType: 'str',
          name: 'type',
          op: 'eq',
          stringValue: 'G',
        },
      ],
      startPosition: 0,
      maxResults: -1,
    }
    return axios.post('api/framework/v1/level/levels', params).then((res) => {
      return res.data
    })
  }

  getFlowFormModelRelationInfoId(id) {
    return axios
      .get(`api/framework/v1/flow-form-model-relation-v2/flow-relations/${id}?flowVer=`)
      .then((res) => {
        res.data.forEach((item) => {
          item.label = item.name
          item.value = item.id
        })
        return res.data
      })
  }

  getYwlxCategoryTree(params) {
    return axios({
      url: 'api/framework/v1/we-app-business-type-group/tree',
      method: 'get',
      params: params,
    })
  }

  getYwlxFormCategoryTree(params) {
    return axios({
      url: 'api/framework/v1/we-app-business-type-group/app-module/tree',
      method: 'get',
      params: params,
    })
  }

  getDefaultFlow(params) {
    return axios({
      url: 'api/framework/v1/we-app-flow-design/default-flow',
      method: 'get',
      params: params,
    })
  }

  // 查询业务类型
  searchBusiType(params) {
    return axios.post(
      'api/framework/v1/we-app-business-type-group/businesstype-tree-search-box',
      params
    )
  }
}

export default new BusinesstypeService()
