<template>
  <div class="exam-t-box">
    <div class="title">距离答题开始还有</div>
    <div class="time-count">
      <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-txt">倒计时结束后，系统会自动进入答题页面</div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      clearTime: null,
      date: {
        num1: '0',
        num2: '0',
        num3: '0',
        num4: '0'
      }
    }
  },
  mounted() {
    clearInterval(this.clearTime)
    this.clearTime = setInterval(() => {
      this.countDown()
    }, 1000)
  },
  methods: {
    countDown() {
      // const date = JSON.parse(window.sessionStorage.getItem('examInfo')).start_time
      const date = JSON.parse(window.sessionStorage.getItem('examInfo')).start_time
      const nowtime = new Date()
      const endtime = new Date(date)
      const lefttime = parseInt((endtime.getTime() - nowtime.getTime()) / 1000)
      if (lefttime <= 0) {
        clearInterval(this.clearTime)
        this.date = {
          num1: '0',
          num2: '0',
          num3: '0',
          num4: '0'
        }
        this.$router.push({
          path: `/examAnswer/${JSON.parse(window.sessionStorage.getItem('examInfo')).exam_id}`
        })
      }
      const td = `${this.addZero(parseInt(lefttime / 60 % 60))}${this.addZero(parseInt(lefttime % 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>
.exam-t-box{
  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-count{
    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,.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-txt{
    text-align: center;
    font-size: 18px;
    color: #C01540;
    line-height: 25px;
    letter-spacing: 3px;
    margin-top: 40px;
  }
}
</style>
