<template>
  <div class="block">
    <span class="demonstration">请选择日期</span>
    <el-date-picker
      v-model="value"
      align="right"
      type="daterange"
      unlink-panels
      value-format="yyyy-MM-dd"
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      :picker-options="pickerOptions"
      :clearable=false
      size="small"
      @change="dateTimeChange">
    </el-date-picker>
  </div>
</template>
<script>
import Util from '../utils/util.js'
export default {
  data () {
    return {
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > Date.now()
        },
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      value: []
    }
  },
  mounted () {
    this.defaultValue()
  },
  methods: {
    defaultValue () {
      this.value = []
      let sdata = Util.getDay(-7, '-')
      let edate = Util.getDay(0, '-')
      this.value.push(sdata)
      this.value.push(edate)
    },
    dateTimeChange (value) {
      let dateArr = []
      for (let i = 0; i < value.length; i++) {
        dateArr.push(value[i].replace(/-/g, ''))
      }
      this.$emit('choose-date', dateArr)
    }
  }
}
</script>
