<template>
  <div class="sys-container">
    <el-form ref="form" :rules="rules" :model="form" label-width="120px">
      <el-form-item label="输入原密码" prop="oldPwd">
        <el-input
          v-model="form.oldPwd"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="输入新密码" prop="newPwd">
        <el-input
          v-model="form.newPwd"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="confirmNewPwd">
        <el-input
          v-model="form.confirmNewPwd"
          size="small"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <el-button type="primary" size="small" @click="save">保存</el-button>
  </div>
</template>

<script>
import { userUpdatePwd } from '@/api/user';

export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      console.log(value);
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.form.newPwd) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      form: {
        oldPwd: '',
        newPwd: '',
        confirmNewPwd: '',
      },
      rules: {
        oldPwd: [
          { required: true, trigger: 'blur', message: '原密码不能为空' },
        ],
        newPwd: [
          { required: true, trigger: 'blur', message: '新密码不能为空' },
        ],
        confirmNewPwd: [{ validator: validatePass2, trigger: 'blur' }],
      },
    };
  },
  methods: {
    showData() {},
    save() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          const { msg } = await userUpdatePwd(this.form);
          this.$notify({
            title: msg,
            type: 'success',
          });
          this.$refs['form'].resetFields();
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style scoped></style>
