const WEEKS = ['日', '一', '二', '三', '四', '五', '六']
export function getDate() {
  const date = new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  if (month < 10) {
    month = '0' + month
  }
  if (day < 10) {
    day = '0' + day
  }
  return year + '-' + month + '-' + day
}

export function formatDate(date) {
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  if (month < 10) {
    month = '0' + month
  }
  if (day < 10) {
    day = '0' + day
  }
  return year + '-' + month + '-' + day
}

export function getDateRangeByOffsetDays(offsetDays) {
  const now = new Date()
  let end = now.getFullYear() + '-' + ('0' + (now.getMonth() + 1))
      .slice(-2) + '-' + ('0' + now.getDate()).slice(-2)

  let begin = new Date(now.getTime() - (3600 * 24 * offsetDays * 1000) )
  let start = begin.getFullYear() + '-' + ('0' + (begin.getMonth() + 1))
      .slice(-2) + '-' + ('0' + begin.getDate()).slice(-2)
  return { start, end }
}

export function getOneWeekOffset(start) {
  const weekAgo = new Date(start).getTime() + 7 * 24 * 3600 * 1000
  const endDate = new Date(weekAgo)
  const year = endDate.getFullYear()
  const month = endDate.getMonth() + 1
  const date = endDate.getDate()
  const end = year + '-' + ('0' + month).slice(-2) + '-' + ('0' + date).slice(-2)
  return { start, end }
}

export function getOneWeekText() {
  const day = new Date().getDay()
  const arr = new Array(7)
  for (let index = 0; index < 7; index++) {
    let tempDay = (day + index) % 7
    arr[index] = WEEKS[tempDay]
  }
  return arr
}

export function getNextSevenDate() {
  const dates = new Array(7)
  for (let index = 0; index < dates.length; index++) {
    dates[index] = getDateByOffeset(index)
  }
  return dates
}

function getDateByOffeset(offset) {
  const date = new Date()
  date.setDate(date.getDate() + offset) //获取offset天后的日期
  const year = date.getFullYear()
  let month = date.getMonth() + 1 //获取当前月份
  month = month < 10 ? '0' + month : month
  let day = date.getDate() //获取当前月份的日期
  day = day < 10 ? '0' + day : day
  return {
    fullDate: year + '-' + month + '-' + day,
    date: day,
  }
}

export function getDateTime() {
  const date = new Date()
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hour = date.getHours()
  let minute = date.getMinutes()
  let second = date.getSeconds()
  return year + '-' + ('0' + month).slice(-2) + '-' + ('0' + day).slice(-2) +
      ' ' + ('0' + hour).slice(-2) + ':' + ('0' + minute).slice(-2) + ':' + ('0' + second).slice(-2)
}

export function getDateDiff(date) {
  const reqdate = new Date(date)
  const today = new Date().getTime()
  return parseInt((today - reqdate) / 1000 / 3600 / 24)
}