<template>
  <div class="login-box">
    <div class="content">
      <div class="main-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="main-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">登录</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 main-cont900" v-if="data && data.config" v-html="data.config.welcome_message"></div>
  </div>
</template>
<script>
import * as api from '@/api/base.js'

export default {
  data() {
    return {
      tips: '',
      clearExamTime: null,
      title: '提示',
      prompt: '不能退出全屏',
      data: {},
      examineeNumber: '',
      isPopup: false,
      loginParam: {
        clearTime: null,
        is: true,
        countTimeText: ''
      }
    }
  },
  computed: {
    examInfo() {
      return this.$store.state.examInfo || {}
    },
    examId() {
      return this.examInfo.exam_id
    }
  },
  watch: {
    examInfo: {
      deep: true,
      immediate: true,
      handler() {
        this.examInfo.exam_id && this.getExamInfo()
      }
    }
  },
  mounted() {
    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) {
          this.$message({ message: '请横屏预览', type: 'warning' })
        }
      }
    },
    isMobile() {
      return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
    },
    getExamInfo() {
      this.data = this.examInfo
      if (this.getEnabledTime() < 0) {
        this.examTimeInit()
      } else {
        let clearTimes = null
        clearTimes = setInterval(() => {
          if (this.getAfterTime() <= 0) {
            this.tips = '超过登录时间'
            this.loginParam.is = true
            clearInterval(clearTimes)
          }
        }, 1000)
        this.loginParam.is = false
      }
    },
    sendExamInfo(studentId) {
      const param = { student_id: studentId, exam_id: this.examId, status: 2 }
      api.sendExamInfo(param)
    },
    closePop() {
      this.fullScreen()
      this.isPopup = false
    },
    getAfterTime() {
      const curTime = new Date()
      const endTime = new Date(this.data.end_time)
      const countTime = endTime.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.clearExamTime = setInterval(() => {
        const date = this.getEnabledTime()
        if (date >= 0) {
          clearInterval(this.clearExamTime)
          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 datSec = 24 * 60 * 60
      const hourSer = 60 * 60
      const minuteSec = 60
      const dd = Math.floor(leftTime / datSec)
      const hh = Math.floor((leftTime % datSec) / hourSer)
      const mm = Math.floor((leftTime % hourSer) / minuteSec)
      const ss = leftTime % minuteSec
      this.loginParam.countTimeText =
        dd > 0
          ? `距离开考还有: ${dd}天${this.addZero(hh)}时${this.addZero(mm)}分${this.addZero(ss)}秒`
          : `距离开考还有: ${this.addZero(hh)}时${this.addZero(mm)}分${this.addZero(ss)}秒`
    },
    // 开启全屏
    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) {
      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
        }
        api.userLogin(this.examId, { examinee_number: this.examineeNumber }).then(res => {
          this.$store.commit('setStudentInfo', res.info)
          const status = parseInt(res.sheet_status)
          // this.sendExamInfo(res.info.student_id)
          if (status === 0) {
            this.$router.replace({ name: 'confirmInfo', params: { examId: this.examId }, query: this.$route.query })
          } else if (status === 1) {
            this.$alert('试卷已提交')
          }
        })
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.welcome-msg {
  padding-top: 130px;
  &::v-deep img {
    width: 100%;
    display: block;
  }
}
.main-cont900 {
  margin: 0 auto;
  width: 900px;
  &.por {
    position: absolute;
    bottom: 0;
    left: 50%;
    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%;
    padding-top: 60px;
    .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);
      }
    }
    .main-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;
        cursor: pointer;
        &.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>
