<template>
  <div class="app-container">
    <el-form ref="form" label-width="100px" :model="data">
      <el-form-item label="旧密码">
        <el-input
          type="password"
          v-model="data.oldPassword"
          placeholder="请输入旧密码"
        />
      </el-form-item>
      <el-form-item label="新密码">
        <el-input
          type="password"
          v-model="data.newPassword"
          placeholder="请输入新密码"
        />
      </el-form-item>
      <el-form-item label="确认新密码">
        <el-input
          type="password"
          v-model="data.newPassword2"
          placeholder="请确认新密码"
        />
      </el-form-item>
      <el-form-item>
        <el-button :disabled="disabled" type="primary" @click="submit"
          >确认</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { channgePass } from "@/api/auth/user";

export default {
  name: "changePassword",
  data: function() {
    return {
      disabled: false,
      data: {
        oldPassword: null,
        newPassword: null,
        newPassword2: null
      }
    };
  },
  created: function() {},
  methods: {
    submit: function() {
      this.disabled = true;
      if (!this.data.oldPassword) {
        this.$message.warning({
          message: "请输入旧密码"
        });
        this.disabled = false;
        return;
      }
      if (!this.data.newPassword) {
        this.$message.warning({
          message: "请输入新密码"
        });
        this.disabled = false;
        return;
      }
      if (!this.data.newPassword2) {
        this.$message.warning({
          message: "请确认新密码"
        });
        this.disabled = false;
        return;
      }
      channgePass(this.data).then(res => {
        if (res.success) {
          this.$alert("密码修改成功，请重新登录", {
            type: "info"
          }).then(
            () => {
              this.$store.dispatch("FedLogOut");
              location.reload();
            },
            () => {
              this.$store.dispatch("FedLogOut");
              location.reload();
            }
          );
        } else {
          this.$message.error({
            message: res.msg
          });
          this.disabled = false;
        }
      });
    }
  }
};
</script>

<style scoped></style>
