<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="form" @submit.native.prevent>
      <el-form-item prop="account" label="账号">
        <el-input v-model="ruleForm.account" placeholder="邮箱/手机号">
          <i slot="prefix" class="el-input__icon el-icon-user"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" label="验证码">
        <el-input v-model="ruleForm.code" placeholder="请输入验证码">
          <i slot="prefix" class="el-input__icon el-icon-unlock"></i>
          <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="请输入新登录密码">
          <i slot="prefix" class="el-input__icon el-icon-lock"></i>
        </el-input>
      </el-form-item>
      <el-form-item prop="passwordR" label="确认新密码">
        <el-input type="password" v-model="ruleForm.passwordR" placeholder="请输入新登录密码">
          <i slot="prefix" class="el-input__icon el-icon-lock"></i>
        </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: 'Password',
  components: { countdown },
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: { account: '', code: '', password: '', passwordR: '' },
      rules: {
        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' }
        ],
        passwordR: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { validator: validatePass, 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.resetPassword(this.ruleForm).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() {
      api
        .sendCode({ account: this.ruleForm.account })
        .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>
