/**
 * 图片：
.jpeg格式：image/jpeg
.png格式： image/png
.gif格式： image/gif

音频：
.mp3格式：audio/mpeg

视频：
.mp4格式：video/mp4
.m3u8格式：application/x-mpegURL
.mov格式：video/x-ms-wmv
.avi格式：video/x-msvideo
.flv格式：video/x-flv
.wmv格式：video/x-ms-wmv

文本：
.xls格式：application/vnd.ms-excel
.xlsx格式：application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.doc格式：application/msword
.docx格式：application/vnd.openxmlformats-officedocument.wordprocessingml.document
.txt格式：text/plain
.pdf格式：application/pdf
.ppt格式：application/vnd.ms-powerpoint
.zip格式：application/zip
.rar格式：application/x-rar
 */

import { ElMessage } from 'element-plus'
import { downFileById, downloadToPdfStream } from '@/http/apis/commonApi'
export const handleFileSuccess = (res, fileList, onefile, fn) => {
  if (res.code !== 200) {
    ElMessage.error('上传失败!')
    const index = fileList.findIndex(i => i.response.code !== 200)
    fileList.splice(index, 1)
  }
  if (onefile && fileList.length > 1) {
    ElMessage.error('只能上传一个文件!')
    fileList.shift()
  }
  // fileList.forEach(async i => {
  //   i.fileUrlById = await downloadFileUrl(i.response.data.id)
  // })
  fn && fn()
}
export const handleFileError = error => {
  ElMessage.error(error.message ? JSON.parse(error.message) : '上传失败！')
}

export const fileTypes = ['wps', 'pdf', 'doc', 'docx']
// export const fileTypes = ['application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/wps-office.wps', 'application/wps-office.doc', 'application/wps-office.docx', 'application/wpsoffice']
export const fileDesc = 'pdf、word'
export const fileFormatVerification = (file, { types, size }) => {
  const fileTypes = file.name.substring(file.name.lastIndexOf('.') + 1).toLocaleLowerCase()
  const isType = types.indexOf(fileTypes) !== -1
  const isSize = size ? file.size / 1024 <= size : true
  if (!isType) {
    ElMessage.error('上传文件格式错误!')
  }
  if (!isSize) {
    ElMessage.error(
      `上传文件大小不能超过 ${size >= 1024 ? size / 1024 : size}${
        size >= 1024 ? 'M' : 'KB'
      }!`
    )
  }
  return isType && isSize
}
export const handleFileExceed = (fileList) => {
  ElMessage.warning(`该处只可上传${fileList.length}个文件`)
}
export const handleFilePreview = (file) => {
  window.open(`/fileView?id=${file?.response?.data?.id}`, '_blank')
}
// 编辑回显附件
export const reviewFileParam = (files) => {
  let fileList = []
  if (files) {
    fileList = files?.map?.(item => ({
      uid: item?.id,
      name: item?.originalFileName,
      status: 'success',
      url: item?.url,
      response: {
        data: item
      }
    })) || []
  }
  return JSON.parse(JSON.stringify(fileList))
}
// 提交附件
export const changFilesParam = (files) => {
  let fileList = []
  if (files && files.length) {
    fileList = files.map(item => {
      return item.response ? item.response?.data : { ...item }
    })
  }
  return fileList
}
export const downloadFileUrl = async fileId => {
  const res = await downFileById({ fileId })
  const url = URL.createObjectURL(res)
  return url
}

export const downloadToPdfStreamFileUrl = async fileId => {
  const res = await downloadToPdfStream({ fileId })
  const url = URL.createObjectURL(res)
  return url
}

