<template>
  <div class="club-register">
    <el-form :model="formData" :rules="rules" ref="form" label-width="0">
      <el-form-item prop="mobile">
        <el-input
          v-model="formData.mobile"
          placeholder="手机号"
          @blur="onMobileBlur"
          maxlength="11"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item prop="verifyCode">
        <div class="verifyCode flex justify-between">
          <el-input
            v-model="formData.verifyCode"
            placeholder="验证码"
            maxlength="6"
            show-word-limit
          ></el-input>
          <div class="send ml-8" @click="onSend">{{ sendCodeBtnText }}</div>
        </div>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="formData.password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item prop="confirmPwd">
        <el-input
          v-model="formData.confirmPwd"
          placeholder="再次输入密码"
        ></el-input>
      </el-form-item>
    </el-form>

    <SimpleDialog
      v-model="active"
      @confirm="onConfirm"
      @cancel="onCancel"
      description="抱歉，该手机号已注册，您可以登录后再来进行正品授权申请！"
      :center="true"
    />
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { isMobile } from '@/utils/validator'
export default {
  data() {
    const confirmPwdValide = (rule, value, callback) => {
      if (this.formData.password !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }

    return {
      formData: {
        mobile: '',
        verifyCode: '',
        password: '',
        confirmPwd: '',
      },
      sendStatus: 0,
      active: false,
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur'] },
        ],
        verifyCode: [
          { required: true, message: '验证码不能为空', trigger: ['blur'] },
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: ['blur'] },
        ],
        confirmPwd: [
          { required: true, message: '请再次输入密码', trigger: ['blur'] },
          { validator: confirmPwdValide, trigger: ['blur'] },
        ],
      },
    }
  },
  computed: {
    ...mapGetters(['authUserId', 'routePrefix', 'accessToken']),
    sendCodeBtnText() {
      return this.sendStatus === 0
        ? '获取验证码'
        : `再次发送${this.sendStatus}s`
    },
  },
  methods: {
    async onSend() {
      if (this.sendStatus > 0) return
      // 验证手机号是否合法
      if (!isMobile(this.formData.mobile)) {
        this.$toast('请输入正确的手机号')
        return
      }
      try {
        // 发送验证码
        await this.$http.api.sendVerifyCode({
          mobile: this.formData.mobile,
          authUserId: this.authUserId,
          type: 1,
        })
        this.$toast('验证码已发送')
        // 开启倒计时
        this.countdown()
      } catch (error) {
        console.log(error)
      }
    },

    countdown() {
      this.sendStatus = 30
      this.timer = setInterval(() => {
        if (this.sendStatus === 0) {
          clearInterval(this.timer)
          return
        }
        this.sendStatus--
      }, 1000)
    },

    onConfirm() {
      this.$router.push(`${this.routePrefix}`)
    },
    onCancel() {
      this.active = false
    },

    onMobileBlur() {
      this.checkouMobileBindClub()
    },
    // 判断用户手机号是否绑定机构
    async checkouMobileBindClub() {
      if (!isMobile(this.formData.mobile)) return
      try {
        const res = await this.$http.api.fetchClubAuthInfo({
          authUserId: this.authUserId,
          mobile: this.formData.mobile,
        })
        if (res.data.clubUser) {
          this.active = true
        }
      } catch (error) {
        console.log(error)
      }
    },

    genetageFormData() {
      return {
        mobile: this.formData.mobile,
        verifyCode: this.formData.verifyCode,
        password: this.formData.password,
      }
    },

    // 表单验证
    validate() {
      this.$emit('step', this.genetageFormData())
      return this.$refs.form.validate()
    },
  },
}
</script>

<style lang="scss" scoped>
.verifyCode {
  .send {
    cursor: pointer;
    white-space: nowrap;
    @include themify($themes) {
      color: themed('color');
    }
  }
}
</style>
