import { defHttp } from '/@/utils/http/axios';
import tool from '/@/utils/Tool';

enum Api {
  GetChildDepartment = '/user-center/department/getChildDepartment',
  GetPositionInfoByDepartmentId = '/user-center/position/getPositionInfoByDepartmentId',
  GetAtpPostTree = '/user-center/post/GetAtpPostTree',
  SavePost = '/user-center/post/SavePost',
  DeletePost = '/user-center/post/DeletePost'
}

/**
 * @description: 获取机构列表
 */

export const structureList = () => {
  return new Promise<void>((resolve, reject) => {
    defHttp.post({ url: Api.GetChildDepartment }).then((res) => {
      var a = tool.structureTree([res.datas]);
      resolve(a[0].children)
    })
  })
};

/**
 * @description: 根据机构id获取下属职位列表
 */
export const getPostsList = (params) => {
  const formData = new FormData();
  Object.keys(params).forEach((key) => formData.append(key, params[key]))
  return new Promise<void>((resolve, reject) => {
    defHttp.post({ url: Api.GetPositionInfoByDepartmentId, params: formData}).then((res) => {
      resolve(res)
    })
  })
};

/**
 * @description: 根据职位id获取下属岗位列表
 */
export const getPositionList = (params) => {
  const formData = new FormData();
  Object.keys(params).forEach((key) => formData.append(key, params[key]))
  return new Promise<void>((resolve, reject) => {
    defHttp.post({ url: Api.GetAtpPostTree, params: formData}).then((res) => {
      resolve(res)
    })
  })
};

/**
 * @description: 保存岗位
 */
export const savePosition = (params) => {
  const formData = new FormData();
  Object.keys(params).forEach((key) => formData.append(key, params[key]))
  return new Promise<void>((resolve, reject) => {
    defHttp.post({ url: Api.SavePost, params: formData}).then((res) => {
      resolve(res)
    })
  })
};

/**
 * @description: 删除岗位
 */
export const delPosition = (params) => {
  const formData = new FormData();
  Object.keys(params).forEach((key) => formData.append(key, params[key]))
  return new Promise<void>((resolve, reject) => {
    defHttp.post({ url: Api.DeletePost, params: formData}).then((res) => {
      resolve(res)
    })
  })
};