<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="form" @submit.native.prevent>
      <el-form-item prop="username" label="昵称">
        <el-input v-model="ruleForm.username" placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item prop="account" label="账号">
        <el-input v-model="ruleForm.account" placeholder="邮箱/手机号"></el-input>
      </el-form-item>
      <el-form-item prop="code" label="验证码">
        <el-input v-model="ruleForm.code" placeholder="请输入验证码">
          <countdown
            slot="suffix"
            size="mini"
            :disabled="disabledSend"
            @start="sendCodeRequest"
            ref="countdown"
          ></countdown>
        </el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input type="password" v-model="ruleForm.password" placeholder="请输入登录密码"></el-input>
      </el-form-item>
      <el-button type="primary" class="submit-button" :loading="loading" @click="handleSubmit">注册</el-button>
    </el-form>
  </div>
</template>

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

export default {
  name: 'Register',
  props: {
    sendcode: { type: Object, default: () => ({}) },
    headers: { type: Object, default: () => ({}) }
  },
  components: { countdown },
  data() {
    return {
      ruleForm: { username: '', account: '', code: '', password: '' },
      rules: {
        username: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
        account: [{ required: true, message: '请输入邮箱/手机号', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度为6-20个字符', 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(response) {
      this.$alert('注册成功', '提示', {
        type: 'success',
        confirmButtonText: '去登录',
        callback: action => {
          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>
