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

enum Api {
    GetChildDepartment = '/user-center/department/getChildDepartment',
    GetPositionInfoByDepartmentId = '/user-center/position/getPositionInfoByDepartmentId',
    EditPositionInfoById = '/user-center/position/editPositionInfoById',
    DeletePositionInfoById = '/user-center/position/deletePositionInfoById'
}

/**
 * @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 savePosts = (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.EditPositionInfoById, params: formData }).then((res) => {
            resolve(res)
        })
    })
};

/**
* @description: 通过ID删除职位
*/
export const delPosts = (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.DeletePositionInfoById, params: formData }).then((res) => {
            resolve(res)
        })
    })
};