<!--  -->
<template>
  <div class="Modify Modifypassword">
    <el-dialog
      :title="$t('Key88')"
      :visible.sync="dialogVisible"
      top="80px"
      width="570px"
      :show-close="false"
    >
      <div class="registration2">
        <div class="form">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
            <el-form-item prop="oldpassword">
              <span slot="label" class="lable">
                <img src="../../assets/login/singin9.png" alt="" />
                <span>旧密码</span>
              </span>
              <el-input
                v-model.trim="ruleForm.oldpassword"
                placeholder="请输入旧密码"
                :type="oldpasswordType"
              />
              <img
                v-show="oldpasswordType == 'password'"
                @click="lookParssowrd(0)"
                class="rightimg"
                src="../../assets/login/password1.png"
                alt=""
              />
              <img
                v-show="oldpasswordType == 'text'"
                @click="lookParssowrd(0)"
                class="rightimg"
                src="../../assets/login/password2.png"
                alt=""
              />
            </el-form-item>
            <el-form-item prop="password">
              <span slot="label" class="lable">
                <img src="../../assets/login/singin9.png" alt="" />
                <span> 新密码 </span>
              </span>
              <el-input
                v-model.trim="ruleForm.password"
                placeholder="请输入新密码"
                :type="passwordType"
              />
              <img
                v-show="passwordType == 'password'"
                @click="lookParssowrd(1)"
                class="rightimg"
                src="../../assets/login/password1.png"
                alt=""
              />
              <img
                v-show="passwordType == 'text'"
                @click="lookParssowrd(1)"
                class="rightimg"
                src="../../assets/login/password2.png"
                alt=""
              />
              <p :class="passwordError ? 'textRed' : 'psswordHint'">
                <!-- 请输入8-12位大写字母、小写字母和数字组合。 -->
                {{ $t("Key121") }}
              </p>
            </el-form-item>
            <el-form-item prop="Twopassword">
              <span slot="label" class="lable" style="width: 200px">
                <img src="../../assets/login/singin9.png" alt="" />
                <span>重复密码</span>
              </span>
              <el-input
                v-model.trim="ruleForm.Twopassword"
                placeholder="请重复输入新密码"
                :type="twoPasswordType"
              />
              <img
                v-show="twoPasswordType == 'password'"
                @click="lookParssowrd(2)"
                class="rightimg"
                src="../../assets/login/password1.png"
                alt=""
              />
              <img
                v-show="twoPasswordType == 'text'"
                @click="lookParssowrd(2)"
                class="rightimg"
                src="../../assets/login/password2.png"
                alt=""
              />
              <p :class="passwordErrorTwo ? 'textRed' : 'psswordHint'">
                <!-- 请再次输入密码，必须和新密码一致。 -->
                {{ $t("Key122") }}
              </p>
            </el-form-item>
            <el-divider></el-divider>
            <el-form-item style="margin-bottom: 0">
              <button
                type="button"
                @click.prevent="submitForm()"
                class="footer-btn-sure"
              >
                <!-- 确定 -->{{ $t("Key94") }}
              </button>
              <button
                type="button"
                @click.prevent="dialogVisible = false"
                class="footer-btn-cancle"
              >
                <!-- 取消 -->{{ $t("Key83") }}
              </button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getContent } from "@/api/ajax";
import Cookies from "js-cookie";
import { removeToken } from "@/utils/auth";

export default {
  name: "Modifymailbox",
  components: {},
  data() {
    var basePassword = (rule, value, callback) => {
      let _this = this;
      if (value === "") {
        callback(new Error("请输入旧密码")); //
      } else if (value == _this.ruleForm.password) {
        callback(new Error(_this.$t("Key210"))); //"新旧密码一致!"
      } else {
        callback();
      }
    };
    var validatePassword = (rule, value, callback) => {
      let _this = this;
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else {
        if (value.length < 8) {
          callback(new Error("密码至少8位"));
        } else if (value.length > 12) {
          callback(new Error("密码最多12位"));
        } else {
          if (this.ruleForm.oldpassword !== "") {
            this.$refs.ruleForm.validateField("oldpassword");
            if (value == _this.ruleForm.oldpassword) {
              callback(new Error(_this.$t("Key210")));
            }
          }
          if (this.ruleForm.Twopassword !== "") {
            this.$refs.ruleForm.validateField("Twopassword");
          }

          if (
            !/[A-Z]/.test(value) ||
            !/[a-z]/.test(value) ||
            !/[0-9]/.test(value)
          ) {
            callback(new Error("密码必须是大写字母、小写字母和数字的组合。"));
          }
          callback();
        }
      }
    };
    var validateTwoPassword = (rule, value, callback) => {
      let _this = this;
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== _this.ruleForm.password) {
        callback(new Error(_this.$t("Key211"))); //两次输入密码不一致!
      } else {
        callback();
      }
    };
    return {
      dialogVisible: false,
      oldpasswordType: "password",
      passwordType: "password",
      twoPasswordType: "password",
      passwordError: false,
      passwordErrorTwo: false,
      ruleForm: {
        oldpassword: "",
        password: "",
        Twopassword: "",
      },
      rules: {
        oldpassword: [
          {
            validator: basePassword,
            trigger: "blur",
          },
        ],
        password: [
          {
            validator: validatePassword,
            trigger: "blur",
          },
        ],
        Twopassword: [
          {
            validator: validateTwoPassword,
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {},
  watch: {},
  //方法集合
  methods: {
    changeShow() {
      this.dialogVisible = true;
    },
    // 去除空格
    trimInput(key) {
      this.ruleForm[key] = this.ruleForm[key].trim();
    },

    // 查看密码
    lookParssowrd(number) {
      if (number == 0) {
        if (this.oldpasswordType == "text") {
          this.oldpasswordType = "password";
        } else {
          this.oldpasswordType = "text";
        }
      } else if (number == 1) {
        if (this.passwordType == "text") {
          this.passwordType = "password";
        } else {
          this.passwordType = "text";
        }
      } else {
        if (this.twoPasswordType == "text") {
          this.twoPasswordType = "password";
        } else {
          this.twoPasswordType = "text";
        }
      }
    },
    // 验证密码
    changePassword() {
      if (this.ruleForm.password) {
        let reg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,12}$/;
        let result = reg.test(this.ruleForm.password);
        if (result) {
          this.passwordError = false;
        } else {
          this.passwordError = true;
        }
      }
    },
    // 验证第二次密码是否一样
    Changetowpassword() {
      if (this.ruleForm.Twopassword) {
        if (this.ruleForm.Twopassword != this.ruleForm.password) {
          this.passwordErrorTwo = true;
          return;
        } else {
          this.passwordErrorTwo = false;
        }
      }
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.onSubmit();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    onSubmit() {
      let MethodName = "user_manager-UpdateMyPassword";
      let data = {
        password_old: this.ruleForm.oldpassword,
        password_new: this.ruleForm.password,
      };
      getContent(MethodName, data).then((res) => {
        this.dialogVisible = false;
        this.$message.success("密码更改成功，请重新登录");
        setTimeout(() => {
          removeToken();
          Cookies.remove("userMessage");
          Cookies.remove("session_id");
          Cookies.remove("user_code");
          Cookies.remove("user_type");
          this.$router.replace({ path: "/login" });
        }, 1000);
      });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="scss" scoped>
/* @import url(); 引入css类 */
.registration2 {
  width: 100%;
  .title {
    // height: 60px;
    // line-height: 60px;
    p {
      margin: 0;
      padding: 0;
    }
    .p1 {
      font-size: 24px;
      color: #ff9900;
    }
    .p2 {
      margin-top: 40px;
      font-size: 20px;
      color: #000000;
    }
  }
  .form {
    .title2 {
      font-weight: 600;
      font-size: 20px;
      color: #000000;
      margin: 20px 0;
    }
    hr {
      margin: 0;
      width: 474px;
      height: 1px;
      background: #d9d9d9;
    }
    .el-input,
    .el-select {
      width: 334px;
    }

    .el-form-item {
      width: 100%;
      display: flex;
      .lable {
        display: inline-block;
        width: 120px !important;
        text-align: left;
        img {
          width: 24px;
          vertical-align: middle;
        }
        span {
          margin-left: 10px;
        }
      }
      &.el-form-item__content {
        width: 100% !important;
      }
    }
    // .el-form-item::after {
    //   content: "*";
    //   color: red;
    //   position: relative;
    //   left: 5px;
    //   top: 10px;
    // }
    .getVerification {
      width: 116px;
      height: 40px;
      margin-left: 16px;
      border-radius: 4px;
      cursor: pointer;
      box-sizing: border-box;
      background: #ff9900;
      border: 1px solid #ff9900;
      color: white;
    }
    .footer-btn {
      &-sure {
        width: 100%;
        height: 48px;
        border-radius: 4px;
        cursor: pointer;
        box-sizing: border-box;
        background: #ff9900;
        border: 1px solid #ff9900;
        font-weight: bold;
        font-size: 18px;
        text-align: center;
        color: #ffffff;
        margin-bottom: 16px;
      }
      &-cancle {
        width: 100%;
        height: 48px;
        border-radius: 4px;
        cursor: pointer;
        box-sizing: border-box;
        background: #fff;
        border: 1px solid rgba(0, 0, 0, 0.15);
        font-size: 18px;
        text-align: center;
        color: #2c2c2c;
      }
    }
    .waitTime {
      width: 116px;
      height: 40px;
      margin-left: 16px;
      border-radius: 4px;
      cursor: pointer;
      box-sizing: border-box;
      background: #f0f0f0;
      color: black;
      border: none;
    }
    .rightimg {
      width: 24px;
      position: absolute;
      right: 16px;
      top: 10px;
      cursor: pointer;
    }
    .psswordHint {
      width: 334px;
      margin: 0;
      padding: 0;
      font-size: 14px;
      color: #000000;
      opacity: 0.3;
      line-height: 20px;
      margin-top: 5px;
    }
    .textRed {
      width: 334px;
      margin: 0;
      padding: 0;
      font-size: 14px;
      color: red;
      opacity: 1;
      line-height: 20px;
      margin-top: 5px;
    }
    .sumit {
      width: 474px;
      text-align: right;
      button {
        width: 337px;
        height: 48px;
        background: #ff9900;
        border-radius: 4px;
        cursor: pointer;
        border: none;
        outline: none;
        color: white;
        img {
          vertical-align: middle;
          animation: rotated 1s linear infinite;
        }
      }
    }
    .agreementAndRemeberMeAndForgetparssword {
      width: 474px;
      display: flex;
      justify-content: flex-end;
      > div {
        margin: 20px 0;
        width: 334px;
        position: relative;
      }
      .agreement {
        span {
          color: #ff9900;
        }
      }
    }
    .remeberMeAndForgetparssword {
      p {
        margin: 0;
        color: #ff9900;
        cursor: pointer;
        text-align: right;
      }
    }
    .Noafter::after {
      content: "";
    }
  }
  @keyframes rotated {
    0% {
      transform: rotateZ(0deg);
    }
    100% {
      transform: rotateZ(360deg);
    }
  }
}
</style>
<style lang="scss">
.form {
  .el-form-item__content {
    width: 100%;
  }
}
.Modify {
  .el-dialog {
    box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.15);
    border-radius: 8px;
  }
  .el-dialog__header {
    padding: 48px 48px 32px 48px;
    .el-dialog__title {
      font-weight: bold;
      font-size: 20px;
      line-height: 150%;
      color: #000000;
    }
  }
  .el-dialog__body {
    padding: 0px 48px 30px;
  }
}
.Modifypassword {
  .el-input__inner {
    padding-right: 40px;
  }
}
</style>