<template>
  <div class="page-form-container">
    <el-form ref="form" :model="formData" :rules="formRules" label-width="120px">
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="formData.mobile" placeholder="请输入手机号" />
      </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" :disabled="sendStatus > 0" @click="handleSendVerifyCode">{{ sendCodeBtnText }}</el-button>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="绑定登录账号" prop="loginAccount">
        <el-input v-model="formData.loginAccount" 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 { sendVerifyCode, bindLoginAccount } from '@/api/user'
import { isMobile, isAccount } from '@/utils/validate'
export default {
  data() {
    const valideMobile = (rule, value, callback) => {
      if (isMobile(value)) {
        callback()
      } else {
        callback(new Error('请输入正确的手机号'))
      }
    }
    const valideAccount = (rule, value, callback) => {
      if (isAccount(value)) {
        callback()
      } else {
        callback(new Error('仅支持英文字母和数字'))
      }
    }
    return {
      formData: {
        mobile: '',
        verifyCode: '',
        loginAccount: ''
      },
      sendStatus: 0,
      formRules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: valideMobile, trigger: 'blur' }
        ],
        verifyCode: [{ required: true, message: '请输入收到的验证码', trigger: 'blur' }],
        loginAccount: [
          { required: true, message: '请输入要绑定的登录号', trigger: 'blur' },
          { validator: valideAccount, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    sendCodeBtnText() {
      return this.sendStatus === 0 ? '发送验证码' : `再次发送${this.sendStatus}s`
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(valide => {
        if (!valide) return
        this.handleBindLoginAccount()
      })
    },
    // 绑定登录号
    handleBindLoginAccount() {
      this.formData.authUserId = this.$store.getters.authUserId
      bindLoginAccount(this.formData).then(res => {
        this.$message.success('绑定登录号成功')
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.back()
      })
    },
    // 发送验证码
    handleSendVerifyCode() {
      if (!isMobile(this.formData.mobile)) {
        this.$message.warning('请输入正确的手机号')
        return
      }
      sendVerifyCode({ mobile: this.formData.mobile }).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>
