export default {
  data() {
    return {
      targetTime: new Date('2024-07-31T15:59:59').getTime(), // 目标时间
      remainingTime: 0, // 剩余时间
      intervalId: null // 定时器Id
    }
  },
  computed: {
    countdownText() {
      const days = Math.floor(this.remainingTime / (1000 * 60 * 60 * 24))
      const hours = Math.floor((this.remainingTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
      const minutes = Math.floor((this.remainingTime % (1000 * 60 * 60)) / (1000 * 60))
      const seconds = Math.floor((this.remainingTime % (1000 * 60)) / 1000)
      if (days) {
        return `${days} 天 ${hours} 小时 ${minutes} 分钟 ${seconds} 秒`
      } else if (hours) {
        return `${hours} 小时 ${minutes} 分钟 ${seconds} 秒`
      } else if (minutes) {
        return `${minutes} 分钟 ${seconds} 秒`
      } else if (seconds) {
        return `${seconds} 秒`
      }
    }
  },
  methods: {
    updateCountdown() {
      const now = new Date().getTime()
      this.remainingTime = this.targetTime - now
      if (this.remainingTime <= 0) {
        clearInterval(this.intervalId)
        this.remainingTime = 0
      }
    }
  },
  mounted() {
    this.updateCountdown()
    this.intervalId = setInterval(this.updateCountdown, 1000)
  },
  beforeDestroy() {
    clearInterval(this.intervalId)
  }
}
