<template>
  <div class="login-box">
    <div class="content">
      <div class="mian-cont900">
        <div class="title">{{ data.name }}</div>
        <div class="exam-time">
          <div class="tit">考试时间</div>
          <div class="line"></div>
          <div class="time">
            {{ data.start_time }} - {{ data.end_time }}
          </div>
        </div>
        <div class="mian-form">
          <!-- <div class="form prohibit"> -->
          <div :class="loginParam.is ? 'form prohibit' : 'form'">
            <div class="tips">{{ tips }}</div>
            <!-- 您登录的次数已超过最大限制 -->
            <input type="text" @click="fullScreen" v-model="examineeNumber" placeholder="请输入准考证号" v-show="!loginParam.is">
            <input type="text" @click="fullScreen" placeholder="请输入准考证号" readonly="readonly" class="prohibit" v-show="loginParam.is">
            <div class="btn" @click="login">登录</div>
            <!-- <div class="btn prohibit">登录</div> -->
            <div class="time-tips" v-show="loginParam.is">{{ loginParam.countTimeText }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="welcome-msg mian-cont900" v-if="data" v-html="data.config.welcome_message"></div>
    <!-- <my-dia :title="title" :prompt="prompt" :isPopup="isPopup" v-if="isPopup" @close="closePop">
        <template v-slot:button>
          <button @click="closePop">我知道了</button>
          <button>好的</button>
        </template>
    </my-dia> -->
  </div>
</template>
<script>
import action from '@action'
// import DialogComponent from '../../components/exam/dialogComponent.vue'
export default {
  components: {
    // 'my-dia': DialogComponent
  },
  data() {
    return {
      tips: '',
      claerExamTime: null,
      title: '提示',
      prompt: '不能退出全屏',
      data: '',
      examineeNumber: '',
      isPopup: false,
      loginParam: {
        clearTime: null,
        is: true,
        countTimeText: ''
      }
    }
  },
  mounted() {
    // window.onresize = () => {
    //   this.isPopup = true
    //   console.log(1111)
    // }
    action.Login.getExamInfo(this.$route.params.examId).then(res => {
      // this.$store.commit('setExamInfo', res)
      window.sessionStorage.setItem('examInfo', JSON.stringify(res))
      this.data = res
      if (this.getEnabledTime() < 0) {
        this.examTimeInit()
      } else {
        let claearTimes = null
        claearTimes = setInterval(() => {
          // console.log(this.getAfterTime())
          if (this.getAfterTime() <= 0) {
            this.tips = '超过登录时间'
            this.loginParam.is = true
            clearInterval(claearTimes)
          }
        }, 1000)
        this.loginParam.is = false
      }
    }).finally(res => {
    })
  },
  methods: {
    closePop() {
      this.fullScreen()
      this.isPopup = false
    },
    getAfterTime() {
      const curTime = new Date()
      const endtTime = new Date(this.data.end_time)
      const countTime = endtTime.getTime() - curTime.getTime()
      // const setStartTime = this.data.config.enabled_after
      //   ? new Date(startTime.setMinutes(startTime.getMinutes() + this.data.config.after_login))
      //   : curTime
      // const countTime = setStartTime.getTime() - curTime.getTime()
      return countTime
    },
    getEnabledTime() {
      const curTime = new Date()
      const setCurTime = this.data.config.enabled_before
        ? new Date(curTime.setMinutes(curTime.getMinutes() + this.data.config.before_login))
        : curTime
      const startTime = new Date(this.data.start_time)
      const countTime = setCurTime.getTime() - startTime.getTime()
      return countTime
    },
    examTimeInit() {
      this.loginParam.is = true
      this.claerExamTime = setInterval(() => {
        const date = this.getEnabledTime()
        if (date >= 0) {
          clearInterval(this.claerExamTime)
          this.loginParam.is = false
        } else {
          this.countDown(Math.abs(date))
        }
      }, 1000)
    },
    addZero(i) {
      return i < 10 ? '0' + i : i + ''
    },
    // 倒计时
    countDown(time) {
      const lefttime = parseInt((time) / 1000)
      const h = this.addZero(parseInt(lefttime / (60 * 60) % 24))
      const m = this.addZero(parseInt(lefttime / 60 % 60))
      const s = this.addZero(parseInt(lefttime % 60))
      this.loginParam.countTimeText = `距离开考还有: ${h} : ${m} : ${s}`
    },
    // 开启全屏
    fullScreen() {
      const el = document.documentElement
      const rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen || el.msRequestFullscreen
      if (typeof rfs !== 'undefined' && rfs) {
        rfs.call(el)
      }
      return false
    },
    // 点击登录
    login() {
      if (!this.loginParam.is) {
        action.Login.userLogin({ examinee_number: this.examineeNumber }).then(res => {
          // this.$store.commit('setStudentInfo', res)
          window.sessionStorage.setItem('studentInfo', JSON.stringify(res))
          const status = parseInt(res.sheet_status)
          if (status === 0) {
            this.$router.push({
              name: 'confirmInfo'
            })
          }
          status === 1 && (this.$alert('已提交考卷', {
            callback: action => {}
          }))
        }).catch(err => {
          this.$alert(err.message, {
            callback: action => {}
          })
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.welcome-msg{
  padding-top: 130px;
}
.mian-cont900{
  margin: 0 auto;
  width: 900px;
}
.login-box{
  width: 100%;
  height: 100%;
  background: #fff;
  .content{
    width: 100%;
    height: 387px;
    background: url(../../assets/images/login-bg.png);
    background-size: 100% 100%;
    .title{
      padding-top: 110px;
      font-size: 36px;
      font-weight: 600;
      color: #FFFFFF;
      line-height: 50px;
      letter-spacing: 7px;
      text-shadow: 0px 2px 4px rgba(0, 0, 0, 0.5);
    }
    .exam-time{
      padding-top: 20px;
      .tit{
        font-size: 18px;
        font-weight: bold;
        color: #FFFFFF;
        line-height: 25px;
        text-shadow: 0px 2px 4px rgba(0, 0, 0, 0.5);
      }
      .line{
        width: 24px;
        height: 2px;
        background: #FFFFFF;
        box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.5);
        margin: 10px 0;
      }
      .time{
        height: 25px;
        font-size: 18px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #FFFFFF;
        line-height: 25px;
        text-shadow: 0px 2px 4px rgba(0, 0, 0, 0.5);
      }
    }
    .mian-form{
      width: 100%;
      height: 230px;
      background: #FFFFFF;
      box-shadow: 0px 2px 12px 0px rgba(177, 186, 195, 0.3);
      border-radius: 5px;
      margin-top: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      .form{
        position: relative;
        display: flex;
        &.prohibit{
          input{
            background: #EEEEEE;
            border: 1px solid #CCCCCC;
          }
          .btn{
            color: #ccc;
            background: #eee;
          }
        }
      }
      input{
        padding-left: 15px;
        width: 585px;
        height: 40px;
        border-radius: 4px;
        border: 1px solid #CCCCCC;
        font-size: 14px;
        outline: none;
        &::-webkit-input-placeholder{
          color: #999999;
        }
        &.prohibit{
          background: #EEEEEE;
          border: 1px solid #CCCCCC;
        }
      }
      .btn{
        width: 180px;
        height: 42px;
        background: #C01540;
        border-radius: 4px;
        margin-left: 20px;
        font-size: 14px;
        font-weight: bold;
        color: #FFFFFF;
        line-height: 40px;
        text-align: center;
        font-style: normal;
        &.prohibit{
          color: #ccc;
          background: #eee;
        }
      }
      .tips{
        position: absolute;
        top: -80%;
        left: 0;
        font-size: 18px;
        font-weight: 400;
        color: #C01540;
        line-height: 25px;
      }
      .time-tips{
        position: absolute;
        bottom: -80%;
        left: 0;
        font-size: 18px;
        color: #222222;
        line-height: 25px;
      }
    }
  }
}
</style>
