import Common from '../../common/Common.js';
import { Uuid } from 'pc-component-v3';
import store from '../../store/index.js';
/**
 * 文件下载服务
 */
export default {
  /**
     * POST 方式下载文件
     * @param {http请求的地址} url 
     * @param {请求参数对应的FORM表单} formData 
     * @param {导出文件的文件名} fileName 
     */
  postDownloadFile: function (url, data, fileName) {
    var taskId = Uuid.createUUID();
    //将”+”号转换为”%2B”
    var item = {
      url: url,
      method: 'POST',
      contentType: 'application/x-www-form-urlencoded',
      data: data,
      taskId: taskId,
      fileName: fileName,
      status: '',
      isStart: false,
    };
    store.commit('addCount', item);
  },

  /**
  * GET 方式下载文件
  * @param {*} url 
  * @param {*} fileName 
  */
  downloadFile: function (url, fileName) {
    var taskId = Uuid.createUUID();
    //将”+”号转换为”%2B”
    var item = {
      taskId: taskId,
      fileName: fileName,
      status: '',
      isStart: false,
      url: url,
    };
    store.commit('addCount', item);
  },

  /**
  * 文件下载
  * @param {Object} className 类名称
  * @param {Object} fileName 文件名称
   * @author GuoZhiBo 20200409
  */
  fileDownload: function (className, fileName) {
    var downloadUrl = Common.getApiURL('file/fileDownload') + '?className=' + className
      + '&fileName=' + window.encodeURIComponent(fileName);
    this.downloadFile(downloadUrl, fileName);
  },

  /**
  * 图片下载
  * @param {Object} className 类名称
  * @param {Object} fileName 文件名称
  * @author GuoZhiBo 20200409
  */
  imageDownload: function (className, fileName) {
    var downloadUrl = Common.getApiURL('file/imageDownload') + '?className=' + className
      + '&fileName=' + window.encodeURIComponent(fileName);
    this.downloadFile(downloadUrl, fileName);
  },

  /**
  * 视屏下载
  * @param {Object} className 类名称
  * @param {Object} fileName 文件名称
  * @author GuoZhiBo 20200409
  */
  videoDownload: function (className, fileName) {
    var downloadUrl = Common.getApiURL('file/videoDownload') + '?className=' + className
      + '&fileName=' + window.encodeURIComponent(fileName);
    this.downloadFile(downloadUrl, fileName);
  },

  /**
  * 缩略图下载
  * @param {Object} className 类名称
  * @param {Object} fileName 文件名称
  * @author GuoZhiBo 20200409
  */
  thumbnailImageDownload: function (className, fileName) {
    var downloadUrl = Common.getApiURL('file/thumbnailImageDownload') + '?className=' + className
      + '&fileName=' + window.encodeURIComponent(fileName);
    this.downloadFile(downloadUrl, fileName);
  },

  /**
  * 报表下载
  * @param {Object} fileName
  * @author GuoZhiBo 20200410
  */
  reportDownload: function (fileName) {
    var downloadUrl = Common.getApiURL('file/reportDownload') + '?fileName=' + window.encodeURIComponent(fileName);
    this.downloadFile(downloadUrl, fileName);
  },


  /**
  * audio下载
  * @param {Object} fileName
  * @author GuoZhiBo 20200527
  */
  audioDownload: function (fileName) {
    var downloadUrl = Common.getApiURL('file/audioDownload') + '?fileName=' + window.encodeURIComponent(fileName);
    this.downloadFile(downloadUrl, fileName);
  },
};