<template>
  <div class="page-form-container">
    <el-form ref="form" :model="formData" :rules="formRules" label-width="80px">
      <el-form-item label="原手机号" prop="oldMobile">
        <el-input v-model="formData.oldMobile" placeholder="请输入原手机号" disabled />
      </el-form-item>
      <el-form-item label="验证码" prop="verifyCode">
        <el-row>
          <el-col :span="18">
            <el-input v-model="formData.verifyCode" placeholder="请输入验证码" />
          </el-col>
          <el-col :span="6" class="send-code">
            <el-button type="primary" @click="handleSendVerifyCode()">{{ sendCodeBtnText }}</el-button>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="新手机号" prop="newMobile">
        <el-input v-model="formData.newMobile" placeholder="请输入新手机号" />
      </el-form-item>
    </el-form>
    <div class="control-box">
      <el-button type="primary" @click="submit()">确认</el-button>
      <el-button type="warning" @click="navigateBack()">返回</el-button>
    </div>
  </div>
</template>

<script>
import { resetMobile, sendVerifyCode } from '@/api/user'
import { isMobile } from '@/utils/validate'
export default {
  data() {
    const valideMobile = (rule, value, callback) => {
      if (isMobile(value)) {
        callback()
      } else {
        callback(new Error('请输入正确的手机号'))
      }
    }
    return {
      formData: {
        oldMobile: '',
        newMobile: '',
        verifyCode: ''
      },
      sendStatus: 0,
      formRules: {
        oldMobile: [
          { required: true, message: '请输入原来的手机号', trigger: 'blur' },
          { validator: valideMobile, trigger: 'blur' }
        ],
        verifyCode: [{ required: true, trigger: 'blur', message: '请输入旧手机号收到的验证码' }],
        newMobile: [
          { required: true, message: '请输入新的手机号', trigger: 'blur' },
          { validator: valideMobile, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    sendCodeBtnText() {
      return this.sendStatus === 0 ? '发送验证码' : `再次发送${this.sendStatus}s`
    }
  },
  created() {
    this.formData.oldMobile = this.$store.getters.mobile
  },
  methods: {
    submit() {
      this.$refs.form.validate((valide) => {
        if (!valide) return
        this.saveMobile()
      })
    },
    // 保存
    saveMobile() {
      this.formData.authUserId = this.$store.getters.authUserId
      resetMobile(this.formData).then((res) => {
        this.$message.success('修改手机号成功')
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.back()
      })
    },
    // 发送验证码
    handleSendVerifyCode() {
      if (!isMobile(this.formData.oldMobile)) {
        this.$message.warning('请输入正确的手机号')
        return
      }
      sendVerifyCode({ mobile: this.formData.oldMobile }).then((res) => {
        this.$message.success('验证码已发送')
        this.countdown()
      })
    },
    // 验证码倒计时
    countdown() {
      this.sendStatus = 30
      const timer = setInterval(() => {
        if (this.sendStatus === 0) {
          clearInterval(timer)
          return
        }
        this.sendStatus--
      }, 1000)
    }
  }
}
</script>

<style lang="scss" scoped>
.send-code {
  text-align: right;
}
</style>
