<template>
  <div>
    <el-form ref="form" :model="form" label-width="100px" :rules="rules">
      <el-form-item label="用户名:" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="原密码:" prop="oldPass">
        <el-input v-model="form.oldPass" />
      </el-form-item>
      <el-form-item label="新密码:" prop="newPass">
        <el-input v-model="form.newPass" />
      </el-form-item>
      <el-form-item label="确认新密码:" prop="confirmPwd">
        <el-input v-model="form.confirmPwd" />
      </el-form-item>
    </el-form>
    <div class="btn">
      <el-button type="primary" @click="confirm">确认修改</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value !== this.form.newPass) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        name: 'admin',
        oldPass: '',
        newPass: '',
        confirmPwd: ''
      },
      rules: {
        name: { required: true, message: '请输入用户名', trigger: 'blur' },
        oldPass: { required: true, message: '请输入原密码', trigger: 'blur' },
        newPass: { required: true, message: '请输入新密码', trigger: 'blur' },
        confirmPwd: [{ required: true, validator: validatePass2, trigger: 'blur' }]

      }
    }
  },
  methods: {
    confirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // alert('submit!');
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.btn {
  margin-top: 50px;
  text-align: center;
}
</style>
