<template>
  <div class="right">
    <div class="showTitle">
      <el-icon><Tickets /></el-icon>
      <h2>修改密码</h2>
    </div>
    <div class="buttom">
      <div>
        <h5>旧密码:</h5>
        <input type="password" />
      </div>
      <div>
        <h5>新密码:</h5>
        <input type="password" />
      </div>
      <div>
        <h5>确认密码:</h5>
        <input class="last" type="password" />
      </div>
      <input type="button" @click="convertMsg()" value="确认修改" />
    </div>
  </div>
</template>
<script>
import { changePwd } from "@/api";

import { mapState } from "vuex";
export default {
  name: "ConvertMsg",
  computed: {
    ...mapState("LoginStore", ["tokenStore"]),
  },
  created() {
    //页面加载时就从本地通过localstorage获取存储的token值
    if (localStorage.getItem("token")) {
      this.token = localStorage.getItem("token");
    }
  },
  methods: {
    convertMsg() {
      //取出密码
      const prePwd = document.documentElement
        .getElementsByClassName("buttom")[0]
        .getElementsByTagName("input")[0].value;
      const pwd1 = document.documentElement
        .getElementsByClassName("buttom")[0]
        .getElementsByTagName("input")[1].value;
      const pwd2 = document.documentElement
        .getElementsByClassName("buttom")[0]
        .getElementsByTagName("input")[2].value;
      console.log("密码", prePwd, pwd1, pwd2);

      var pPattern =
        /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/;
      //判断是否为空
      if (prePwd.trim() == "" || pwd1.trim() == "" || pwd2.trim() == "") {
        alert("请填写完整信息!");
      }
      //判断密码是否一致
      else if (pwd1 != pwd2) {
        alert("前后密码不一致");
      } else if (!pPattern.test(pwd1)) {
        alert(
          "密码最少6位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符"
        );
      } else {
        const params = {
          oldPW: prePwd,
          newPW: pwd1,
        };

        changePwd(params).then((response) => {
          if (response.data.code == 200) {
            alert("修改成功!");
          } else {
            alert("修改失败，请重试");
          }
        });
      }
    },
  },
};
</script>

<style scoped>
.right {
  width: 59rem;
  height: 35rem;
  background-color: rgb(255, 255, 255);
  top: 1rem;
  left: 24rem;
  border: 1px solid gray;
  border-radius: 10px;
  margin-left: 25rem;
}
.showTitle {
  position: relative;
  width: 100%;
  height: 3rem;
  border-bottom: 1px solid rgb(165, 162, 162);
  box-sizing: border-box;
  text-align: start;
  padding-left: 2rem;
}
.showTitle h2 {
  display: inline-block;
  font-size: 1.3rem;
  text-align: left;
}
.buttom input[type="button"] {
  background-color: #409eff;
  width: 6rem;
  height: 2rem;
  border-radius: 1rem;
  font-size: 1rem;
  border: none;

  color: white;
  text-align: center;
  letter-spacing: 0.3rem;
  cursor: pointer;
}
.buttom {
  text-align: left;
}
.buttom h5 {
  font-size: 1.1rem;
  padding-left: 4rem;
  padding-right: 2rem;
  display: inline-block;
}
.buttom input {
  font-size: 1.1rem;
}

.buttom input[type="button"][data-v-3e4046af][data-v-3e4046af] {
  background-color: #409eff;
  width: 7rem;
  height: 3rem;
  border-radius: 2rem;
  font-size: 0.8rem;
  border: none;
  color: white;
  text-align: center;
  letter-spacing: 0.3rem;
  cursor: pointer;
  margin-left: 12rem;
  margin-top: 1rem;
}
.last {
  margin-left: -1rem;
}
</style>
