<template>
  <div class="validation">
    <div class="container">
      <div class="content">
        <div class="content-top">
          <h3>添加密保设置</h3>
          <div class="step">
            <img src="/images/all/step02.png" alt=""/>
            <div class="step-item"><span class="active">账号验证</span><span class="active">密保设置</span><span>设置完成</span></div>
          </div>
        </div>
        <div class="content-bottom">
          <div>
            <el-form :model="valid2" :rules="rules2" ref="valid2" label-width="100px" class="demo-ruleForm">
              <el-form-item prop="question1">
                <el-select v-model="valid2.question1" placeholder="问题一">
                  <el-option label="您的母亲的生日是几月几日？" value="您的母亲的生日是几月几日？"></el-option>
                  <el-option label="您最喜欢的宠物叫什么名字？" value="您最喜欢的宠物叫什么名字"></el-option>
                  <el-option label="您最喜欢的电影片名是什么？" value="您最喜欢的电影片名是什么？"></el-option>
                  <el-option label="您的学号是？" value="您的学号是？"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="answer1">
                <el-input v-model="valid2.answer1" placeholder="答案一"></el-input>
              </el-form-item>
              <el-form-item prop="question2">
              <el-select v-model="valid2.question2" placeholder="问题一">
                <el-option label="您的爷爷叫什么名字？" value="您的爷爷叫什么名字？"></el-option>
                <el-option label="您的配偶的生日是几月几日？" value="您的配偶的生日是几月几日？"></el-option>
                <el-option label="您最喜欢的歌手是谁？" value="您最喜欢的歌手是谁？"></el-option>
                <el-option label="您就读的小学学校全称是？" value="您就读的小学学校全称是？"></el-option>
              </el-select>
              </el-form-item>
              <el-form-item prop="answer2">
                <el-input v-model="valid2.answer2" placeholder="答案二"></el-input>
              </el-form-item>
              <el-form-item>
                <a class="btn finish"
                   :disabled="!answer1SecondChecked || !question1SecondChecked || !answer2SecondChecked || !question2SecondChecked"
                   @click="questionSubmit">提交</a>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
      <loading v-show="isShowLoading"/>
    </div>
  </div>
</template>

<script>
  import Loading from '~components/common/loading/Loading.vue'
  export default {
    name: 'validation',
    components: {
      Loading
    },
    data () {
//      第二步验证手机
      var validateSecondAnswer1 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请填写正确的答案'))
          this.answer1SecondChecked = false
        } else {
          if (this.valid2.answer1 !== '') {
            this.answer1SecondChecked = true
          }
          callback()
        }
      }
      var validateSecondAnswer2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请填写正确的答案'))
          this.answer2SecondChecked = false
        } else {
          if (this.valid2.answer2 !== '') {
            this.answer2SecondChecked = true
          }
          callback()
        }
      }
      var validateSecondQuestion1 = (rule, value, callback) => {
        if (value === '') {
          this.question1SecondChecked = false
          callback(new Error('请选择问题'))
        } else {
          this.question1SecondChecked = true
          callback()
        }
      }
      var validateSecondQuestion2 = (rule, value, callback) => {
        if (value === '') {
          this.question2SecondChecked = false
          callback(new Error('请选择问题'))
        } else {
          this.question2SecondChecked = true
          callback()
        }
      }
      return {
        isShowLoading: false,
        answer1SecondChecked: false,
        question1SecondChecked: false,
        answer2SecondChecked: false,
        question2SecondChecked: false,
        valid2: {
          answer1: '',
          question1: '',
          answer2: '',
          question2: ''
        },
        rules2: {
          question1: [
            { validator: validateSecondQuestion1, required: true, message: '请选择问题', trigger: 'change' }
          ],
          answer1: [
            {validator: validateSecondAnswer1, trigger: 'blur'}
          ],
          question2: [
            { validator: validateSecondQuestion2, required: true, message: '请选择问题', trigger: 'change' }
          ],
          answer2: [
            {validator: validateSecondAnswer2, trigger: 'blur'}
          ]
        }
      }
    },
    computed: {
      firstStepToken () {
        return this.$store.state.login.token.data
      }
    },
    mounted () {
//    获取邮箱token
      this.$nextTick(() => {
        this.getEmailLinkToken()
      })
//      密保问题
//      this.$http.get('/data/question.json').then(response => {
//        this.questionOne = response.data.questionOne
//        this.questionTwo = response.data.questionTwo
//      })
    },
    methods: {
//      设置密保
      questionSubmit () {
        if (this.answer1SecondChecked && this.question1SecondChecked && this.answer2SecondChecked && this.question2SecondChecked) {
          this.isShowLoading = true
          let param = new FormData()
          let userQuestion = []
          userQuestion.push({'question': this.valid2.question1, 'answer': this.valid2.answer1, 'sort': '1'}, {'question': this.valid2.question2, 'answer': this.valid2.answer2, 'sort': '2'})
          let userQuestions = JSON.stringify(userQuestion)
          param.append('userQuestions', userQuestions)
          param.append('token', this.firstStepToken)
          let config = {
            headers: {'Content-Type': 'multipart/form-data'}
          }
          this.$http.post(`/update/user/setQuestion`, param, config)
            .then(response => {
              if (response.data.success) {
                this.isShowLoading = false
                this.$router.push({ path: '/encrypted-setting/EncryptedSettingThirdStep' })
              } else {
                return Promise.reject(response.data)
              }
            }).catch(err => {
              this.isShowLoading = false
              this.$message.error(err.errMsg)
            })
        }
      },
//      获得邮箱token
      getEmailLinkToken () {
        var url = window.location.search
        var request = {}
        if (url.indexOf('?' !== -1)) {
          var str = url.substr(1)
          var strs = str.split('&')
          for (var i = 0; i < strs.length; i++) {
            request[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1])
          }
        }
        this.Token = request['token'] || ''
        if (this.Token) {
          this.$store.commit('login/GET_TOKEN', this.Token)
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .validation {
    margin: 0 auto;
    width: 100%;
    background: #eee;
  .container{
    padding-top: 50px;
    margin: 0 auto;
    width: 980px;
    text-align: center;
  .content{
    padding: 0 50px;
    margin: 50px auto 0;
    width: 100%;
    /*height: 540px;*/
    text-align: center;
    background: #fff;
  .content-top{
    height: 80px;
    line-height: 80px;
  h3{
    margin-bottom: 0;
    font-size: 24px;
    color: #000;
    border-bottom: 1px solid #dcdcdc;
  }
  .step{
    position: relative;
    margin-top: 10px;
  img{
    width: 315px;
    height: 46px;
  }
  .step-item{
    position: absolute;
    top: 45px;
    left: 265px;
  span{
    margin-right: 78px;
    font-size: 14px;
    color: #b4b4b4;
  }
  span.active {
    color: #0076ad;
  }
  }
  }
  }
  form {
    margin-top: 150px;
    padding-bottom: 44px;
  input{
    padding: 0 0 0 18px;
    width: 360px;
    height: 44px;
    line-height: 44px;
    font-size: 14px;
    color: #000;
    border-radius: 0;
  }
  input.answer {
    background: url("/images/all/more.png") no-repeat 325px center;
    cursor: pointer;
  }
  ul{
    display: none;
    position: absolute;
    top: 44px;
    left: 0;
    width: 360px;
    background: #fff;
    box-shadow: 0 0 5px rgba(0,0,0,.5);
    -moz-box-shadow: 0 0 5px rgba(0,0,0,.5);
    -o-box-shadow: 0 0 5px rgba(0,0,0,.5);
    -webkit-box-shadow: 0 0 5px rgba(0,0,0,.5);
    z-index: 10;
  li{
    padding-left: 18px;
    width: 100%;
    height: 30px;
    line-height: 30px;
    text-align: left;
    font-size: 14px;
    color: #000;
    cursor: pointer;
  &:hover{
     background: #0076ad;
     color: #fff;
   }
  }
  }
  span.tip{
    position: absolute;
    top: 0;
    right: -238px;
    font-size: 13px;
    color: #8c8c8c;
  a{
    font-size: 13px;
    color: #0076ad;
  }
  }
  span.tip.codeError-tip{
    position: absolute;
    top: 3px;
    left: 378px;
    width: 200px;
    text-align: left;
    color: #ff4949;
    font-size: 12px;
  }
  i{
    position: absolute;
    top: 13px;
    left: 20px;
    font-size: 20px;
    color: #a0a0a0;
  }
  .btn {
    margin: 34px 0 16px 0;
    width: 360px;
    height: 44px;
    line-height: 44px;
    font-size: 16px;
    color: #fff;
    background: #0076AD;
    border-radius: 3px;
  }
  }
  .content-bottom{
    margin: 155px auto 0;
    padding-bottom: 50px;
    width: 360px;
  div.warp{
    padding-bottom: 65px;
  }
  p{
    font-size: 24px;
    color: #323232;
  img{
    margin-right: 20px;
    width: 30px;
    height: 28px;
  }
  }
  p.pass{
    font-size: 24px;
    color: #e77405;
  img{
    height: 30px;
  }
  }
  p.passed {
    color: #2ab300;
  img{
    height: 30px;
  }
  }
  span{
    display: inline-block;
    font-size: 14px;
    color: #8b8b8b;
  }
  span.close-tip{
    margin: 15px 0 140px 0;
  }
  .close-btn{
    margin: 0 auto;
    width: 200px;
    height: 36px;
    line-height: 36px;
    font-size: 14px;
    text-align: center;
    color: #323232;
    border: 1px solid #d2d2d2;
    border-radius: 3px;
    cursor: pointer ;
  }
  span.use{
    display: inline-block;
    margin-bottom: 30px;
    width: 360px;
    font-size: 14px;
    color: #000;
    text-align: left;
  em{
    font-size: 14px;
    font-style: normal;
    color: #000;
  }
  }
  .form-group {
    margin: 0 auto 16px;
    position: relative;
    width: 360px;
    height: 44px;
    line-height: 44px;
  input{
    padding: 0 0 0 18px;
    width: 360px;
    height: 44px;
    line-height: 44px;
    font-size: 14px;
    color: #000;
    border-radius: 0;
  }
  input.msg{
    float: left;
    width: 210px;
    padding: 0 0 0 18px;
    height: 44px;
    line-height: 44px;
    font-size: 14px;
    color: #000;
    border-radius: 0;
  }
  span.msg{
    float: right;
    margin: 0;
    width: 130px;
    height: 44px;
    line-height: 44px;
    text-align: center ;
    font-size: 14px;
    color: #5a5a5a;
    background: #f4f4f4;
    border: 1px solid #dcdcdc;
    cursor: pointer;
  }
  span.msg.send{
    background: #d2d2d2;
    color: #fff;
  }
  }
  .btn {
    margin: 34px 0 10px 0;
    width: 360px;
    height: 44px;
    line-height: 44px;
    font-size: 16px;
    color: #fff;
    background: #0076AD;
    border-radius: 3px;
  }
  }
  .choose{
    margin: 155px auto 0;
    padding-bottom: 44px;
  div{
    padding: 0 15px;
    margin: 0 auto 16px;
    width: 360px;
    height: 60px;
    line-height: 60px;
    text-align: left;
    overflow: hidden;
    border: 1px solid #d2d2d2;
    cursor: pointer;
  &:hover,&.active{
             border-color: #0076ad;
  span{
    color: #0076ad;
  }
  i.second {
    color: #0076ad;
  }
  }
  img.first{
    float: left;
    margin: 24px 20px 0 0;
    font-size: 20px;
    color: #323232;
  }
  img.first.mob{
    margin: 22px 20px 0 5px;
    font-size: 28px;
  }
  i.second {
    float: right;
    margin: 20px 0 0 5px;
    font-size: 20px;
    color: #323232;
  }
  span{
    float: left;
    font-size: 14px;
    color: #323232;
  }
  }
  }
  a.return{
    position: absolute;
    left: 0;
    top: -15px;
  img{
    width: 34px !important;
    height: 34px !important;
  }
  }
  }
  }
  }
</style>
