<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="newAdminName">
                <el-input v-model="valid2.newAdminName" placeholder="新管理员姓名"></el-input>
              </el-form-item>
              <el-form-item prop="mobile">
                <el-input v-model="valid2.mobile" placeholder="新管理员手机号"></el-input>
              </el-form-item>
              <el-form-item prop="code">
                <el-input type="text" v-model="valid2.code"
                          v-bind:class="{ active: secondStepCodeErrorChecked }"
                          auto-complete="off"
                          class="msg"
                          placeholder="短信验证码"></el-input>
                <el-button type="primary" class="code"
                           v-show="showSecondStepCode"
                           @click="getSecondCheckCode"
                           :disabled="getCodeBtnIsDisabled">获取验证码</el-button>
                <el-button type="primary"  v-show="!showSecondStepCode" class="code code-send">已发送({{second_step_time}}s)</el-button>
                <span v-show="secondStepCodeErrorChecked" class="tip codeError-tip">{{secondCodeErrorMsg}}</span>
              </el-form-item>
              <el-form-item prop="changeReason">
                <el-input type="textarea" v-model="valid2.changeReason" placeholder="申诉说明"></el-input>
                <span class="tip description" v-show="descriptionTip">请描述您申诉的原因，并尽可能多地列举出证明此企业账号为您所有的证据</span>
              </el-form-item>
              <el-form-item prop="contactTel">
                <el-input type="text" v-model="valid2.contactTel" auto-complete="off" placeholder="联系电话"></el-input>
              </el-form-item>
              <el-form-item>
                <a class="btn finish"
                    :disabled="!newAdminNameChecked || !mobileSecondChecked || !secondCodeChecked || !changeReasonChecked || !contactTelChecked"
                    @click="setNewMobile">确认</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
    },
    props: ['tokenId'],
    middleware: 'authenticated',
    data () {
//      第二步验证手机
      var validateNewAdminName = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请填写新管理员姓名'))
          this.newAdminNameChecked = false
        } else {
          if (this.valid2.newAdminName !== '') {
            if (value.length >= 20) {
              callback(new Error('输入长度过长，20个字符以内'))
              this.newAdminNameChecked = false
            } else {
              this.newAdminNameChecked = true
            }
          }
          callback()
        }
      }
      var validateSecondMobile = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请填写正确的手机号'))
          this.getCodeBtnIsDisabled = true
          this.mobileSecondChecked = false
        } else {
          if (this.valid2.mobile !== '') {
            var reg = /^1[0-9]{10}$/
            if (!reg.test(value)) {
              callback(new Error('请填写正确的手机号'))
              this.getCodeBtnIsDisabled = true
              this.mobileSecondChecked = false
            } else {
              this.getCodeBtnIsDisabled = false
              this.mobileSecondChecked = true
            }
          }
          callback()
        }
      }
      var validateSecondCode = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请填写正确的验证码'))
          this.secondStepCodeErrorChecked = false
          this.secondCodeChecked = false
        } else {
          if (this.valid2.code !== '') {
            if (this.valid2.mobile === '') {
              callback(new Error('请先填写正确的手机号'))
            } else {
              if (this.secondToken) {
                let param = new FormData()
                param.append('mobile', this.valid2.mobile)
                param.append('code', this.valid2.code)
                param.append('token', this.secondToken)
                let config = {
                  headers: {'Content-Type': 'multipart/form-data'}
                }
                this.$http.post(`/sso/change/admin/check/newMobile`, param, config)
                  .then(response => {
                    if (response.data.success) {
                      this.secondCodeChecked = true
                      this.secondStepCodeErrorChecked = false
                    } else {
                      this.secondStepCodeErrorChecked = true
                      this.secondCodeChecked = false
                      return Promise.reject(response.data)
                    }
                  }).catch(err => {
                    this.secondCodeErrorMsg = err.errMsg
                  })
              } else {
                callback(new Error('请先获取验证码'))
                this.secondStepCodeErrorChecked = false
                this.secondCodeChecked = false
              }
            }
          }
          callback()
        }
      }
      var validateSecondChangeReason = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请填写申诉说明'))
          this.changeReasonChecked = false
          this.descriptionTip = false
        } else {
          if (this.valid2.changeReason !== '') {
            if (value.length >= 100) {
              callback(new Error('输入长度过长，100个字符以内'))
              this.changeReasonChecked = false
              this.descriptionTip = false
            } else {
              this.changeReasonChecked = true
              this.descriptionTip = false
            }
          }
          callback()
        }
      }
      var validateSecondContactTel = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请填写正确的联系电话'))
          this.contactTelChecked = false
        } else {
          if (this.valid2.contactTel !== '') {
            var reg = /^1[0-9]{10}$/
            if (!reg.test(value)) {
              callback(new Error('请填写正确的联系电话'))
              this.contactTelChecked = false
            } else {
              this.contactTelChecked = true
            }
          }
          callback()
        }
      }
      return {
        isShowLoading: false,
        second_step_time: 0,
        codeErrorChecked: false,
        secondStepCodeErrorChecked: false,
        secondCodeChecked: false,
        changeReasonChecked: false,
        contactTelChecked: false,
        descriptionTip: true,
        secondCodeErrorMsg: '',
        newAdminNameChecked: false,
        mobileSecondChecked: false,
        getCodeBtnIsDisabled: true,
        showSecondStepCode: true,
        valid2: {
          newAdminName: '',
          mobile: '',
          code: '',
          changeReason: '',
          contactTel: ''
        },
        rules2: {
          newAdminName: [
            {validator: validateNewAdminName, trigger: 'blur'}
          ],
          mobile: [
            {validator: validateSecondMobile, trigger: 'blur'}
          ],
          code: [
            {validator: validateSecondCode, trigger: 'blur'}
          ],
          changeReason: [
            {validator: validateSecondChangeReason, trigger: 'blur'}
          ],
          contactTel: [
            {validator: validateSecondContactTel, trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
//      获取第二步手机验证码
      getSecondCheckCode () {
        this.isShowLoading = true
        this.$http.get(`/sso/change/admin/check/newMobile`, {params: {mobile: this.valid2.mobile, token: this.tokenId ? this.tokenId : this.$route.query.token}})
          .then(response => {
            this.isShowLoading = false
            if (response.data.success) {
              this.secondToken = response.data.content.token
              if (this.secondToken !== '') {
                this.$message({
                  message: '验证码已经发送到您的手机，请注意查收',
                  type: 'success'
                })
                this.showSecondStepCode = false
                this.second_step_time = 60
                var secondStepTime = setInterval(() => {
                  this.second_step_time--
                  if (this.second_step_time <= 0) {
                    this.showSecondStepCode = true
                    clearInterval(secondStepTime)
                  }
                }, 1000)
              }
            } else {
              return Promise.reject(response.data)
            }
          }).catch(err => {
            this.isShowLoading = false
            this.$message.error(err.errMsg)
          })
      },
//      更换管理员确认
      setNewMobile () {
        if (this.newAdminNameChecked && this.mobileSecondChecked && this.secondCodeChecked && this.changeReasonChecked && this.contactTelChecked) {
          this.isShowLoading = true
          let param = new FormData()
          param.append('newAdminName', this.valid2.newAdminName)
          param.append('mobile', this.valid2.mobile)
          param.append('code', this.valid2.code)
          param.append('token', this.tokenId ? this.tokenId : this.$route.query.token)
          param.append('codeToken', this.secondToken)
          param.append('changeReason', this.valid2.changeReason)
          param.append('contactTel', this.valid2.contactTel)
          let config = {
            headers: {'Content-Type': 'multipart/form-data'}
          }
          this.$http.post(`/sso/change/admin`, param, config)
            .then(response => {
              this.isShowLoading = false
              if (response.data.success) {
                this.$emit('stepEvent', 'last')
              } else {
                return Promise.reject(response.data)
              }
            }).catch(err => {
              this.$message.error(err.errMsg)
              this.isShowLoading = false
              this.second_step_time = 0
            })
        }
      }
    }
  }
</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: 57px;
                  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: 10px;
                right: -243px;
                width: 223px;
                line-height: 18px;
                text-align: left;
                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>
