<template>
  <div class="f-main">
    <div class="content-top">
      <p>更换管理员</p>
      <!--<a href="javascript:void(0)" class="back" @click="jump('select')"><i class="el-icon-back"></i></a>-->
    </div>
    <div class="f-form">
      <div class="page-part">
        <mt-field placeholder="新管理员姓名"
                  v-model="valid.contactName"
                  :state="state.contactName"
                  @blur.native.capture="validateContactName"
        ></mt-field>
      </div>
      <div class="page-part">
        <mt-field placeholder="新管理员手机号"
                  v-model="valid.mobile"
                  :state="state.mobile"
                  type="tel"
                  @blur.native.capture="validateMobile"
        ></mt-field>
      </div>
      <div class="page-part">
        <mt-field auto-complete="off"
                  placeholder="短信验证码"
                  v-model="valid.code"
                  :state="state.code"
                  @blur.native.capture="validateCode">
          <span class="token" @click="getCheckCode" v-text="tokenText" v-if="state.mobile === 'success'">获取验证码</span>
          <span class="token-no" v-text="tokenText" v-if="state.mobile !== 'success'">获取验证码</span>
        </mt-field>
      </div>
      <div class="page-part">
        <mt-field placeholder="更换原因"
                  type="textarea"
                  rows="4"
                  v-model="valid.description"
                  @blur.native.capture="validateDescription"></mt-field>
        <p class="pwd">请描述您申诉的原因，并尽可能多地列举出证明此企业账号为您所有的证据</p>
      </div>
      <div class="page-part">
        <mt-field placeholder="联系电话"
                  v-model="valid.contactTel"
                  :state="state.contactTel"
                  type="tel"
                  @blur.native.capture="validateContactTel"
        ></mt-field>
      </div>
      <div class="page-part">
        <mt-button size="large" type="primary" @click="sureAccount('last')">提 交</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'step-one',
    props: ['tokenId'],
    data () {
      return {
        state: {
          contactName: 'error',
          mobile: 'error',
          code: 'error',
          description: 'error',
          contactTel: 'error'
        },
        valid: {
          contactName: '',
          mobile: '',
          code: '',
          description: '',
          contactTel: ''
        },
        tokenCode: '',
        tokenTime: 60,
        tokenText: '获取验证码'
      }
    },
    methods: {
      jump (type) {
        this.$emit('stepEvent', type)
      },
      // 弹窗处理
      downToast (type) {
        this.$toast({
          message: type,
          iconClass: 'el-icon-warning'
        })
      },
      // 验证姓名
      validateContactName () {
        if (!this.valid.contactName) {
          this.downToast('请填写您的姓名')
          this.state.contactName = 'error'
        } else {
          if (this.valid.contactName.length > 20) {
            this.downToast('输入长度过长，限定20个字符以内')
            this.state.contactName = 'warning'
          } else {
            this.state.contactName = 'success'
          }
        }
      },
      // 验证手机号
      validateMobile () {
        if (!this.valid.mobile) {
          this.downToast('请先填写手机号')
          this.state.mobile = 'error'
        } else {
          let reg = /^1[0-9]{10}$/
          if (!reg.test(this.valid.mobile)) {
            this.downToast('请填写正确的手机号')
            this.state.mobile = 'warning'
          } else {
            this.state.mobile = 'success'
          }
        }
      },
      // 验证正确的验证码
      validateCode () {
        if (!this.valid.code) {
          this.downToast('请先填写验证码')
          this.state.code = 'error'
        } else {
          if (!this.valid.mobile) {
            this.downToast('请先填写正确的手机号码')
            this.state.code = 'warning'
          } else {
            if (this.tokenCode) {
              let param = new FormData()
              param.append('mobile', this.valid.mobile)
              param.append('token', this.tokenCode)
              param.append('code', this.valid.code)
              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.state.code = 'success'
                  } else {
                    this.$toast({
                      message: response.data.errMsg,
                      iconClass: 'el-icon-error'
                    })
                  }
                }).catch((err) => {
                  this.downToast(err.errMsg)
                })
            } else {
              this.downToast('请点击先获取验证码信息')
              this.state.code = 'warning'
            }
          }
        }
      },
      // 获取验证码
      getCheckCode () {
        if (this.tokenTime > 0 && this.tokenTime < 60) {
          this.downToast('请稍后再点击，我在倒计时')
        } else {
          if (this.state.mobile === 'success') {
            this.$indicator.open('获取中...')
            let _this = this
            this.$http.get('/sso/change/admin/check/newMobile', {params: {mobile: this.valid.mobile, token: this.tokenId ? this.tokenId : this.$route.query.token}})
              .then(response => {
                this.$indicator.close()
                if (response.data) {
                  this.tokenCode = response.data.content.token
                  this.$toast({
                    message: '验证码已经发送到您的手机，请注意查收',
                    iconClass: 'el-icon-success'
                  })
                  this.tokenText = '已发送(' + this.tokenTime + 'S)'
                  let setTime = setInterval(() => {
                    _this.tokenTime--
                    this.tokenText = '已发送(' + this.tokenTime + 'S)'
                    if (this.tokenTime <= 0) {
                      clearInterval(setTime)
                      _this.tokenText = '获取验证码'
                      _this.tokenTime = 60
                    }
                  }, 1000)
                }
              }).catch((err) => {
                this.$indicator.close()
                this.downToast(err.errMsg)
              })
          }
        }
      },
      // 申诉
      validateDescription () {
        if (!this.valid.description) {
          this.downToast('请填写申诉说明')
          this.state.description = 'error'
        } else {
          if (this.valid.description.length >= 100) {
            this.downToast('输入长度过长，100个字符以内')
            this.state.description = 'warning'
          } else {
            this.state.description = 'success'
          }
        }
      },
      // 验证电话
      validateContactTel () {
        let reg = /^1[0-9]{10}$/
        if (!this.valid.contactTel) {
          this.downToast('请填写联系电话')
          this.state.contactTel = 'error'
        } else {
          if (!reg.test(this.valid.contactTel)) {
            this.downToast('请填写正确的联系电话')
            this.state.contactTel = 'warning'
          } else {
            this.state.contactTel = 'success'
          }
        }
      },
      sureAccount (type) {
        console.log('type', this.tokenId)
        if (this.state.mobile !== 'success' ||
          this.state.contactName !== 'success' ||
          this.state.code !== 'success' ||
          this.state.description !== 'success' ||
          this.state.contactTel !== 'success') {
          this.downToast('请确认填写部分是否有误')
        } else {
          this.$indicator.open('验证过程中...')
          let param = new FormData()
          param.append('newAdminName', this.valid.contactName)
          param.append('mobile', this.valid.mobile)
          param.append('code', this.valid.code)
          param.append('contactTel', this.valid.contactTel)
          param.append('changeReason', this.valid.description)
          param.append('token', this.tokenId ? this.tokenId : this.$route.query.token)
          param.append('codeToken', this.tokenCode)
          let config = {
            headers: {'Content-Type': 'multipart/form-data'}
          }
          this.$http.post('/sso/change/admin', param, config)
            .then(response => {
              this.$indicator.close()
              if (response.data.success) {
                this.$emit('stepEvent', type)
                this.$emit('lastEvent', 'success')
              } else {
                this.downToast(response.data.errMsg)
              }
            }).catch((err) => {
              this.$indicator.close()
              this.downToast(err.errMsg)
            })
        }
      }
    }
  }
</script>
