import axios from '@/common/services/axios-instance'
class AuditPermissionTreeService {
  getParentOrgInfo(params) {
    return axios({
      url: 'api/xcoa-mobile/v1/iamorg/getManagedHierarchyOrg',
      method: 'post',
      params,
    }).then((res) => {
      res.data.forEach((item) => {
        item.isLeaf = item.leaf
        if (item.props.orgId === null) {
          item.props = { ORG_ID: null, BELONGED_ORG_ID: -1 }
        } else {
          item.props = { ORG_ID: item.props.orgId, BELONGED_ORG_ID: -1 }
        }
      })
      return res
    })
  }

  getPcParentOrgInfo() {
    return axios({
      url: 'api/xcoa-mobile/v1/iamorg/getCurrentUserGroup',
      method: 'get',
    }).then((res) => {
      res.data.isLeaf = true
      res.data.props = { ORG_ID: res.data.id, BELONGED_ORG_ID: -1 }
      res.data.text = res.data.name
      const result = { data: [] }
      result.data.push(res.data)
      return result
    })
  }

  // 业务类型分类树
  getCategoryTree(parentId, params) {
    // url: `api/xcoa-mobile/v1/iam-tree/findTreeNodeById/${params.configId}/${parentId}`,
    return axios({
      url: `api/xcoa-mobile/v1/iamorg/getAuditOrgTreeNodeById/${parentId}`,
      method: 'post',
      params,
    }).then((res) => {
      res.data.forEach((item) => {
        item.isLeaf = item.leaf
        if (item.props.orgId === null) {
          item.props = {}
        } else {
          item.props = { ORG_ID: Number(item.props.orgId), BELONGED_ORG_ID: -1 }
        }
      })
      return res
    })
  }

  // 拖拽功能
  dragNode(params) {
    return axios({
      url: `api/xcoa-mobile/v1/iam-tree/moveTreeNode/${params.configId}/${params.id}/${params.targetId}/${params.position}`,
      method: 'post',
      data: null,
    }).then((res) => {
      res.data.forEach((item) => (item.isLeaf = item.leaf))
      return res
    })
  }

  // 地址树搜索功能
  groupsQuery(data) {
    return axios({
      url: `api/xcoa-mobile/v1/iamorg/groups_query`,
      method: 'post',
      params: data,
    }).then((res) => {
      res.data.forEach((item) => {
        item.isLeaf = item.leaf
        item.name = item.text
        item.oldId = item.id
        item.props.ORG_ID = item.props.orgId
        if (item.props.ORG_ID !== null) {
          item.code = item.props.ORG_ID.toString()
          item.id = item.props.ORG_ID
        } else {
          item.code = item.id.toString()
        }

        item.type = 'Group'
        item.checkable = true
      })
      return res.data
    })
  }
}
export default new AuditPermissionTreeService()
