<template>
  <div class="countdown-container">
    <div class="title">距离答题开始还有</div>
    <div class="time">
      <div class="time-block">{{ date.num1 }}</div>
      <div class="time-block">{{ date.num2 }}</div>
      <div class="spots"><span></span><span></span></div>
      <div class="time-block">{{ date.num3 }}</div>
      <div class="time-block">{{ date.num4 }}</div>
    </div>
    <div class="tips">倒计时结束后，系统会自动进入答题页面</div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      timer: null,
      date: { num1: '0', num2: '0', num3: '0', num4: '0' }
    }
  },
  computed: {
    examInfo() {
      return this.$store.state.examInfo || {}
    },
    examId() {
      return this.examInfo.exam_id
    }
  },
  mounted() {
    clearInterval(this.timer)
    this.timer = setInterval(() => {
      this.countDown()
    }, 1000)
  },
  methods: {
    countDown() {
      const nowTime = new Date()
      const startTime = new Date(this.examInfo.start_time)
      const distanceTime = parseInt((startTime.getTime() - nowTime.getTime()) / 1000)
      if (distanceTime <= 0) {
        clearInterval(this.timer)
        this.date = { num1: '0', num2: '0', num3: '0', num4: '0' }
        setTimeout(() => {
          this.$router.replace({ name: 'examAnswer', params: { examId: this.examId }, query: this.$route.query })
        }, 2000)
      }
      const td = `${this.addZero(parseInt((distanceTime / 60) % 60))}${this.addZero(parseInt(distanceTime % 60))}`
      const arr = td.split('')
      Object.keys(this.date).map((item, index) => {
        this.date[item] = arr[index]
      })
    },
    addZero(i) {
      return i < 10 ? '0' + i : i + ''
    }
  }
}
</script>
<style lang="scss" scoped>
.countdown-container {
  width: 100%;
  height: 100%;
  background: url(../assets/images/exam-t-bg.png);
  background-size: cover;
  .title {
    font-size: 36px;
    font-weight: bold;
    color: #262626;
    line-height: 50px;
    letter-spacing: 30px;
    text-align: center;
    padding-top: 170px;
  }
  .time {
    display: flex;
    justify-content: center;
    padding-top: 40px;
    // justify-items: center;
    // align-items: center;
    .time-block {
      width: 182px;
      height: 182px;
      background: rgba(255, 255, 255, 0.5);
      font-size: 100px;
      font-weight: bold;
      color: #2b2b2b;
      line-height: 182px;
      text-align: center;
      // justify-content: center;
      // justify-items: center;
      align-items: center;
      margin-right: 10px;
    }
    .spots {
      padding: 0 20px 0 10px;
      span {
        display: block;
        width: 17px;
        height: 17px;
        background: #999999;
        border-radius: 50%;
        margin-top: 48px;
      }
    }
  }
  .tips {
    text-align: center;
    font-size: 18px;
    color: #c01540;
    line-height: 25px;
    letter-spacing: 3px;
    margin-top: 40px;
  }
}
</style>
