<template>
  <el-date-picker v-model="value" type="daterange" align="right" unlink-panels range-separator="至"
    start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" format="yyyy-MM-dd">
  </el-date-picker>
</template>

<script>
export default {
  data () {
    const orderSearch = this.$store.getters['app/getOrderSerch']
    const startTimestamp = orderSearch.start_time
    const endTimestamp = orderSearch.end_time
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: '今天',
            onClick (picker) {
              const start = new Date(new Date().setHours(0, 0, 0, 0))
              const end = new Date(new Date().setHours(23, 59, 59, 999))
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '这月',
            onClick (picker) {
              const end = new Date()
              end.setHours(23, 59, 59, 999)
              const start = new Date(new Date().getFullYear(), new Date().getMonth(), 1, 0, 0, 0, 0)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '今年',
            onClick (picker) {
              const end = new Date()
              end.setHours(23, 59, 59, 999)
              const start = new Date(new Date().getFullYear(), 0, 1, 0, 0, 0, 0)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一周',
            onClick (picker) {
              const end = new Date()
              end.setHours(23, 59, 59, 999)
              const start = new Date()
              start.setDate(end.getDate() - 6)
              start.setHours(0, 0, 0, 0)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick (picker) {
              const end = new Date()
              end.setHours(23, 59, 59, 999)
              const start = new Date()
              start.setMonth(end.getMonth() - 1)
              start.setHours(0, 0, 0, 0)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick (picker) {
              const end = new Date()
              end.setHours(23, 59, 59, 999)
              const start = new Date()
              start.setMonth(end.getMonth() - 3)
              start.setHours(0, 0, 0, 0)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '上月',
            onClick (picker) {
              const end = new Date(new Date().getFullYear(), new Date().getMonth(), 0, 23, 59, 59, 999)
              const start = new Date(new Date().getFullYear(), new Date().getMonth() - 1, 1, 0, 0, 0, 0)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      value: [startTimestamp, endTimestamp]
    }
  },
  watch: {
    value (newVal) {
      if (newVal && newVal.length === 2) {
        const end = newVal[1]
        end.setHours(23, 59, 59, 999) // 设置结束日期为当天的23:59:59
        const startTimestamp = Math.floor(newVal[0].getTime() / 1000) // 转换毫秒为秒
        const endTimestamp = Math.floor(end.getTime() / 1000) // 转换毫秒为秒
        this.$store.commit('app/setOrderSerch', { start_time: startTimestamp })
        this.$store.commit('app/setOrderSerch', { end_time: endTimestamp })
      }
      if (newVal === null) {
        this.$store.commit('app/setOrderSerch', { start_time: '' })
        this.$store.commit('app/setOrderSerch', { end_time: '' })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-date-editor .el-range__close-icon {
  display: block !important;
}
</style>
