<template>
  <div>
    <el-form label-position="top" :model="ruleForm" :rules="rules" ref="form" @submit.prevent autocomplete="off">
      <el-form-item prop="username" :label="$t('register.nickname.title')">
        <el-input v-model="ruleForm.username" :placeholder="$t('register.nickname.placeholder')"></el-input>
      </el-form-item>
      <el-form-item prop="account" :label="$t('register.account.title')">
        <el-input v-model="ruleForm.account" :placeholder="$t('register.account.placeholder')"></el-input>
      </el-form-item>
      <el-form-item prop="code" :label="$t('register.code.title')">
        <el-input v-model="ruleForm.code" :placeholder="$t('register.code.placeholder')">
          <template #suffix>
            <countdown size="small" :disabled="disabledSend" @start="sendCodeRequest" ref="countdown"></countdown>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password" :label="$t('register.password.title')">
        <el-input type="password" v-model="ruleForm.password" :placeholder="$t('register.password.placeholder')" autocomplete="new-password"></el-input>
      </el-form-item>
      <el-button type="primary" class="submit-button" :loading="loading" @click="handleSubmit"> {{ $t('register.title') }}</el-button>
    </el-form>
  </div>
</template>

<script>
import * as api from '@/api/account'
import countdown from './countdown.vue'

export default {
  name: 'AppRegister',
  props: {
    sendcode: { type: Object, default: () => ({}) },
    headers: { type: Object, default: () => ({}) }
  },
  components: { countdown },
  data() {
    return {
      ruleForm: { username: '', account: '', code: '', password: '' },
      rules: {
        username: [{ required: true, message: this.$t('register.nickname.rule'), trigger: 'blur' }],
        account: [{ required: true, message: this.$t('register.account.rule'), trigger: 'blur' }],
        code: [{ required: true, message: this.$t('register.code.rule'), trigger: 'blur' }],
        password: [
          { required: true, message: this.$t('register.password.rule'), trigger: 'blur' },
          { min: 6, max: 20, message: this.$t('register.password.rule2'), trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  computed: {
    disabledSend() {
      const value = this.ruleForm.account
      return !(/^1[3-9]\d{9}$/.test(value) || /@/.test(value))
    }
  },
  methods: {
    // 提交
    handleSubmit() {
      this.$refs.form.validate().then(this.handleRequest)
    },
    // 接口请求
    handleRequest() {
      this.loading = true
      api.register(this.ruleForm, { headers: this.headers }).then(response => {
        response.code === 0 ? this.handleSuccess(response) : this.$message.error(response.msg)
        this.loading = false
      })
    },
    // 成功
    handleSuccess() {
      this.$alert('注册成功', '提示', {
        type: 'success',
        confirmButtonText: '去登录',
        callback: () => {
          this.$router.push('login')
        }
      })
    },
    // 验证码
    sendCodeRequest() {
      const params = Object.assign({}, this.sendcode, { account: this.ruleForm.account })
      api
        .sendCode(params, { headers: this.headers })
        .then(response => {
          console.log(response)
          if (response.code === 0) {
            this.$message({ type: 'success', message: '验证码已发送' })
          } else {
            // 停止计时
            this.$refs.countdown.stop()
            this.$message({ type: 'error', message: response.msg })
          }
        })
        .catch(this.$refs.countdown.stop)
    }
  }
}
</script>

<style lang="scss">
.el-form-item {
  margin-bottom: 20px;
}
.el-form-item__label {
  line-height: 24px;
}
</style>
