<!--
 * @Author: 阿小凉
 * @Date: 2022-10-09 14:37:30
 * @LastEditTime: 2022-10-09 15:05:10
-->
<!--修改用户密码-->
<template>
  <el-dialog
    title="重置密码"
    :visible.sync="dialogVisible"
    width="550px"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :show-close="false"
  >
    <div>
      <el-form
        :model="password"
        ref="password"
        label-width="100px"
        class="demo-ruleForm"
        :rules="rule"
      >
        <el-form-item label="新密码" prop="newPwd">
          <el-input
            type="password"
            v-model="password.newPwd"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="oldPwd">
          <el-input
            type="password"
            v-model="password.oldPwd"
            show-password
          ></el-input>
        </el-form-item>
        <div style="text-align: center;margin-top:50px;">
          <el-button type="primary" @click="submitForm('password')"
            >确定修改</el-button
          >
          <el-button @click="logout()">退出</el-button>
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>
<script>
import { changePwd } from "@/api/users/index";
import store from "@/store";
export default {
  data() {
    return {
      dialogVisible: true,
      password: {
        newPwd: "",
        oldPwd: ""
      },
      rule: {
        newPwd: [{ required: true, message: "请输入新密码", trigger: "blur" }],
        oldPwd: [{ required: true, message: "请输入确认密码", trigger: "blur" }]
      }
    };
  },
  //   watch() {
  //     let is_reset_password = this.$store.state.user.is_reset_password;
  //   },
  created() {
    console.log(this.$store.state.user.is_reset_password, "stats");
    let is_reset_password = this.$store.state.user.is_reset_password;
    if (is_reset_password == 1) {
      this.dialogVisible = true;
    } else {
      this.dialogVisible = false;
    }
  },

  methods: {
    submitForm() {
      this.$refs["password"].validate(valid => {
        if (valid) {
          if (this.password.newPwd !== this.password.oldPwd) {
            this.$message.warning("您两次输入的密码不相同，请重新输入");
            return;
          }
          const data = {
            id: store.getters.id,
            password: this.password.newPwd
          };
          changePwd(data).then(res => {
            console.log(res, "xiuga9");
            this.$message.success("修改成功!");
            this.logout();
          });
        }
      });
    },
    async logout() {
      await this.$store.dispatch("user/logout").then(() => {
        this.$router.push("/login");
      });
    }
  }
};
</script>
<style lang="scss" scoped></style>
