import { axios } from '@/utils/request'
import { stringify } from 'qs'

/**
 * user page func
 * parameter: {
 *     keyword: '',
 * }
 * @param parameter
 * @returns {*}
 */
export function getDeptTree (parameter) {
  return axios({
    url: `/upms/depts/tree?${stringify(parameter)}`,
    method: 'get',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

/**
 * user page func
 * parameter: {
 *     keyword: '',
 * }
 * @param parameter
 * @returns {*}
 */
export function getDeptTreeWithoutRoot (parameter) {
  return axios({
    url: `/upms/depts/treeWithoutRoot?${stringify(parameter)}`,
    method: 'get',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

export function queryDept (parameter) {
  return axios({
    url: `/upms/depts?${stringify(parameter)}`,
    method: 'get',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

export function addDept (parameter) {
  return axios({
    url: `/upms/depts`,
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json;charset=UTF-8'
    },
    data: parameter
  })
}

export function updateDept (parameter) {
  return axios({
    url: `/upms/depts/${parameter.deptId}`,
    method: 'PUT',
    data: parameter
  })
}

export function fetchDept (parameter) {
  return axios({
    url: `/upms/depts/${parameter.deptId}`,
    method: 'get',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

export function getDeptsAllByParentId (parameter) {
  return axios({
    url: `/upms/depts/all/child/${parameter.deptId}?${stringify(parameter)}`,
    method: 'get',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

export function deleteDept (parameter) {
  return axios({
    url: `/upms/depts/${parameter.deptId}`,
    method: 'DELETE',
    data: parameter
  })
}

/**
 * user page func
 * parameter: {
 *     keyword: '',
 * }
 * @param parameter
 * @returns {*}
 */
export function getDeptAllocateTree (parameter) {
  return axios({
    url: `/upms/depts/allocate/${parameter.allocateType}?${stringify(parameter)}`,
    method: 'get',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

/**
 * add func
 * parameter: { }
 * @param parameter
 * @returns {*}
 */
export function importDept (id, file) {
  return axios({
    url: '/upms/depts/import/' + id,
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    data: file
  })
}
