<template>
  <div class="data-range-container">
    <el-button v-show="showButton" type="primary" @click="previous">前一天</el-button>
    <el-date-picker
      v-model="datePrivate"
      format="yyyy-MM-dd"
      value-format="yyyy-MM-dd"
      :picker-options="dateOption"
      type="date"
      placeholder="选择日期">
    </el-date-picker>
    <el-button v-show="showButton" type="primary" @click="next">后一天</el-button>
  </div>
</template>

<script>
import {GetDateStr} from '@/utils/util'
export default {
  name: 'date-signal',
  props: {
    value:{ // 必须要使用value
      default: ()=>([]),
    },
    offset:{ // 必须要使用value
      default: 0,
    },
    showButton:{ // 必须要使用value
      default: false
    },
  },
  data() {
    return {
      datePrivate: '',
      dateOption:{
        firstDayOfWeek: 1,
        disabledDate: (time) => {
          return time.getTime() > this.today;
        }
      },
    }
  },
  computed: {
    today: {
      get: function(){
        return (new Date()).getTime()
      },
    },
    dateN: {
      get: function(){
        let date=this.datePrivate

        if(!date||date.length==0){
          date= new Date()
        }else{
          date=date.replace(/-/g,'/')
        }

        return date
      },
    },
  },
  watch:{
    value(val) {
      if(val && val.length){
        if(this.datePrivate !== val){
          this.datePrivate = val[0]
        }
      }
    },
    datePrivate(val) {
      let arr=['', '']
      if (val) {
        arr = [GetDateStr(val, 0), GetDateStr(val, 0)]
      }
      this.$emit('input', arr)
    }
  },
  mounted(){
    // console.log(this.total)
    this.datePrivate = GetDateStr((new Date()).getTime(), (-1)*(this.offset))
  },
  methods: {
    next(){
      this.datePrivate=GetDateStr(this.dateN,1)
    },
    previous(){
      this.datePrivate=GetDateStr(this.dateN,-1)
    }
  },
}
</script>

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