import axios from '@/common/services/axios-instance'
const reqSuffix = '' // '/a.manage'
class NavService {
  // 获取授权信息
  getMenuHiera(params) {
    return axios({
      url: `api/framework/v1/menu/menu-hierarchy-auth`,
      method: 'get',
      params: {
        ...params,
      },
    })
  }

  // 树，获取下级菜单
  getTreeChild(params) {
    return axios({
      url: `api/framework/v1/menu/tree/auth-submenu-items`,
      method: 'get',
      params: {
        ...params,
        productMode: false,
      },
    })
  }

  // 获取菜单列表
  getNavList(params) {
    return axios({
      url: `api/framework/v1/group-manager/list${reqSuffix}`,
      method: 'post',
      data: {
        expressions: [
          {
            name: 'parentId',
            op: 'eq',
            dataType: 'str',
            stringValue: params.id,
          },
        ],
        orderBy: 'weight asc,id asc',
      },
    })
  }

  // 删除菜单
  deleteNav(params) {
    return axios({
      url: `api/framework/v1/menu/menu-items${reqSuffix}`,
      method: 'delete',
      params: {
        ids: params.join(','),
      },
    })
  }

  // 新建菜单
  createNav(params) {
    return axios({
      url: `api/framework/v1/menu/menu-item${reqSuffix}`,
      method: 'post',

      ...params,
    })
  }

  // 保存菜单
  saveNav(params) {
    return axios({
      url: `api/framework/v1/menu/menu-items${reqSuffix}`,
      method: 'put',
      data: [params],
    })
  }

  // 导出
  exportData(params) {
    return axios({
      url: `api/framework/v1/menu/export${reqSuffix}`,
      method: 'get',
      params: {
        ...params,
      },
    })
  }

  // 导入
  importData(params) {
    return axios({
      url: `api/framework/v1/menu/import${reqSuffix}`,
      method: 'post',

      ...params,
    })
  }

  // 排序保存
  reSort(params) {
    return axios({
      url: `api/framework/v1/menu/menu-item-infos${reqSuffix}`,
      method: 'put',

      ...params,
    })
  }

  // 清缓存
  clearCache() {
    return axios({
      url: `api/framework/v1/menu/menu-trees/clear-cache${reqSuffix}`,
      method: 'get',
    })
  }

  // 获取授权情况
  getPermission(params) {
    return axios({
      url: `api/framework/v1/menu/menu-item-permission${reqSuffix}`,
      method: 'get',
      ...params,
    })
  }

  // 授权提交
  savePermission(params) {
    return axios({
      url: `api/framework/v1/menu/menu-item-permissions${reqSuffix}`,
      method: 'put',

      ...params,
    })
  }

  participant = {
    // 获取授权-权限-参与者-组织机构 树根节点

    getTreeRoot(params) {
      return axios({
        url: `api/framework/v1/hiera-manage/group-current-user`,
        method: 'get',
      })
    },

    // 获取授权-权限-参与者-组织机构 下级
    getTreeChild(params) {
      return axios({
        url: `api/framework/v1/group-manager/manager/all/children`,
        method: 'get',
        params: {
          ...params,
          filter: 'G',
          orderBy: 'weight asc',
        },
      })
    },

    getDepList(params) {
      return axios({
        url: `api/framework/v1/group-manager/manager/group/${params.groupId}/search`,
        method: 'post',
        data: {
          searchAction: true,
          expressions: [
            {
              op: 'or',
              expValue: [
                {
                  dataType: 'str',
                  name: 'code',
                  op: 'like',
                  stringValue: '%' + params.text + '%',
                },
                {
                  dataType: 'str',
                  name: 'name',
                  op: 'like',
                  stringValue: '%' + params.text + '%',
                },
              ],
            },
          ],
          startPosition: 1,
          maxResults: 1,
          orderBy: 'weight asc',
        },
      })
    },
  }

  isChildNode(menuId, parentId) {
    return axios.get(`api/framework/v1/menu/${menuId}/is-child/${parentId}`).then((res) => {
      return res.data
    })
  }

  pasteNav(sourceId, targetId, operationType) {
    return axios
      .get(`api/framework/v1/menu/${operationType}-menu-items`, {
        params: {
          ids: sourceId,
          toId: targetId,
        },
      })
      .then((res) => {
        return res.data
      })
  }

  getAllGroups(id) {
    return axios.get(`api/framework/v1/group-manager/group/${id}`).then((res) => {
      return res.data
    })
  }

  getChildrenByGroupId(params) {
    params = {
      filter: 'GU',
      orderBy: 'weight asc',
      ...params,
    }
    return axios
      .get('api/framework/v1/group-manager/manager/all/children', { params })
      .then((res) => {
        return res.data
      })
  }

  queryUsers(params) {
    return axios
      .post('api/framework/v1/user-manager/all/users_query', { ...params })
      .then((res) => {
        return res.data
      })
  }

  changeOwner(params) {
    const { owner, ids } = params
    return axios
      .put(`api/framework/v1/menu/menu-items-owner?owner=${owner}&ids=${ids}`)
      .then((res) => {
        return res.data
      })
  }

  queryPermission(params) {
    const { account, menuId } = params
    return axios
      .get(`api/framework/v1/menu/user/${account}/menu/${menuId}/effective-perms`)
      .then((res) => {
        return res.data
      })
  }

  getRoles(url, params) {
    return axios.post(url, { ...params }).then((res) => {
      return res.data
    })
  }
}

export default new NavService()
