<template>
  <div class="login-box">
    <div class="content">
      <div class="mian-cont900 por">
        <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"
              v-model="examineeNumber"
              placeholder="请输入准考证号"
              v-show="!loginParam.is"
              @keydown="keydown($event)"
            />
            <input
              type="text"
              placeholder="请输入准考证号"
              readonly="readonly"
              class="prohibit"
              v-show="loginParam.is"
            />
            <div class="btn" @click="login">登录2</div>
            <!-- 11 -->
            <!-- <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>
  </div>
</template>
<script>
import action from '@action'
// import DialogComponent from '../../components/exam/dialogComponent.vue'
export default {
  components: {
    // 'my-dia': DialogComponent
  },
  metaInfo() {
    return {
      title: JSON.parse(window.localStorage.getItem('examInfo')).name || ''
    }
  },
  data() {
    return {
      tips: '',
      claerExamTime: null,
      title: '提示',
      prompt: '不能退出全屏',
      data: '',
      examineeNumber: '',
      isPopup: false,
      loginParam: {
        clearTime: null,
        is: true,
        countTimeText: ''
      }
    }
  },
  mounted() {
    this.getExamInfo()
    this.tipsMobileAngle()
    window.onresize = () => {
      this.tipsMobileAngle()
    }
  },
  methods: {
    tipsMobileAngle() {
      if (this.isMobile()) {
        const winWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0
        const winHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0
        if (winHeight > winWidth) {
          console.log(1)
          this.$message({
            message: '请横屏预览',
            type: 'warning'
          })
        }
      }
    },
    isMobile() {
      return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
    },
    getExamInfo() {
      action.Login.getExamInfo(this.$route.params.examId).then(res => {
        window.localStorage.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
        }
      })
    },
    sendExamInfo(studentId) {
      const param = {
        student_id: studentId,
        exam_id: this.data.exam_id,
        status: 2
      }
      action.Login.sendExamInfo(param)
        .then(res => {})
        .catch(err => {
          console.log(err)
        })
    },
    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()
      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
    },
    // 禁止input输入空格
    keydown(event) {
      console.log(event.keyCode)
      if (event.keyCode === 32 || event.keyCode === 187 || event.keyCode === 107) {
        event.returnValue = false
      }
    },
    // 点击登录
    login() {
      if (!this.loginParam.is) {
        if (this.examineeNumber === '') {
          this.$alert('请输入准考证号')
          return false
        }
        action.Login.userLogin(this.data.exam_id, { examinee_number: this.examineeNumber })
          .then(res => {
            window.localStorage.setItem('studentInfo', JSON.stringify(res))
            const status = parseInt(res.sheet_status)
            // this.sendExamInfo(res.info.student_id)
            if (status === 0) {
              this.$router.replace({
                name: 'confirmInfo'
              })
            }
            status === 1 && this.$alert('已提交考卷')
          })
          .catch(err => {
            if (err.message.indexOf('error') !== -1) {
              this.$alert('网络异常，请保持网络通畅', {
                callback: action => {}
              })
            } else {
              this.$alert(err.message, {
                callback: action => {}
              })
            }
          })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.welcome-msg {
  padding-top: 130px;
  &::v-deep img {
    width: 100%;
    display: block;
  }
}
.mian-cont900 {
  margin: 0 auto;
  width: 900px;
  &.por {
    position: absolute;
    bottom: 0;
    left: 50%;
    -webkit-transform: translateX(-50%);
    margin-bottom: -115px;
  }
}
.login-box {
  width: 100%;
  height: 100%;
  background: #fff;
  .content {
    position: relative;
    width: 100%;
    height: 50%;
    // 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>
