import Date from './datetime.js'

export const calendarBaseShortcuts = [{
  text: '今天',
  onClick(picker) {
    const start = new Date().toLocaleDateString().replaceAll('/', '-')
    picker.$emit('pick', [start + ' 00:00:00', start + ' 23:59:59'])
  }
}, {
  text: '昨天',
  onClick(picker) {
    const start = new Date().daysAgo(1).toLocaleDateString().replaceAll('/', '-')
    picker.$emit('pick', [start + ' 00:00:00', start + ' 23:59:59'])
  }
}, {
  text: '最近一周',
  onClick(picker) {
    const start = new Date().daysAgo(7).toLocaleDateString().replaceAll('/', '-') + ' 00:00:00'
    picker.$emit('pick', [start, new Date().toLocaleDateString().replaceAll('/', '-') + ' 23:59:59'])
  }
}, {
  text: '最近30天',
  onClick(picker) {
    const start = new Date().daysAgo(30).toLocaleDateString().replaceAll('/', '-')
    picker.$emit('pick', [start + ' 00:00:00', new Date().toLocaleDateString().replaceAll('/', '-') + ' 23:59:59'])
  }
}, {
  text: '这个月',
  onClick(picker) {
    const start = new Date().monthBegin().toLocaleDateString().replaceAll('/', '-')
    picker.$emit('pick', [start + ' 00:00:00', new Date().toLocaleDateString().replaceAll('/', '-') + ' 23:59:59'])
  }
}, {
  text: '本季度',
  onClick(picker) {
    const start = new Date().quarterBegin().toLocaleDateString().replaceAll('/', '-')
    picker.$emit('pick', [start + ' 00:00:00', new Date().toLocaleDateString().replaceAll('/', '-') + ' 23:59:59'])
  }
}]

export const calendarMoveShortcuts = [{
  text: '‹ 往前一天 ',
  onClick(picker) {
    if (picker.value.length === 0) {
      picker.value = [new Date(), new Date()]
    }
    const start = picker.value[0].daysAgo(1).toLocaleDateString().replaceAll('/', '-')
    const end = picker.value[1].daysAgo(1).toLocaleDateString().replaceAll('/', '-')
    picker.$emit('pick', [start + ' 00:00:00', end + ' 23:59:59'])
  }
}, {
  text: ' 往后一天 ›',
  onClick(picker) {
    let start = new Date()
    let end = new Date()
    if (picker.value.length > 0) {
      if (end - picker.value[1] > 8.64E7) {
        start = picker.value[0].daysAgo(-1).toLocaleDateString().replaceAll('/', '-')
        end = picker.value[1].daysAgo(-1).toLocaleDateString().replaceAll('/', '-')
      } else {
        start = picker.value[0].toLocaleDateString().replaceAll('/', '-')
      }
    }
    picker.$emit('pick', [start + ' 00:00:00', end + ' 23:59:59'])
  }
}, {
  text: '« 往前一周 ',
  onClick(picker) {
    if (picker.value.length === 0) {
      picker.value = [new Date().daysAgo(7), new Date()]
    }
    const end = picker.value[0].daysAgo(0).toLocaleDateString().replaceAll('/', '-')
    const start = picker.value[0].daysAgo(7).toLocaleDateString().replaceAll('/', '-')
    picker.$emit('pick', [start + ' 00:00:00', end + ' 23:59:59'])
  }
}, {
  text: ' 往后一周 »',
  onClick(picker) {
    let start = new Date()
    let end = new Date()
    if (picker.value.length > 0) {
      if (end - picker.value[1] > 8.64E7) {
        start = picker.value[1].daysAgo(0).toLocaleDateString().replaceAll('/', '-')
        end = picker.value[1].daysAgo(-7).toLocaleDateString().replaceAll('/', '-')
      } else {
        start = picker.value[0].toLocaleDateString().replaceAll('/', '-')
      }
    }
    picker.$emit('pick', [start + ' 00:00:00', end + ' 23:59:59'])
  }
}]

export const calendarShortcuts = [
  ...calendarBaseShortcuts,
  ...calendarMoveShortcuts
]
