<template>
  <div>
    <span>{{ hour? hourString+'小时'+minuteString+'分'+secondString+"秒" : minuteString+'分'+secondString+"秒" }}</span>
  </div>
</template>

<script>
export default {
  props: ["remainTime"],
  data() {
    return {
      hour: '',
      minute: '',
      second: '',
      promiseTimer: ''
    }
  },
  computed: {
    hourString() {
      return this.formatNum(this.hour)
    },
    minuteString() {
      return this.formatNum(this.minute)
    },
    secondString() {
      return this.formatNum(this.second)
    }
  },
  watch:{
	remainTime(newValue, oldValue) {
	  			console.log("监听")
			  this.checkTime()
	          return newValue;
	     }
  },  
  mounted() {
   this.checkTime()
  },
  methods: {
	  checkTime(){
	  		if (this.remainTime > 0) {
	  		  this.hour = Math.floor((this.remainTime / 3600) % 24)
	  		  this.minute = Math.floor((this.remainTime / 60) % 60)
	  		  this.second = Math.floor(this.remainTime % 60)
	  		  this.countDowm()
	  		} 
	  },
    countDowm() {
      var self = this
      clearInterval(this.promiseTimer)
      this.promiseTimer = setInterval(function() {
        if (self.hour === 0) {
          if (self.minute !== 0 && self.second === 0) {
            self.second = 59
            self.minute -= 1
          } else if (self.minute === 0 && self.second === 0) {
            self.second = 0
            self.$emit('countDowmEnd', true)
            clearInterval(self.promiseTimer)
          } else {
            self.second -= 1
          }
        } else {
          if (self.minute !== 0 && self.second === 0) {
            self.second = 59
            self.minute -= 1
          } else if (self.minute === 0 && self.second === 0) {
            self.hour -= 1
            self.minute = 59
            self.second = 59
          } else {
            self.second -= 1
          }
        }
      }, 1000)
    },
    formatNum(num) {
      return num < 10 ? '0' + num : '' + num
    }
  }
}
</script>
