<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="form" label-width="100px" @submit.native.prevent>
      <el-form-item label="手机号" prop="account">
        <el-input v-model.trim="ruleForm.account"></el-input>
      </el-form-item>
      <el-form-item prop="code" label="验证码">
        <el-input v-model.trim="ruleForm.code" placeholder="请输入验证码">
          <countdown
            slot="suffix"
            size="mini"
            :disabled="disabledSend"
            @start="sendCodeRequest"
            ref="countdown"
          ></countdown>
        </el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model.trim="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="passwordR">
        <el-input type="password" v-model.trim="ruleForm.passwordR"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSubmit">确认修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

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

export default {
  name: 'MyPassword',
  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' }
        ]
      }
    }
  },
  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.updatePassword(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" scoped>
::v-deep .el-input {
  max-width: 270px;
}
</style>
