<template>
  <el-form ref="form" :model="pwd" :rules="rules" label-width="80px">
    <el-form-item label="旧密码" prop="oldPassword">
      <el-input v-model="pwd.oldPassword" placeholder="请输入旧密码" type="password" show-password/>
    </el-form-item>
    <el-form-item label="新密码" prop="newPassword">
      <el-input v-model="pwd.newPassword" placeholder="请输入新密码" type="password" show-password/>
    </el-form-item>
    <el-form-item label="确认密码" prop="confirmPassword">
      <el-input v-model="pwd.confirmPassword" placeholder="请确认新密码" type="password" show-password/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { mapGetters } from 'vuex'
import {doEncrypt, doDecryptStr,doSign,doVerifySignature,userinfoEncrypt,userinfoDecrypt} from '@/utils/smutil'
import { debounce } from 'lodash'
import {removeToken} from "@/utils/auth";
export default {
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.pwd.newPassword !== value) {
        callback(new Error("新密码两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      user: {},
      pwd: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: "旧密码不能为空", trigger: "blur" }
        ],
        newPassword: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
          { min: 8, max: 20, message: "长度在 8 到 20 个字符", trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ]
      },
      //字母连续规则
      // strReg : /(a(?=b)|b(?=c)|c(?=d)|d(?=e)|e(?=f)|f(?=g)|g(?=h)|h(?=i)|i(?=j)|j(?=k)|k(?=l)|l(?=m)|m(?=n)|n(?=o)|o(?=p)|p(?=q)|q(?=r)|r(?=s)|s(?=t)|t(?=u)|u(?=v)|v(?=w)|w(?=x)|x(?=y)|y(?=z)|z(?=a)){3}[a-z]/i,
      // strResverReg : /(a(?=z)|z(?=y)|y(?=x)|x(?=w)|w(?=v)|v(?=u)|u(?=t)|t(?=s)|s(?=r)|r(?=q)|q(?=p)|p(?=o)|o(?=n)|n(?=m)|m(?=l)|l(?=k)|k(?=j)|j(?=i)|i(?=h)|h(?=g)|g(?=f)|f(?=e)|e(?=d)|d(?=c)|c(?=b)|b(?=a)){3}[a-z]/i,
      //数字连续规则
      // numReg : /(0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)|9(?=0)){3}\d/,
      // numResverReg : /(0(?=9)|9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){3}\d/,
      //键盘字母横向连续规则
      // keyboardHorizontalReg : /(q(?=w)|w(?=e)|e(?=r)|r(?=t)|t(?=y)|y(?=u)|u(?=i)|i(?=o)|o(?=p)|p(?=q)|a(?=s)|s(?=d)|d(?=f)|f(?=g)|g(?=h)|h(?=j)|j(?=k)|k(?=l)|l(?=a)|z(?=x)|x(?=c)|c(?=v)|v(?=b)|b(?=n)|n(?=m)|m(?=z)){3}[a-z]/i,
      // keyboardHorizontalResverReg : /(p(?=o)|o(?=i)|i(?=u)|u(?=y)|y(?=t)|t(?=r)|r(?=e)|e(?=w)|w(?=q)|q(?=p)|l(?=k)|k(?=j)|j(?=h)|h(?=g)|g(?=f)|f(?=d)|d(?=s)|s(?=a)|a(?=l)|m(?=n)|n(?=b)|b(?=v)|v(?=c)|c(?=x)|x(?=z)|z(?=m)){3}[a-z]/i,
      //多个相同字母、数字规则
      // sameReg : /([0-9a-zA-Z])\1{3}/,
      // keyboardSlopeArr : ["1qaz", "2wsx", "3edc", "4rfv", "5tgb", "6yhn", "7ujm", "8ik,", "9ol.", "0p;/", "/;p0", ".lo9", ",ki8", "mju7", "nhy6", "bgt5", "vfr4", "cde3", "xsw2", "zaq1", "4esz", "5rdx", "6tfc", "7ygv", "8uhb", "9ijn", "0okm", "-pl,", "=[;.", ".;[=", ",lp-", "mko0", "nji9", "bhu8", "vgy7", "cft6", "xdr5", "zse4"],
      // keyboardSlopeShiftArr : ["!qaz", "@wsx", "#edc","$rfv", "%tgb", "^yhn", "&ujm", "*ik<", "(ol>", ")p:?", "?:p)", ">lo(", "<ki*", "mju&", "nhy^", "bgt%", "vfr$", "cde#", "xsw@", "zaq!", "$esz", "%rdx", "^tfc", "&ygv", "*uhb", "(ijn", ")okm", "_pl<", "+{:>", ">:{+", "<lp_", "mko)", "nji(", "bhu*", "vgy&", "cft^", "xdr%", "zse$"],
      // lastReg: /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$%^&*`~()-+=]+$)(?![0-9\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\W_!@#$%^&*`~()-+=]{8,20}$/
    };
  },
  methods: {
    // 表单重置
    pwdReset() {
      this.pwd={
        oldPassword: undefined,
          newPassword: undefined,
          confirmPassword: undefined
      }
    },
    submit:debounce(function(){
      this.$refs["form"].validate(async valid => {
        if (valid) {
          // 密码验证规则
          // if (this.sameReg.test(this.pwd.newPassword)) {
          //   this.$message.error('密码不能含有连续4位相同的数字或字母')
          //   return
          // } else if (this.strResverReg.test(this.pwd.newPassword.toLowerCase())) {
          //   this.$message.error('密码不能含有4位连续的字母')
          //   return
          // } else if (this.strReg.test(this.pwd.newPassword.toLowerCase())) {
          //   this.$message.error('密码不能含有4位连续的字母')
          //   return
          // } else if (this.numReg.test(this.pwd.newPassword)) {
          //   this.$message.error('密码不能含有4位连续的数字')
          //   return
          // } else if (this.numResverReg.test(this.pwd.newPassword)) {
          //   this.$message.error('密码不能含有4位连续的数字')
          //   return
          // } else if (this.keyboardHorizontalReg.test(this.pwd.newPassword)) {
          //   this.$message.error('密码不能含有4位横向连续的字母')
          //   return
          // } else if (this.keyboardHorizontalResverReg.test(this.pwd.newPassword)) {
          //   this.$message.error('密码不能含有4位横向连续的字母')
          //   return
          // } else if (this.keyboardSlopeArr.some(v => this.pwd.newPassword.toLowerCase().indexOf(v) > -1)) {
          //   this.$message.error('密码不能含有4位键盘斜向连续的字符')
          //   return
          // } else if (this.keyboardSlopeShiftArr.some(v => this.pwd.newPassword.toLowerCase().indexOf(v) > -1)) {
          //   this.$message.error('密码不能含有4位键盘斜向连续的字符')
          //   return
          // } else if (!this.lastReg.test(this.pwd.newPassword)) {
          //   this.$message.error('密码不满足8~20位大写字母、小写字母、数字、特殊字符三种以上的组合')
          //   return
          // }

          // 用户信息解密
          let decryptUserInfo = userinfoDecrypt(localStorage.getItem('active'))
          this.user = JSON.parse(decryptUserInfo).sysUser
          const param = {
            id: this.user.id,
            oldPassword: this.pwd.oldPassword,
            newPassword: this.pwd.newPassword,
            confirmPassword: this.pwd.confirmPassword,
          }
          await this.$axios.post('/sysUserController/updatePassword', param).then((res) => {
            if (res.code==0){
              this.pwdReset()
              this.$message.success('密码修改成功，请重新登录！')
              this.$axios.post(
                '/logout',{}
              ).then((res) => {
                removeToken()
                //注销返回自己的登录页
                this.$router.push(`/login?redirect=${this.$route.fullPath}`)
              })
            }
            if (res.code==1){
              this.$message.error(res.data)
            }
          }).catch((error) => {
            // this.$message.error(error)
          })
        }
      });
    },1000),
    close() {
      this.$tab.closePage();
    }
  }
};
</script>

<!--<style rel="stylesheet/scss" lang="scss" scoped>-->
<!--.no-autofill-pwd{-->
<!--  text-security:disc!important;-->
<!--  -webkit-text-security:disc!important;-->
<!--}-->
<!--</style>-->
