<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <b>修改密码</b>
      </div>
      <el-form
        :model="addForm"
        :rules="rules"
        label-width="120px"
        ref="addForm"
        style="width: 500px"
      >
        <el-form-item label="原密码" prop="OldPwd">
          <el-input
            type="text"
            v-model="addForm.OldPwd"
            placeholder="请输入不超过20个字符"
            clearable
            autocomplete="new-password"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="NewPwd" v-if="visible">
          <el-input
            type="password"
            v-model="addForm.NewPwd"
            placeholder="请输入长度不超过20个字符"
            maxlength="20"
            autocomplete="new-password"
          >
            <i
              slot="suffix"
              class="el-icon-view el-input__icon"
              title="显示密码"
              @click="changePass('show')"
              style="cursor: pointer"
            ></i>
          </el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="NewPwd" v-else>
          <el-input
            type="text"
            v-model="addForm.NewPwd"
            placeholder="请输入长度不超过20个字符"
            maxlength="20"
            autocomplete="new-password"
          >
            <i
              slot="suffix"
              class="el-icon-more el-input__icon"
              title="隐藏密码"
              @click="changePass('hide')"
              style="cursor: pointer"
            ></i>
          </el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="twoNewPwd" v-if="twovisible">
          <el-input
            type="password"
            v-model="addForm.twoNewPwd"
            placeholder="请输入长度不超过20个字符"
            maxlength="20"
          >
            <i
              slot="suffix"
              class="el-icon-view el-input__icon"
              title="显示密码"
              @click="changetwoPass('show')"
              style="cursor: pointer"
            ></i>
          </el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="twoNewPwd" v-else>
          <el-input
            type="text"
            v-model="addForm.twoNewPwd"
            placeholder="请输入长度不超过20个字符"
            maxlength="20"
          >
            <i
              slot="suffix"
              class="el-icon-more el-input__icon"
              title="隐藏密码"
              @click="changetwoPass('hide')"
              style="cursor: pointer"
            ></i>
          </el-input>
        </el-form-item>
        <el-form-item style="text-align: center; padding-top: 20px">
          <el-button type="primary" size="medium" @click.native="addSubmit"
            >确定</el-button
          >
          <el-button
            type="primary"
            size="medium"
            plain
            @click.native="historyBack"
            >返回</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { modifyPwd } from "@/api/api.js";
import http from "@/api/http.js";
import md5 from "@/utils/md5.js";
export default {
  data() {
    return {
      visible: true,
      twovisible: true,
      addForm: {
        OldPwd: "",
        NewPwd: "",
        twoNewPwd: "",
      },
      rules: {
        OldPwd: [
          {
            required: true,
            message: "请输入原密码",
            trigger: "blur",
          },
        ],
        NewPwd: [
          {
            required: true,
            message: "请输入确认密码",
            trigger: "blur",
          },
        ],
        twoNewPwd: [
          {
            required: true,
            message: "请输入确认密码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    changePass(value) {
      this.visible = !(value === "show");
    },
    changetwoPass(value) {
      this.twovisible = !(value === "show");
    },
    addSubmit() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          let that = this;
          if (that.addForm.NewPwd != that.addForm.twoNewPwd) {
            that.$message.warning("新密码两次输入不一致，请核对！");
            return false;
          }
          modifyPwd({
            OldPwd: md5.hex_md5(that.addForm.OldPwd),
            NewPwd: md5.hex_md5(that.addForm.NewPwd),
          }).then((res) => {
            if (res.Ret == 200) {
              that.$message.success("修改密码成功,请重新登录！");
              setTimeout(function () {
                localStorage.setItem("auth", "");
                localStorage.setItem("userName", "");
                localStorage.setItem("Role", "");
                localStorage.setItem("RoleType", "");
                localStorage.setItem("AdminId", "");
                localStorage.setItem("AdminName", "");
                localStorage.setItem("RoleIdentity", "");
                localStorage.setItem("ManageType", "");
                that.$router.push({ path: "/login" });
              }, 1000);
            }
          });
        }
      });
    },
    historyBack() {
      //返回上一页
      history.back();
    },
  },
};
</script>

<style scoped lang="scss"></style>
