<template>
  <div class="data-range-container">
    <el-date-picker
      v-model="startDatePrivate"
      format="yyyy-MM-dd"
      value-format="yyyy-MM-dd"
      :picker-options="startDateOption"
      type="date"
      placeholder="选择日期">
    </el-date-picker>
    -
    <el-date-picker
      v-model="endDatePrivate"
      format="yyyy-MM-dd"
      value-format="yyyy-MM-dd"
      :picker-options="endDateOption"
      type="date"
      placeholder="选择日期">
    </el-date-picker>
  </div>
</template>

<script>
import {GetDateStr, getLastMonthDay, GetNextMonthDay, GetPreMonthDay} from '@/utils/util'
export default {
  name: 'date-range',
  props: {
    value:{ // 必须要使用value
      default: ()=>([]),
    },
    offset:{ // 必须要使用value
      default: 0,
    },
  },
  data() {
    return {
      startDatePrivate: '',
      endDatePrivate: '',
      startDateOption:{
        firstDayOfWeek: 1,
        disabledDate: (time) => {
          let endTimeVal = this.endDateN;
          if (endTimeVal) {
            return time.getTime() > endTimeVal;
          }
        }
      },
      endDateOption:{
        firstDayOfWeek: 1,
        disabledDate: (time) => {
          let startTimeVal = this.startDateN;
          if (startTimeVal) {
            return time.getTime() < startTimeVal;
          }
        }
      }
    }
  },
  computed: {
    startDateN: {
      get: function(){
        let date=this.startDatePrivate
        date=date.replace(/-/g,'/')
        return (new Date(date)).getTime()
      },
    },
    endDateN: {
      get: function(){
        let date=this.endDatePrivate
        date=date.replace(/-/g,'/')
        return (new Date(date)).getTime()
      },
    }
  },
  watch:{
    value(val) {
      if(val && val.length){
        if(this.startDatePrivate !== val[0] || this.endDatePrivate !== val[1] ){
          this.startDatePrivate = val[0]
          this.endDatePrivate = val[0]
        }
      }
    },
    startDatePrivate(val) {
      this.commit()
    },

    endDatePrivate(val) {
      this.commit()
    }
  },
  mounted(){
    // console.log(this.total)
    this.endDatePrivate = GetDateStr((new Date()).getTime(), 0)
    this.startDatePrivate = GetDateStr((new Date()).getTime(), (-1)*(this.offset))
  },
  methods: {
    commit(){
      let startDate=this.startDatePrivate
      let endDate=this.endDatePrivate
      let start=startDate?GetDateStr(startDate,0):''
      let end=endDate?GetDateStr(endDate,0):''
      this.$emit('input', [start, end])
    }
  },
}
</script>

<style scoped>
.page-container{
  padding:30px 10px
}
</style>
