<template>
  <div>
    <el-date-picker
      v-if="!isSingleStyle"
      v-model="choiceTime"
      type="daterange"
      align="right"
      unlink-panels
      range-separator="至"
      start-placeholder="开始日期"
      end-placeholder="结束日期"
      :value-format="format"
      :picker-options="pickerOptions"
      @change="change">
    </el-date-picker>
    <el-date-picker
      v-else
      v-model="choiceTime"
      type="daterange"
      align="right"
      placeholder="选择日期时间"
      unlink-panels
      :value-format="format"
      :picker-options="pickerOptions"
      @change="change">
    </el-date-picker>
  </div>
</template>

<script>
/**
 * 自定义时间选择器，开始时间和结束时间
 *
 * 回调方法 handelChange：
 *  返回一个数组，第一个参数为开始时间，第二个为结束时间。
 *
 */
export default {
  props: {
    format: {
      type: String,
      default: 'yyyy-MM-dd hh:mm:ss'
    },
    isSingleStyle: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      choiceTime: [],
      // 时间选择的数据
      pickerOptions: {
        shortcuts: [
          {
            text: '今天',
            onClick(picker) {
              const start = new Date()
              picker.$emit('pick', [start, start])
            }
          },
          {
            text: '昨天',
            onClick(picker) {
              const start = new Date()
              start.setDate((start.getDate() - 1))
              picker.$emit('pick', [start, start])
            }
          },
          {
            text: '最近7天',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近30天',
            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])
            }
          },
          {
            text: '本月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setDate(1)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '上月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              console.log('天数' + start.getMonth())
              start.setMonth((start.getMonth() - 1))
              start.setDate(1)
              end.setDate(1)
              end.setTime(end.getTime() - 3600 * 1000 * 24 * 1)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      }
    }
  },
  methods: {
    change(val) {
      this.$emit('handelChange', val)
    }
  }

}
</script>

<style scoped>

</style>
