<template>
  <div class="confirm-mian">
    <div class="con-info" v-if="!isTreaty">
      <div class="head">全国统一高考试卷A</div>
      <div class="info-form">
        <div class="tit">基本信息确认</div>
        <ul>
          <li>
            <div class="name">姓名</div>
            <input type="text" v-model="form.name" @keydown="keydown($event)">
          </li>
          <li>
            <div class="name">学号</div>
            <input type="text" v-model="form.student_number" @keydown="keydown($event)">
          </li>
          <li>
            <div class="name">性别</div>
            <div class="select-btn">
              <div :class="form.gender == 1 && 'active'" @click="selectGender(1)">男</div>
              <div :class="form.gender == 2 && 'active'" @click="selectGender(2)">女</div>
            </div>
          </li>
        </ul>
        <div class="confirm-btn" @click="confirmInfo">确定</div>
      </div>
    </div>
    <div class="explain-box" v-if="isTreaty">
      <div class="head">全国统一高考试卷A</div>
      <div class="exp-text">
        <p>为保证本考试的公平性和严肃性，本次考试将会：{{examInfo.config.promise_message}}</p>
        <!-- <p v-html="examInfo.config.promise_message"></p> -->
      </div>
      <div class="exp-btn">
        <div class="tips">点击“我同意”按钮接受以上内容。如果您不同意这些条款，将无法进行考试</div>
        <div class="btn-box">
          <div @click="agree">我同意</div>
          <div @click="disagree">不同意</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import action from '@action'
export default {
  data() {
    return {
      sInfo: JSON.parse(window.sessionStorage.getItem('studentInfo')),
      examInfo: JSON.parse(window.sessionStorage.getItem('examInfo')),
      form: {
        name: '',
        student_number: this.$store.state.studentInfo.student_number,
        gender: 0
      },
      isTreaty: false
    }
  },
  mounted() {
    this.defaultInto()
  },
  methods: {
    // 禁止input输入空格
    keydown (event) {
      event.keyCode === 32 && (event.returnValue = false)
    },
    agree() {
      // 判断考试时间开始没有
      // console.log(this.isExamStatus(this.examInfo.start_time > 0))
      this.isExamStatus(this.examInfo.start_time) > 0
        ? (() => {
          this.$router.push({
            name: 'examTime'
          })
        })()
        : (() => {
          this.$router.push({
            path: `/examAnswer/${this.examInfo.exam_id}`
          })
        })()
    },
    disagree() {
      this.$router.push({
        path: `/login/${this.examInfo.exam_id}`
      })
    },
    // 判断进入考试页面还是倒计时页面
    isExamStatus(date) {
      const curTime = new Date()
      const startTime = new Date(date)
      const countTime = startTime.getTime() - curTime.getTime()
      return countTime
    },
    // 选择男女
    selectGender(t) {
      this.form.gender = t
    },
    // 默认信息获取
    defaultInto() {
      const key = Object.keys(this.form)
      key.map(item => {
        this.form[item] = this.sInfo.info[item]
      })
    },
    // 确认信息
    confirmInfo() {
      const val = Object.values(this.form)
      console.log(this.verification(val))
      if (this.verification(val)) {
        action.Login.confirmInfo(this.form).then(res => {
          if (!res.code) {
            this.isTreaty = true
          }
        }).catch(err => {
          this.$alert(err.message, {
            callback: action => {}
          })
        })
      }
    },
    // 验证信息是否为空
    verification(val) {
      console.log(val)
      for (let i = 0; i < val.length; i++) {
        if (val[i] === '' || val[i] === 0 || val[i] === '0') {
          this.$alert('请完善信息')
          return false
        }
      }
      return true
    }
  }
}
</script>
<style lang="scss" scoped>
.confirm-mian{
  width: 100%;
  height: 100%;
}
.con-info{
  width: 100%;
  height: 100%;
  background: url(../../assets/images/con-info-bg.png);
  background-size: cover;
  .head{
    // width: 100%;
    height: 80px;
    background: #FFFFFF;
    font-size: 24px;
    font-weight: bold;
    color: #222222;
    line-height: 80px;
    padding-left: 30px;
  }
  .info-form{
    // margin: 0 auto;
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%,-50%);
    margin-top: 40px;
    width: 400px;
    height: 458px;
    background: #FFFFFF;
    border-radius: 8px;
    .tit{
      text-align: center;
      margin-top: 30px;
      font-size: 18px;
      font-weight: bold;
      color: #222222;
      line-height: 25px;
    }
    ul{
      width: 320px;
      margin: 0 auto 0 auto;
      padding: 0;
      list-style: none;
      li{
        margin-bottom: 20px;
        .name{
          font-size: 14px;
          color: #222222;
          line-height: 20px;
          margin-bottom: 10px;
        }
        input{
          width: 310px;
          height: 45px;
          border-radius: 2px;
          border: 1px solid #F1F1F1;
          font-size: 14px;
          color: #222222;
          padding: 0 0 0 10px;
          outline: none;
        }
        .select-btn{
          width: 100%;
          display: flex;
          div{
            width: 150px;
            height: 40px;
            border-radius: 4px;
            border: 1px solid #CCCCCC;
            font-size: 14px;
            font-weight: bold;
            color: #999999;
            line-height: 14px;
            text-align: center;
            line-height: 40px;
            &:nth-child(2){
              margin-left: auto;
            }
            &.active{
              background: #C01540;
              color: #fff;
              border: none;
            }
          }
        }
      }
    }
    .confirm-btn{
      width: 320px;
      height: 40px;
      background: #C01540;
      border-radius: 4px;
      font-size: 14px;
      font-weight: bold;
      color: #FFFFFF;
      line-height: 14px;
      margin: 0 auto;
      text-align: center;
      line-height: 40px;
      margin-top: 30px;
    }
  }
}
.explain-box{
  width: 100%;
  min-height: 100%;
  background: #F9F9F9;
  .head{
    height: 80px;
    background: #FFFFFF;
    font-size: 24px;
    font-weight: bold;
    color: #222222;
    line-height: 80px;
    padding-left: 30px;
  }
  .exp-text{
    width: 900px;
    background: #FFFFFF;
    border-radius: 8px;
    padding: 40px 40px 80px;
    margin: 40px auto 0;
    margin-top: 40px;
    p{
      font-size: 18px;
      font-weight: bold;
      color: #222222;
      line-height: 25px;
    }
  }
  .exp-btn{
    position: absolute;
    bottom: 40px;
    left: 50%;
    -webkit-transform: translateX(-50%);
    .tips{
      font-size: 14px;
      color: #222222;
      line-height: 20px;
    }
    .btn-box{
      width: 240px;
      display: flex;
      margin: 20px auto 0;
      div{
        width: 100px;
        height: 40px;
        background: #C01540;
        border-radius: 4px;
        font-size: 14px;
        font-weight: bold;
        color: #FFFFFF;
        line-height: 40px;
        text-align: center;
        &:nth-child(2){
          border-radius: 4px;
          border: 1px solid #CCCCCC;
          background: none;
          color: #999;
          margin-left: auto;
        }
      }
    }
  }
}
.none{
  display: none;
}
</style>
