<template>
  <el-form>
    <el-form-item label="原始密码">
      <el-input v-model.trim="modifyForm.oldpwd" type="password" />
    </el-form-item>
    <el-form-item label="更新密码">
      <el-input v-model.trim="modifyForm.newpwd" type="password" />
    </el-form-item>
    <el-form-item label="确认密码">
      <el-input v-model.trim="modifyForm.confirmpwd" type="password" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">更新密码</el-button>
    </el-form-item>
  </el-form>
</template>

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

export default {
  data() {
    return {
      modifyForm: {
        oldpwd: '',
        newpwd: '',
        confirmpwd: ''
      }
    }
  },
  methods: {
    submit() {
      if (this.modifyForm.newpwd !== this.modifyForm.confirmpwd) {
        this.$message({
          message: '两次密码输入不一致！',
          type: 'error',
          duration: 5 * 1000
        })
        return
      }
      updatePassword(this.modifyForm).then(response => {
        this.$message({
          message: '修改密码成功！',
          type: 'success',
          duration: 5 * 1000
        })
         this.$store.dispatch('user/resetToken')//清除缓存
        this.$router.push(`/menu?redirect=/`)//重新登录
      })
    }
  }
}
</script>
