<template>
  <div class="register">
    <el-page-header @back="$router.push('login')" title="返回登录" content="注册"></el-page-header>
    <div class="register-form">
      <el-form :model="ruleForm" :rules="rules" ref="form" @submit.prevent>
        <el-form-item prop="name">
          <el-input v-model="ruleForm.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item prop="organization">
          <el-input v-model="ruleForm.organization" placeholder="机构"></el-input>
        </el-form-item>
        <el-form-item prop="job">
          <el-input v-model="ruleForm.job" placeholder="职务"></el-input>
        </el-form-item>
        <el-form-item prop="mobile">
          <el-input v-model="ruleForm.mobile" placeholder="手机"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="ruleForm.code" placeholder="验证码">
            <template #suffix>
              <countdown size="small" :disabled="disabledSend" @start="sendCodeRequest" ref="countdown"></countdown>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input v-model="ruleForm.email" placeholder="邮箱"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <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>
  </div>
</template>

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

export default {
  name: 'Register',
  components: { countdown },
  data() {
    return {
      ruleForm: { name: '', organization: '', job: '', mobile: '', code: '', email: '', password: '' },
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        organization: [{ required: true, message: '请输入机构名称', trigger: 'blur' }],
        job: [{ required: true, message: '请输入职务', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }],
        email: [{ 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.mobile
      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).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() {
      api
        .sendCode({ account: this.ruleForm.mobile, signName: 'wmp', template: 'SMS_200186414' })
        .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" scoped>
.register-form {
  margin-top: 20px;
}
</style>
