import request from '@/utils/request';

/**
 * 查询文件夹分类数据
 * @data data
 */
export async function getDocTreeListAPI(data) {
  const res = await request.post('/fm/directory/selectTreeList', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询文件数据
 * @data data
 */
export async function filePageAPI(data) {
  const res = await request.post('/fm/file/pageBusiness', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 保存
 * @data data
 */
export async function fileSaveAPI(data) {
  const res = await request.post('/fm/file/save', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 详情
 * @id id
 */
export async function fileGetByIdAPI(id) {
  const res = await request.get(`/fm/file/getById/${id}`);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * ids查询文件列表
 * @data data
 */
export async function queryIds(data) {
  const res = await request.post('/fm/file/queryIds', data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 获取编码树
export async function selectTreeList(data) {
  const res = await request.post(
    `/main/business_code_category/selectTreeList`,
    data
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 获取右侧列表
export async function listParentId(data) {
  const res = await request.post(
    `/main/business_code_category/listPageParentId`,
    data
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 获取右侧码段列表
export async function pageSegment(data) {
  const res = await request.post(`/main/business_code_manage/page`, data);
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 生成编码
export async function getCode(id) {
  const res = await request.get(
    `/main/business_code_category/applyForCode/` + id
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 上传文件
 * @param file 文件
 */
export async function uploadFileNew(data) {
  const formData = new FormData();
  formData.append('multiPartFile', data.multiPartFile);
  formData.append('module', data.module);
  const res = await request.post('/main/file/uploadFile', formData);
  if (res.data.code === '0') {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}
// 编码方案
export async function listCode(data = {}) {
  const res = await request.get(
    `/main/business_code_category/listCode/${data.id || '0'}` +
      '?parentId=' +
      (data.parentId || '')
  );
  if (res.data.code == 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
