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

<script>
import { updatePwd } from '../../api'
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value !== this.form.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    let { userId, userName } = this.$store.state.user.userBaseInfo
    return {
      form: {
        userName: userName,
        originPwd: '',
        password: '',
        sureNewPwd: '',
        userId: userId
      },
      rules: {
        userName: { required: true, message: '请输入用户名', trigger: 'blur' },
        originPwd: { required: true, message: '请输入原密码', trigger: 'blur' },
        password: { required: true, message: '请输入新密码', trigger: 'blur' },
        sureNewPwd: [{ required: true, validator: validatePass2, trigger: 'blur' }]
      }
    }
  },
  methods: {
    confirm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          updatePwd(this.form).then(res => {
            this.$message.success('操作成功')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.btn {
  margin-top: 50px;
  text-align: center;
}
.el-form {
  width: 500px;
  margin: 0 auto;
}
.title {
  width: 100%;
  height: 60px;
  background: rgba(226, 235, 255, 0.39);
  line-height: 60px;
  font-size: 18px;
  color: #333;
  font-weight: 500;
  text-align: center;
  margin-bottom: 30px;
}
</style>
