import axios from 'axios'
import {
  getToken
} from '@/utils/auth'

var uploadUrl = process.env.VUE_APP_BASE_API + "/sysFiles/upload"
var uploadV1Url = process.env.VUE_APP_BASE_API + "/sysFiles/v1/upload"
var importZipUrl = process.env.VUE_APP_BASE_API + "/bizImport/importZip"
/**
 * 封装上传文件的post方法
 * @param data
 * @returns {Promise}
 */
export function uploadFile(data) {
  return new Promise((resolve, reject) => {
    axios.post(uploadUrl, data, {
      headers: {
        'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime(),
        'authorization': getToken(),
      }
    }).then(response => {
      resolve(response.data)
    }).catch(error => {
      reject(error)
    })
  })
}


/**
 * 封装上传文件方法 支持自定义Formdata 的key
 * @param data
 * @returns {Promise}
 */
export function uploadV1(data) {
  return new Promise((resolve, reject) => {
    axios.post(uploadV1Url, data, {
      headers: {
        'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime(),
        'authorization': getToken(),
      }
    }).then(response => {
      resolve(response.data)
    }).catch(error => {
      reject(error)
    })
  })
}

/**
 * 批量上传文件 
 * @param data   formData类型 * 参数1 type:导入数据类别(biz_cultural_relic、biz_exhibition二选一) 参数2 zipFile:zip压缩文件
 * @param callback  用于获取上传进度
 * @param cancelCallBack  用于取消上传
 * @returns {Promise}
 */
let CancelToken = axios.CancelToken;
export function importZip(data, callback, cancelCallBack) {
  return new Promise((resolve, reject) => {
    axios.post(importZipUrl, data, {
      headers: {
        'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime(),
        'authorization': getToken(),
      },
      onUploadProgress(progressEvent) {
        if (progressEvent.lengthComputable) {
          callback(progressEvent);
        }
      },
      cancelToken: new CancelToken(function executor(c) {
        cancelCallBack(c)
      })
    }).then(response => {
      resolve(response)
    }).catch(error => {
      reject(error)
    })
  })
}


/**
 * 下载文件 
 * @param href 下载地址
 * @param dowmloadName 下载文件用户看到的名称
 */
export function downloadFile(href, dowmloadName) {
  let a = document.createElement("a");
  a.href = href;
  a.download = dowmloadName;
  a.style.display = "none";
  document.body.appendChild(a);
  a.click();
  a.remove();
}

/**
 * 预览文件 
 * @param href 预览地址
 * @param previewName 预览文件用户看到的名称
 */
export function previewFile(href, previewName) {
  let a = document.createElement("a");
  a.href = href;
  a.target = '_blank'
  a.download = previewName;
  a.style.display = "none";
  document.body.appendChild(a);
  a.click();
  a.remove();
}


/**
 * 下载文件 
 * @param url 下载地址
 * @param fileName 下载文件用户看到的名称
 * @param typeSuffix 文件后缀
 */
export function downloadBlob(url, fileName, typeSuffix) {
  axios({
    url,
    method: 'get',
    responseType: 'blob',
    headers: {
      'authorization': getToken()
    }
  }).
  then(response => {
    const blob = new Blob([response.data])
    const link = document.createElement('a')
    link.href = URL.createObjectURL(blob)
    if (typeSuffix) {
      link.download = fileName + '.' + typeSuffix
    } else {
      link.download = fileName
    }
    link.click()
    URL.revokeObjectURL(link.href)
  }).catch(err => {
    console.error(err);
  })
};
