<template>
  <div class="confirm-mian">
    <div class="con-info" v-if="!isTreaty">
      <div class="head">{{ examInfo.name || '' }}</div>
      <div class="box">
        <div class="info-form">
          <div class="tit">基本信息确认</div>
          <ul>
            <template v-for="(item, index) in allForm">
              <li :key="index" v-if="item.key == 'gender'">
                <div class="name">性别</div>
                <div class="select-btn">
                  <div :class="form.gender == 1 && 'active'" @click="selectGender(1, item.enabled_edit)">男</div>
                  <div :class="form.gender == 2 && 'active'" @click="selectGender(2, item.enabled_edit)">女</div>
                </div>
              </li>
              <li :key="index" v-else-if="item.key == 'educational_background'">
                <div class="name">学历</div>
                <el-select v-model="form.educational_background" placeholder="请选择" style="width: 100%" v-if="item.enabled_edit">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                </el-select>
                <el-select disabled v-model="form.educational_background" placeholder="请选择" style="width: 100%" v-else>
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                </el-select>
              </li>
              <li :key="index" v-else>
                <div class="name">{{ item.name }}</div>
                <input :type="typeInput(item.key)" v-model="form[item.key]" @keydown="keydown($event)" v-if="item.enabled_edit" />
                <input :type="typeInput(item.key)" v-model="form[item.key]" @keydown="keydown($event)" disabled v-else />
              </li>
            </template>
          </ul>
          <div class="confirm-btn" @click="confirmInfo">确定</div>
        </div>
      </div>
    </div>
    <div class="explain-box" v-if="isTreaty">
      <div class="head">{{ examInfo.name || '' }}</div>
      <div class="exp-text">
        <div class="t">
          <!-- <div class="fl">为保证本考试的公平性和严肃性，本次考试将会：</div> -->
          <div class="fl" v-html="examInfo.config.promise_message"></div>
        </div>
        <!-- <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 * as api from '@/api/base.js'
export default {
  data() {
    return {
      form: {
        educational_background: ''
      },
      allForm: [],
      isTreaty: false,
      value: '',
      options: [
        { value: '专科', label: '专科' },
        { value: '本科', label: '本科' },
        { value: '硕士', label: '硕士' },
        { value: '博士', label: '博士' },
        { value: '其他', label: '其他' }
      ]
    }
  },
  computed: {
    studentInfo() {
      return this.$store.state.studentInfo || {}
    },
    examInfo() {
      return this.$store.state.examInfo || {}
    },
    examId() {
      return this.examInfo.exam_id
    }
  },
  created() {
    this.setInput()
    this.defaultInto()
  },
  methods: {
    typeInput(k) {
      const type = k === 'age' || k === 'mobile' ? 'number' : 'text'
      return type
    },
    setInput() {
      const key = Object.keys(this.examInfo.config.info)
      const value = Object.values(this.examInfo.config.info)
      for (let i = 0; i < value.length; i++) {
        this.form[key[i]] = ''
        value[i].key = key[i]
      }
      this.allForm = this.dataFilter(value)
    },
    dataFilter(data) {
      return data.filter(item => {
        if (item.enabled_visible) {
          return item
        }
      })
    },
    // 禁止input输入空格
    keydown(event) {
      event.keyCode === 32 && (event.returnValue = false)
    },
    agree() {
      // 判断考试时间开始没有
      if (this.isExamStatus(this.examInfo.start_time) > 0) {
        this.$router.replace({ name: 'examTime', params: { examId: this.examId }, query: this.$route.query })
      } else {
        this.$router.replace({ name: 'examAnswer', params: { examId: this.examId }, query: this.$route.query })
      }
    },
    disagree() {
      this.$router.replace({ name: 'login', params: { examId: this.examId }, query: this.$route.query })
    },
    // 判断进入考试页面还是倒计时页面
    isExamStatus(date) {
      const curTime = new Date()
      const startTime = new Date(date)
      const countTime = startTime.getTime() - curTime.getTime()
      return countTime
    },
    // 选择男女
    selectGender(t, isInput) {
      if (isInput) {
        this.form.gender = t
        this.$forceUpdate()
        console.log(this.form)
      }
    },
    // 默认信息获取
    defaultInto() {
      const key = Object.keys(this.form)
      key.map(item => {
        this.form[item] = item === 'age' ? (parseInt(this.studentInfo[item]) === 0 ? '' : this.studentInfo[item]) : this.studentInfo[item]
      })
    },
    // 确认信息
    confirmInfo() {
      if (this.verification()) {
        api
          .confirmInfo(this.examInfo.exam_id, this.form)
          .then(res => {
            if (!res.code) {
              this.isTreaty = true
              this.$store.commit('setStudentInfo', res.info)
            }
          })
          .catch(err => {
            if (parseInt(err.status) === 403) {
              this.$alert(err.message, {
                callback: () => {
                  this.$router.replace({ name: 'login', params: { examId: this.examId }, query: this.$route.query })
                }
              })
            } else {
              this.$alert(err.message)
            }
          })
      }
    },
    // 验证信息是否为空
    verification() {
      for (let i = 0; i < this.allForm.length; i++) {
        if (this.allForm[i].required) {
          if (this.allForm[i].key === 'educational_background') {
            if (this.form.educational_background === '') {
              this.$alert('请完善信息')
              return false
            }
          } else {
            if (this.form[this.allForm[i].key] === '') {
              this.$alert('请完善信息')
              return false
            } else {
              if (this.allForm[i].key === 'mobile') {
                if (!/^1[3456789]\d{9}$/.test(this.form.mobile)) {
                  this.$alert('手机号格式不正确')
                  return false
                }
              }
              if (this.allForm[i].key === 'email') {
                if (!/^([a-zA-Z\d])(\w)+@[a-zA-Z\d]+\.[a-zA-Z]{2,4}$/.test(this.form.email)) {
                  this.$alert('邮箱格式不正确')
                  return false
                }
              }
            }
          }
        } else {
          if (this.form[this.allForm[i].key] !== '') {
            if (this.allForm[i].key === 'mobile') {
              if (!/^1[3456789]\d{9}$/.test(this.form.mobile)) {
                this.$alert('手机号格式不正确')
                return false
              }
            }
            if (this.allForm[i].key === 'email') {
              if (!/^([a-zA-Z\d])(\w)+@[a-zA-Z\d]+\.[a-zA-Z]{2,4}$/.test(this.form.email)) {
                this.$alert('邮箱格式不正确')
                return false
              }
            }
          }
        }
      }
      return true
    }
  }
}
</script>
<style lang="scss" scoped>
.box {
  display: flex;
  justify-content: center;
  background: url(../assets/images/con-info-bg.png) center;
  background-size: cover;
  background-attachment: fixed;
  background-position: 0 0;
  padding-bottom: 100px;
  min-height: 100%;
}
.confirm-mian {
  width: 100%;
  height: 100%;
}
.con-info {
  width: 100%;
  height: 100%;
  .head {
    position: sticky;
    top: 0;
    height: 80px;
    background: #ffffff;
    font-size: 24px;
    font-weight: bold;
    color: #222222;
    line-height: 80px;
    padding-left: 30px;
  }
  .info-form {
    height: max-content;
    margin-top: 40px;
    width: 400px;
    background: #ffffff;
    border-radius: 8px;
    padding-bottom: 30px;
    .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;
      cursor: pointer;
    }
  }
}
.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;
    .t {
      font-size: 18px;
      font-weight: bold;
      color: #222222;
      line-height: 25px;
      display: flex;
      &::v-deep p {
        margin: 0;
      }
    }
  }
  .exp-btn {
    position: absolute;
    bottom: 40px;
    left: 50%;
    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;
        cursor: pointer;
        &:nth-child(2) {
          border-radius: 4px;
          border: 1px solid #cccccc;
          background: none;
          color: #999;
          margin-left: auto;
        }
      }
    }
  }
}
.none {
  display: none;
}
</style>
