import Ajax from './ajax'
import api from './api'
// import { Message } from 'element-ui'
// import store from '../store'
const toolClass = {
  dateFormat: (timestamp, fmt) => {
    if (!fmt) {
      fmt = 'yyyy-MM-dd hh:mm'
    }

    let date = new Date(timestamp)
    var o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds(),
      'q+': Math.floor((date.getMonth() + 3) / 3),
      'S': date.getMilliseconds()
    }
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (var k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
      }
    }
    return fmt
  },
  ReplaceOrg (url) {
    return url.replace(':org', 'MQ')
  },
  upload (item) {
    return new Promise((resolve, reject) => {
      Ajax({
        ...api.file.image,
        data: { file: item.file },
      }).then((res) => {
        let result = {
          result: res
        }
        if (item.onSuccess) {
          item.onSuccess(result, item.file)
        }
        // console.log(result)
        resolve(result)
      }).catch(reject)
    })
  },
  beforeUpload (file) {
    return true
    // return new Promise((resolve, reject) => {
    //   let w = store.state.app.picSize.w
    //   let h = store.state.app.picSize.h
    //   if ('image'.indexOf(file.type) > -1) {
    //     Message({
    //       type: 'error',
    //       message: `请上传${w}像素*${h}像素尺寸的图片`
    //     })
    //     reject(new Error('error'))
    //   }
    //   var reader = new FileReader()
    //   reader.onload = function (e) {
    //     var data = e.target.result
    //     // 加载图片获取图片真实宽度和高度
    //     var image = new Image()
    //     image.onload = function () {
    //       var width = image.width
    //       var height = image.height
    //       console.log(width)
    //       console.log(height)
    //       if (width === w && height === h) {
    //         resolve()
    //       } else {
    //         Message({
    //           type: 'error',
    //           message: `请上传${w}像素*${h}像素尺寸的图片`
    //         })
    //         reject(new Error('error'))
    //       }
    //     }
    //     image.src = data
    //   }
    //   reader.readAsDataURL(file)
    // })
  }
}

export function formatTimeBySeconds (ms) {
  let hs = ms / 3600000 | 0
  let min = (ms - (hs * 3600000)) / 60000 | 0
  let sec = (ms - (hs * 3600000) - (min * 60000)) / 1000 | 0
  hs = hs < 10 ? '0' + hs : hs
  min = min < 10 ? '0' + min : min
  sec = sec < 10 ? '0' + sec : sec
  return hs > 0 ? `${hs}:${min}:${sec}` : `${min}:${sec}`
}

export default toolClass
