import Request from '../..'

enum Api {
  DeptTree = '/workspace/dept/tree',
  DeptCreate = '/workspace/dept/create',
  DeptUpdate = '/workspace/dept/update',
  DeptDetail = '/workspace/dept/details',
  DeptDelete = '/workspace/dept/del',
  AllDeptUser = '/workspace/dept/org',
  SearchUser = '/workspace/dept/org/user/search'
}

export function fetchTree(deptName?: string) {
  return Request.get<API.DeptTreeResult>({
    url: Api.DeptTree,
    params: {
      deptName,
      parentId: -1
    }
  })
}

export function addObj(data: API.AddDeptParams) {
  return Request.post({
    url: Api.DeptCreate,
    data
  })
}

export function putObj(data: API.DeptParams) {
  return Request.post({
    url: Api.DeptUpdate,
    data
  })
}

export function getObj(id: number | string) {
  return Request.get<API.DeptTreeItem>({
    url: Api.DeptDetail + `/${id}`
  })
}

export function delObj(id: number | string) {
  return Request.post({
    url: Api.DeptDelete,
    data: { deptId: id, deleteType: 'SOFT' }
  })
}

// 查询全部部门（包含用户）
export function getAllDeptAndUser(parentDeptId: number | string = -1, type: string) {
  return Request.get<API.AllDeptUser>({
    url: Api.AllDeptUser,
    params: { parentDeptId, type }
  })
}

// 模糊搜索用户
export function userSearch(query: { username: string }) {
  return Request.get<API.Employee[]>({
    url: Api.SearchUser,
    params: query
  })
}
