import dayjs from 'dayjs'

// 获取当天0点的时间戳
// const getStartTime = (time: number) => {
//   const nowTimeDate = new Date(time)
//   return nowTimeDate.setHours(0, 0, 0, 0)
// }
// 时间戳转日期格式
// const timestampToTime = (timestamp: number, str?: string) => {
//   return dayjs(timestamp).format(str ? str : 'YYYY.MM.DD')
// }
// 获取一周的日期
// const getWeekList = (str?: string): TimeChageResType[] => {
//   let week = []
//   for (var i = 0; i < 7; i++) {
//     let weekObj = {
//       name: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'][i],
//       date: '',
//       timeStamp: 0
//     }
//     week.push(weekObj)
//   }
//   let today = getStartTime(Number(new Date())) // 当天时间戳
//   let today_week = new Date().getDay() // 当天星期几
//   if (today_week === 0) {
//     // 若当天为周日
//     week[6].timeStamp = today
//   } else {
//     week[today_week - 1].timeStamp = today
//   }

//   var leftNum = today_week - 1 // 本周内今天的前几天的数量
//   var rightNum = 7 - today_week // 本周内今天的后几天的数量

//   for (var left = 0; left < leftNum; left++) {
//     week[left].timeStamp = today - (today_week - left - 1) * 1000 * 60 * 60 * 24
//   }
//   for (var right = 0; right < rightNum; right++) {
//     week[right + today_week].timeStamp = today + (right + 1) * 1000 * 60 * 60 * 24
//   }
//   week.map(el => (el.date = timestampToTime(el.timeStamp, str)))
//   const weekList = JSON.parse(JSON.stringify(week))
//   return weekList
// }

// ---------以上为获取当前星期

// ---------以下为往后延申6天

const weekArr = ['日', '一', '二', '三', '四', '五', '六']

const getWeekList = (str?: string, delay?: number, dayNum: number = 7): TimeChageResType[] => {
  // str=>时间格式

  // delay=>不传从当天开始,比如当天为1-1
  // 就返回 01-01  01-02... 传5就是 01-06 01-07...

  // number=>不传就是往后推7天。传数字多少就是推几天。相当于列表的长度

  let today = Date.now() // 当天时间戳

  if (delay) today = today + delay * 1000 * 60 * 60 * 24

  let arr: TimeChageResType[] = []

  for (var i = 0; i < dayNum; i++) {
    let timeCuo = today + i * 1000 * 60 * 60 * 24
    arr.push({
      name: '周' + weekArr[dayjs(timeCuo).day()],
      date: dayjs(timeCuo).format(str ? str : 'YYYY.MM.DD'),
      timeStamp: timeCuo
    })
  }

  return arr
}

export default getWeekList

export type TimeChageResType = {
  name: string
  date: string
  timeStamp: number
}
