export function blanceFmt(val) { // 分转化为元
  var num = Number(val)
  if (!num) { // 等于0
    return num + '.00'
  } else { // 不等于0
    num = Math.round((num) * 100) / 10000
    num = num.toFixed(2)
    num += '' // 转成字符串
    var reg = num.indexOf('.') > -1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g // 千分符的正则
    return num.replace(reg, '$1,') // 千分位格式化
  }
}

export function format(num, decimal) { // 万分位
  if (num) {
    // return (decimal?Number(num) + '':Number(num).toFixed(2) + '').replace(/\d{1,4}(?=(\d{4})+(\.\d*)?$)/g, '$&,')
    return (Number(num) + '').replace(/\d{1,4}(?=(\d{4})+(\.\d*)?$)/g, '$&,')
  } else {
    return 0
  }
}
// 订单状态
export function orderStatus(apply_status, status) {
  if (apply_status) {
    const m1 = new Map([
      [0, { color: '#909399', name: '没取消' }],
      [1, { color: '#909399', name: '审核中' }],
      [2, { color: '#909399', name: '已取消' }],
      [3, { color: '#909399', name: '被驳回' }]
    ])
    return m1.get(apply_status) || {
      name: '未知订单状态',
      color: '#909399'
    }
  } else {
    const m2 = new Map([
      [0, { color: '#f97547', name: '待付款' }],
      [1, { color: '#13c19f', name: '待发货' }],
      [2, { color: '#ffaa1d', name: '待收货' }],
      [3, { color: '#409EFF', name: '已完成' }],
      [4, { color: '#74a2e6', name: '已完成' }]
    ])
    return m2.get(status) || {
      name: '未知订单状态',
      color: '#909399'
    }
  }
}

// 店铺状态
export function storeStatus(status) {
  switch (+status) {
    case 0:
      return {
        name: '未认证',
        color: '#f97547'
      }
    case 1:
      return {
        name: '审核中',
        color: '#13c19f'
      }
    case 2:
      return {
        name: '待签约',
        color: '#ffaa1d'
      }
    case 3:
      return {
        name: '已完成',
        color: '#409EFF'
      }
    case 4:
      return {
        name: '被驳回',
        color: '#999'
      }
    case 5:
      return {
        name: '已冻结',
        color: '#ccc'
      }
    default :
      return {
        name: '审核中',
        color: '#13c19f'
      }
  }
}

// 退货审核状态
export function refundStatus(status) {
  switch (+status) {
    case 0:
      return {
        name: '未审核',
        color: '#f96b6b'
      }
    case 1:
      return {
        name: '已同意',
        color: '#13c19f'
      }
    case 2:
      return {
        name: '已驳回',
        color: '#415161'
      }
    case 3:
      return {
        name: '已完成',
        color: '#f96b6b'
      }
    default :
      return {
        name: '未知支付状态',
        color: '#909399'
      }
  }
}

// 支付状态
export function payStatus(status) {
  switch (+status) {
    case 0:
      return {
        name: '未付款',
        color: '#f96b6b'
      }
    case 1:
      return {
        name: '部分付款',
        color: '#f96b6b'
      }
    case 2:
      return {
        name: '已付款',
        color: '#415161'
      }
    case 3:
      return {
        name: '待退款',
        color: '#f96b6b'
      }
    default :
      return {
        name: '未知支付状态',
        color: '#909399'
      }
  }
}


// 认证状态显示
export function certStatus(status) {
  let out
  switch (status) {
    case 0:
      out = '销售未实名认证'
      break
    case 1:
      out = '等待邀请人审核'
      break
    case 2:
      out = '等待审核'
      break
    case 3:
      out = '等待用户确认'
      break
    case 4:
      out = '邀请人审核驳回'
      break
    case 5:
      out = '审核驳回'
      break
    case 6:
      out = '授权成功'
      break
    default:
      out = status
  }
  return out
}
// 系统设置拆解
export function filterSet(arr, key) {
  if (arr.length < 1) {
    return null
  }
  return arr.filter(item => {
    return item.keys === key
  })[0]
}

// 总金额
export const totalPrice = data => {
  let totalPrice = 0
  data.map(i => {
    i.sku.map(j => {
      totalPrice += j.num * j.price
    })
  })
  return totalPrice
}
// 总数量
export const totalNum = data => {
  let totalNum = 0
  data.map(i => {
    i.sku.map(j => {
      totalNum += j.num
    })
  })
  return totalNum
}
// 计算购买时长
export function coutdown(now, old) {
  if (now == null) { return 0 }
  const big = Date.parse(new Date(old)) / 1000
  const small = Date.parse(new Date(now)) / 1000
  const add0 = num => (num < 10 ? '0' + num : num)
  const sec = big - small
  if (sec > 0) {
    const day = Math.floor(sec / 86400)
    const hour = Math.floor((sec % 86400) / 3600)
    const minite = Math.floor((sec - 86400 * day - hour * 3600) / 60)
    const second = Math.floor(sec - 86400 * day - hour * 3600 - minite * 60)
    return `${add0(day)}`
  } else {
    return 0
  }
}

