<template>
  <div v-loading="view.loading" element-loading-text="服务器受理中...">
    <el-form ref="pwForm" :model="form" :rules="formRules" label-position="top" :show-message="false" status-icon>
      <el-form-item label="原密码" prop="oldpasswd">
        <el-input type="password" v-model="form.oldpasswd"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="passwd">
        <el-input type="password" v-model="form.passwd"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="repasswd">
        <el-input type="password" v-model="form.repasswd"></el-input>
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span='24' style='text-align: center;margin-bottom: 10px;'>
        <el-button @click="submit" class="blue" size='small'>确认修改</el-button>
        <el-button @click="cancel" size='small'>取消修改</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>
  import {mapGetters} from "vuex";
  import AcesAPI from "@/api/base/sysm/AcesAPI";

  export default {
    data() {
      let validateNewpass = (rule, value, callback) => {
        if (!value) {
          callback(new Error("请输入新密码"));
          return;
        } else {
          if (value.length < 6) {
            callback(new Error("密码长度至少6位"));
          }
          if (this.form.repasswd !== "") {
            this.$refs.pwForm.validateField("repasswd");
          }
          if(!this.isPswComplex(this.form.repasswd)){
            callback(new Error("密码长度8位以上且需由数大小写字母、数字及特殊字符两种或以上混合组成！"));
          }
          callback();
        }
      };
      let validateReNewpass = (rule, value, callback) => {
        if (!value) {
          callback(new Error("请再次输入密码"));
        } else if (value !== this.form.passwd) {
          callback(new Error("两次输入密码不一致!"));
        } else {
          callback();
        }
      };
      return {
        form: {
          oldpasswd: "",
          passwd: "",
          repasswd: ""
        },
        view: {
          loading: false
        },
        formRules: {
          oldpasswd: [{required: true, min: 6}],
          passwd: [{required: true, validator: validateNewpass}],
          repasswd: [{required: true, validator: validateReNewpass}]
        }
      };
    },
    computed: {
      ...mapGetters(["sysLogin"])
    },
    methods: {
      submit() {

        this.$refs.pwForm.validate(validate => {

          if (validate) {

            this.view.loading = true;
            AcesAPI.changePwd(this.form.oldpasswd, this.form.passwd)
              .then(data => {
                this.view.loading = false;
                AcesAPI.logout().then(() => {
                  this.$router.replace("/login");
                });
              })
              .catch(() => {
                this.view.loading = false;
              });
          }else {
            if (!this.form.oldpasswd) {
              this.$message({
                message: "请输入原始密码！",
                type: "warning"
              });
              return;
            }
            if (!this.form.passwd) {
              this.$message({
                message: "请输入新密码！",
                type: "warning"
              });
              return;
            }
            if (!this.form.repasswd) {
              this.$message({
                message: "请再次输入密码！",
                type: "warning"
              });
              return;
            }
            if (this.form.passwd.length < 6) {
              this.$message({
                message: "密码长度至少6位！",
                type: "warning"
              });
              return;
            }
            if(!this.isPswComplex(this.form.passwd)){
              this.$message({
                message: "密码长度8位以上且需由数大小写字母、数字及特殊字符两种或以上混合组成.！",
                type: "warning"
              });
              return;
            }
            if(this.form.repasswd !== this.form.passwd){
              this.$message({
                message: "两次输入密码不一致！",
                type: "warning"
              });
              return;
            }
          }
        });
      },
      cancel() {
        this.$emit("cancel");
      },
      isPswComplex(wd) {
        console.log(wd);
        //数字
        let REG_NUMBER = /.*\\d+.*/;
        //小写字母
        let REG_UPPERCASE = /.*[A-Z]+.*/;
        //大写字母
        let REG_LOWERCASE = /.*[a-z]+.*/;
        //特殊符号
        let REG_SYMBOL = /.*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\\"]+.*/;

        //密码为空或者长度小于8位则返回false
        if ( wd == undefined || wd == null || wd.length < 8) return false;
        let i = 0;
        if (REG_NUMBER.test(wd)) i++;
        if (REG_LOWERCASE.test(wd)) i++;
        if (REG_UPPERCASE.test(wd)) i++;
        if (REG_SYMBOL.test(wd)) i++;

        if (i < 2) return false;

        return true;
      }

    }
  };
</script>
