import { MessageBox, Message } from 'element-ui'
import { exportApi, importApi, getResources } from '@/api/common.js'
let pubFun = {
  // 格式化时间
  formatDate(params) {
    let date = params ? new Date(params) : new Date()
    let year = date.getFullYear()
    let month =
      date.getMonth() + 1 < 10
        ? '0' + (date.getMonth() + 1)
        : date.getMonth() + 1
    let strDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
    let h = date.getHours()
    h = h < 10 ? '0' + h : h
    let minute = date.getMinutes()
    minute = minute < 10 ? '0' + minute : minute
    let second = date.getSeconds()
    second = second < 10 ? '0' + second : second
    return (
      year + '-' + month + '-' + strDate + ' ' + h + ':' + minute + ':' + second
    )
  },
  // 公用导出
  exportOperation(url, params, method, fileName) {
    MessageBox.confirm('是否导出Excel?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      cancelButtonClass: 'mosaic-info',
      confirmButtonClass: 'mosaic-primary'
    })
      .then(() => {
        exportApi(url, params, method)
          .then(response => {
            let data = response
            let fileReader = new FileReader()
            fileReader.onload = function () {
              try {
                const jsonData = JSON.parse(fileReader.result)
                Message.closeAll()
                Message.error(jsonData.message)
              } catch (err) {
                if (typeof window.navigator.msSaveBlob !== 'undefined') {
                  window.navigator.msSaveBlob(
                    new Blob([response], {
                      type: 'application/pdf'
                    }),
                    fileName + '.pdf'
                  )
                } else {
                  let url = window.URL.createObjectURL(
                    new Blob([response], {
                      type: 'application/pdf'
                    })
                  )
                  let link = document.createElement('a')
                  link.style.display = 'none'
                  link.href = url
                  link.setAttribute('download', fileName + '.pdf')
                  document.body.appendChild(link)
                  link.click()
                  document.body.removeChild(link)
                  window.URL.revokeObjectURL(url)
                }
              }
            }
            fileReader.readAsText(data)
          })
          .catch(err => {
            console.log(err)
          })
      })
      .catch(() => {})
  },
  // 公用导入
  importOperation(url, files, method) {
    let params = new FormData()
    files.forEach(item => {
      params.append('file', item.raw)
    })
    return new Promise((resolve, reject) => {
      importApi(url, params, method)
        .then(res => {
          if (res.code == 0) {
            Message.closeAll()
            Message.success('上传成功')
            resolve(true)
          }
        })
        .catch(() => {
          reject()
        })
    })
  },
  tip() {
    Message.closeAll()
    Message.warning('敬请期待')
  },
  // 驼峰转下划线
  humpToUnderline(str) {
    return str.replace(/([A-Z])/g, '_$1').toLowerCase()
  },
  // 获取已设置权限
  getpermission(ids) {
    return new Promise(resolve => {
      getResources().then(response => {
        if (response.data.code == 0) {
          let buttonData = response.data.data
          let intersection = buttonData.filter(val => {
            return ids.indexOf(val) > -1
          })
          if (intersection.length > 0) {
            resolve(intersection)
          }
        }
      })
    })
  }
}

export default pubFun
