import request from '@/utils/request';
import httpBlob from '@/utils/httpBlob';
import { download } from '@/utils/file';

/**
 * 上传文件
 * @param file 文件
 */
export async function uploadFile(data) {
  const formData = new FormData();
  formData.append('multiPartFile', data.multiPartFile);
  formData.append('module', data.module);
  const res = await request.post('/main/file/upload', formData);
  if (res.data.code === '0') {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 导入文件 批量
 * @param file 文件
 */
export async function importBatch(data, api, onUploadProgressCb, fileKeyName = 'file') {
  const formData = new FormData();
  
  data.multiPartFiles.forEach((item) => {
    formData.append(fileKeyName, item);
  });
  
  Object.keys(data).forEach(key => {
    if (key !== 'multiPartFiles') {
      formData.append(key, data[key]);
    }
  });
  
  const res = await request.post(api, formData, {
    onUploadProgress: onUploadProgressCb ? onUploadProgressCb : () => {}
  });
  if (res.data.code === '0') {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 下载模板
 */
export async function downLoadTemplate(url,name) {
  const res = await request.post(
    url,
    {},
    {
      responseType: 'blob',
    }
  );
  console.log(res.data, '***********');
  download(res.data, name);

}
/**
 * 上传 base64 文件
 * @param base64 文件数据
 * @param fileName 文件名称
 */
export async function uploadBase64File(base64, fileName) {
  const formData = new FormData();
  formData.append('base64', base64);
  if (fileName) {
    formData.append('fileName', fileName);
  }
  const res = await request.post('/file/upload/base64', formData);
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 分页查询文件上传记录
 */
export async function pageFiles(params) {
  const res = await request.get('/file/page', { params });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除文件
 */
export async function removeFile(id) {
  const res = await request.delete('/file/remove/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除文件
 */
export async function removeFiles(data) {
  const res = await request.delete('/file/remove/batch', {
    data
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取文件列表
 */
export async function getFileList(data) {
  const res = await request.post('/main/file/list', data);
  if (res.data.code === '0') {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

// 通过文件名获取文件流
export async function getFile(params, fileName) {
  const res = await request.get('/main/file/getFile', {
    params,
    responseType: 'blob'
  });
  const arr = params.objectName.split('/');
  download(res.data, fileName || arr[arr.length - 1]);
}

// 导入pbom
export async function importPBom(data) {
  const formData = new FormData();
  data.multiPartFiles.forEach((item, index) => {
    formData.append(`multiPartFiles`, item);
  });
  const res = await request.post(`/main/bomCategory/importPBom`, formData);
  if (res.data.code === '0') {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}
/**
 * 导出
 */
export async function exportTable(data) {
  const res = await request.post(
    data.api,
    data.data,
    {
      responseType: 'blob'
    }
  );
  console.log(res.data, '***********');
  download(res.data, data.fileName+'.xlsx');

}
