import request from '@/utils/request'
import newRequest from '@/utils/new-request'
import dsRequest from '@/utils/dsRequest'
import { Loading, Message } from 'element-ui'
import { saveAs } from 'file-saver'
function createParam(url, data, method) {
  return {
    method: method || 'get',
    url: url, // 请求地址
    responseType: 'blob',
    timeout: 300000,
    data
    // 表明返回服务器返回的数据类型
  }
}

export function downloadFile(url, params) {
  const form = document.createElement('form')
  form.id = 'downloadform'
  form.style.display = 'none' // 将表单隐藏
  form.target = ''
  form.method = 'post'
  form.action = url
  for (var key in params) { // 多个参数，则创建多个
    var input = document.createElement('input')
    input.type = 'hidden'
    input.name = key
    input.value = params[key]
    form.appendChild(input)
  }
  document.body.appendChild(form)
  form.submit()
  document.body.removeChild(form) // 释放标签
}

export function exportExcelData(response, name) {
  if (response.message && response.error) {
    const msg = response.message
    Message({ message: msg, showClose: false, type: 'error' })
    return
  }
  const blob = new Blob([response], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  })

  const fileName = name ? name + '.xlsx' : Date.parse(new Date()) + '.xlsx'
  if (window.navigator.msSaveOrOpenBlob) {
    // console.log(2)
    navigator.msSaveBlob(blob, fileName)
  } else {
    // console.log(3)
    var link = document.createElement('a')
    link.href = window.URL.createObjectURL(blob)
    link.download = fileName
    link.click()
    // 释放内存
    window.URL.revokeObjectURL(link.href)
  }
}

function exportCsvData(response) {
  if (response.message && response.error) {
    const msg = response.message
    Message({ message: msg, showClose: false, type: 'error' })
    return
  }
  const blob = new Blob([response], {
    type: 'text/csv'
  })
  const fileName = Date.parse(new Date()) + '.csv'
  if (window.navigator.msSaveOrOpenBlob) {
    // console.log(2)
    navigator.msSaveBlob(blob, fileName)
  } else {
    // console.log(3)
    var link = document.createElement('a')
    link.href = window.URL.createObjectURL(blob)
    link.download = fileName
    link.click()
    // 释放内存
    window.URL.revokeObjectURL(link.href)
  }
}

export function exportZipFile(response) {
  if (response.message && response.error) {
    const msg = response.message
    Message({ message: msg, showClose: false, type: 'error' })
    return
  }
  const blob = new Blob([response], {
    type: 'application/x-zip-compressed '
  })
  const fileName = Date.parse(new Date()) + '.zip'
  saveAs(blob, fileName)
}

export function exportExcelNew(url, data, method, name) {
  var loading = Loading.service()
  const param = createParam(url, data, method)
  return newRequest(param).then(resp => {
    loading.close()
    exportExcelData(resp, name)
  }).catch(ex => {
    debugger
    loading.close()
  })
}

export function dsOutExcel(url, data, method) {
  var loading = Loading.service()
  const param = createParam(url, data, method)
  return dsRequest(param).then(resp => {
    loading.close()
    exportExcelData(resp)
  }).catch(ex => {
    debugger
    loading.close()
  })
}

export function exportExcel(url, data, method, name) {
  var loading = Loading.service()
  const param = createParam(url, data, method)
  return request(param).then(
    response => {
      loading.close()
      exportExcelData(response, name)
    }
  ).catch(ex => {
    debugger
    loading.close()
  })
}

export function exportCsv(url, data, method) {
  var loading = Loading.service()
  const param = createParam(url, data, method)
  return request(param).then(
    response => {
      loading.close()
      exportCsvData(response)
    }
  ).catch(ex => {
    debugger
    loading.close()
  })
}
