import { axios } from '@/utils/request'
import { stringify } from 'qs'
import request from 'axios'
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'

/**
 * user page func
 * parameter: {
 *     keyword: '',
 * }
 * @param parameter
 * @returns {*}
 */
export function getFilePage (parameter) {
  return axios({
    url: `/upms/files/page?${stringify(parameter)}`,
    method: 'get',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

export function queryFile (parameter) {
  return axios({
    url: `/upms/files?${stringify(parameter)}`,
    method: 'get',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    }
  })
}

export const uploadUrl = process.env.VUE_APP_API_BASE_URL + '/upms/files/upload'
export function uploadFile (parameter) {
  return axios({
    url: `/upms/files/upload`,
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    data: parameter
  })
}

export function downloadFile (url) {
  const storeToken = Vue.ls.get(ACCESS_TOKEN)
  request.post(process.env.VUE_APP_API_BASE_URL + url, null, {
    responseType: 'blob',
    headers: { 'Authorization': 'Bearer ' + storeToken }
  }).then(resp => {
    const headers = resp.headers
    const contentType = headers['content-type']
    if (!resp.data) {
      console.error('响应异常：', resp)
      return false
    } else {
      console.error('下载文件：', resp)
      const blob = new Blob([resp.data], { type: contentType })

      const contentDisposition = resp.headers['content-disposition']
      let fileName = 'unknown'
      if (contentDisposition) {
        fileName = window.decodeURI(escape(resp.headers['content-disposition'].split('=')[1]))
      }
      console.log('文件名称：', fileName)
      downFile(blob, fileName)
    }
  }).catch(function (error) {
    console.log(error)
  })
}

export function downloadFileAndName (url, fileName) {
  const storeToken = Vue.ls.get(ACCESS_TOKEN)
  request.post(process.env.VUE_APP_API_BASE_URL + url, null, {
    responseType: 'blob',
    headers: { 'Authorization': 'Bearer ' + storeToken }
  }).then(resp => {
    const headers = resp.headers
    const contentType = headers['content-type']
    if (!resp.data) {
      console.error('响应异常：', resp)
      return false
    } else {
      console.error('下载文件：', resp)
      const blob = new Blob([resp.data], { type: contentType })
      downFile(blob, fileName)
    }
  }).catch(function (error) {
    console.log(error)
  })
}

/**
 *
 */
function downFile (blob, fileName) {
  // 非IE下载
  if ('download' in document.createElement('a')) {
    const link = document.createElement('a')
    link.href = window.URL.createObjectURL(blob) // 创建下载的链接
    link.download = fileName // 下载后文件名
    link.style.display = 'none'
    document.body.appendChild(link)
    link.click() // 点击下载
    window.URL.revokeObjectURL(link.href) // 释放掉blob对象
    document.body.removeChild(link) // 下载完成移除元素
  } else {
    // IE10+下载
    window.navigator.msSaveBlob(blob, fileName)
  }
}
