import Common from '../../common/Common.js';
export default {
  /**
     * 上传附件
     */
  uploadAttachment: function(formData) {
    return new Promise((resolve, reject) => {
      $.ajax({
        url: Common.getApiURL('file/attachmentUpload'),
        type: 'post',
        async: true,
        beforeSend: function(request) {
          Common.addTokenToRequest(request);
        },
        data: formData,
        contentType: false,
        processData: false,
        success: function(data) {
          resolve(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
          reject();
        },
      });
    });
  },

  /**
     * 获取文件属性
     */
  getFileProperties: function(data) {
    return new Promise((resolve, reject) => {
      $.ajax({
        url: Common.getApiURL(
          'attachmentPanelResource/getFileProperties',
        ),
        type: 'get',
        dataType: 'json',
        data: data,
        beforeSend: function(request) {
          Common.addTokenToRequest(request);
        },
        success: function(data) {
          resolve(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
          Common.processException(
            XMLHttpRequest,
            textStatus,
            errorThrown,
          );
          reject();
        },
      });
    });
  },

  /**
     * 下载附件
     */
  downloadAttachment: function(className, windowNo, recordId, fileName) {
    return new Promise((resolve, reject) => {
      $.ajax({
        url: Common.getApiURL('file/attachmentDownload'),
        type: 'get',
        data: {
          'className': className,
          'recordId': recordId,
          'fileName': fileName,
          'windowNo': windowNo,
        },
        dataType: 'blob', //扩展出了blob类型
        beforeSend: function(request) {
          Common.addTokenToRequest(request);
        },
        success: function(data) {
          resolve(data);                    
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
          Common.processException(
            XMLHttpRequest,
            textStatus,
            errorThrown,
          );
          reject();
        },
      });
    });
  },

  /**
     * 删除附件
     */
  deleteAttachment: function(data) {
    return new Promise((resolve, reject) => {
      $.ajax({
        url: Common.getApiURL(
          'attachmentPanelResource/deleteAttachment',
        ),
        type: 'get',
        data: data,
        beforeSend: function(request) {
          Common.addTokenToRequest(request);
        },
        success: function(data) {
          resolve(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
          reject();
          Common.processException(
            XMLHttpRequest,
            textStatus,
            errorThrown,
          );
        },
      });
    });
  },
};
