import CryptoJS from 'crypto-js'

const payStatus = {
  WAIT_PAY: {
    label: '待支付',
    type: 'primary'
  },
  PAYED: {
    label: '已支付',
    type: 'success'
  },
  CLOSED: {
    label: '已关闭',
    type: 'info'
  },
  CANCELED: {
    label: '已取消',
    type: 'info'
  },
  REFUNDED: {
    label: '已退款',
    type: 'danger'
  },
  REFUNDING: {
    label: '退款中',
    type: 'warning'
  }
}

export function getPayStatus (str, attr = 'label') {
  return payStatus[str] ? payStatus[str][attr] : ''
}

export function parseTime (time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (('' + time).length === 10) time = parseInt(time) * 1000
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const timeStr = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return timeStr
}

export function param2Obj (url) {
  const search = url.split('?')[1]
  if (!search) {
    return {}
  }
  return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
}

// 设置表格总计行高亮
export function totalRowClassName ({row, rowIndex}) {
  for (let key in row) {
    if (row[key] === '总计') {
      return 'tableTotalRow'
    }
    if ((row[key] + '').indexOf('小计') > -1) {
      return 'tableTotalRow tableTotalRow1'
    }
    if ((row[key] + '').indexOf('总计') > -1) {
      return 'tableTotalRow tableTotalRow2'
    }
  }
}

const desKey = 'fnFACVwC'
const key = CryptoJS.enc.Hex.parse('TgxRS8DG65EjK8cpKTiPAw==') // 密钥
const iv = CryptoJS.enc.Hex.parse('') // 偏移量

// aes加密
export function encrypt (word) {
  let encrypted = ''
  if (typeof word === 'string') {
    const key = CryptoJS.enc.Utf8.parse(desKey)
    console.log(word, desKey, 'aaaa')
    encrypted = CryptoJS.DES.encrypt(word, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    })

    return encrypted.toString()

    // const srcs = CryptoJS.enc.Hex.parse(word)
    // // var encryptedBase64Str = CryptoJS.enc.Base64.stringify(srcs)
    // encrypted = CryptoJS.AES.encrypt(srcs, key, {
    //   mode: CryptoJS.mode.ECB,
    //   padding: CryptoJS.pad.ZeroPadding
    // })
  } else if (typeof word === 'object') {
    // 对象格式的转成json字符串
    const data = JSON.stringify(word)
    const srcs = CryptoJS.enc.Utf8.parse(data)
    encrypted = CryptoJS.AES.encrypt(srcs, key, {
      iv: iv,
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    })
  }
  return encrypted.ciphertext.toString()
}

// aes解密
export function decrypt (word) {
  const encryptedHexStr = CryptoJS.enc.Hex.parse(word)
  const srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr)
  const decrypt = CryptoJS.AES.decrypt(srcs, key, {
    iv: iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  })
  const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
  return decryptedStr.toString()
}
