<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 action from '@action'
export default {
  data() {
    return {
      sInfo: JSON.parse(window.localStorage.getItem('studentInfo')),
      examInfo: JSON.parse(window.localStorage.getItem('examInfo')),
      form: {
        educational_background: ''
      },
      allForm: [],
      isTreaty: false,
      value: '',
      options: [
        {
          value: '专科',
          label: '专科'
        },
        {
          value: '本科',
          label: '本科'
        },
        {
          value: '硕士',
          label: '硕士'
        },
        {
          value: '博士',
          label: '博士'
        },
        {
          value: '其他',
          label: '其他'
        }
      ]
    }
  },
  created() {
    this.setInput()
    this.defaultInto()
  },
  mounted() {
    // console.log(this.examInfo.config.info)
  },
  computed: {
    typeInput() {
      return (k) => {
        const type = k === 'age' || k === 'mobile' || k === 'id_number' ? 'number' : 'text'
        return type
      }
    }
  },
  methods: {
    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)
      console.log(this.dataFilter(value))
      // console.log(this.form, '===')
      // this.allForm = value
    },
    dataFilter(data) {
      return data.filter(item => {
        if (item.enabled_visible) {
          return item
        }
      })
    },
    // 禁止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.replace({
            path: '/examTime'
          })
        })()
        : (() => {
          this.$router.replace({
            path: `/examAnswer/${this.examInfo.exam_id}`
          })
        })()
    },
    disagree() {
      this.$router.replace({
        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, 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.sInfo.info[item]) === 0
            ? ''
            : this.sInfo.info[item]
          : this.sInfo.info[item]
      })
    },
    // 确认信息
    confirmInfo() {
      if (this.verification()) {
        action.Login.confirmInfo(this.examInfo.exam_id, this.form).then(res => {
          if (!res.code) {
            this.isTreaty = true
          }
        }).catch(err => {
          this.$alert(err.message, {
            callback: action => {}
          })
        })
      }
    },
    // 验证信息是否为空
    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
                }
              }
            }
          }
        }
      }
      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;
    }
  }
}
.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%;
    -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>
