<template>
  <a-modal
    :title="title"
    :visible="open"
    :confirm-loading="submitLoading"
    @ok="submitForm"
    @cancel="cancel"
  >
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="用户名" prop="userName">
        <a-input v-model="form.userName" :disabled="true"/>
      </a-form-model-item>
      <a-form-model-item  label="新密码" prop="newPassword">
        <a-input-password v-model="form.newPassword" placeholder="请输入新密码" :maxLength="20" />
      </a-form-model-item>
      <a-form-model-item  label="确认密码" prop="confirmPassword">
        <a-input-password v-model="form.confirmPassword" placeholder="请确认密码" :maxLength="20" />
      </a-form-model-item>
    </a-form-model>
    <span style="font-size: 12px;display: inline-block;margin-bottom: 10px;">密码规则：<span style="color: brown;">密码中必须包含大小字母、数字、特殊字符，至少8个字符，最多16个字符。不能相同字符（如111、aaa、123、abc）连续3位或3位以上！</span></span>
  </a-modal>
</template>
<script>
import { resetUserPwd } from '@/api/system/user'

export default {
  name: 'ResetPassword',
  props: {
  },
  data () {
    const validateNewPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else if (!/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$)([^\u4e00-\u9fa5\s]){5,20}$/.test(value)) {
        callback(new Error('密码中必须包含大小字母、数字、特殊字符，至少8个字符，最多16个字符。不能相同字符（如111、aaa、123、abc）连续3位或3位以上！'))
      } else {
        if (this.form.confirmPassword !== '') {
          this.$refs.form.validateField('confirmPassword')
        }
        callback()
      }
    }
    const validateConfirmPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码确认'))
      } else if (value !== this.form.newPassword) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      submitLoading: false,
      title: '重置密码',
      open: false,
      childrenDrawer: false,
      formLayout: 'horizontal',
      form: {
        userName: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      rules: {
        newPassword: [
          { required: true, validator: validateNewPass, trigger: 'change' }
        ],
        confirmPassword: [
          { required: true, validator: validateConfirmPass, trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        userId: undefined,
        userName: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      }
    },
    handleResetPwd (row) {
      this.form = {
        userId: row.userId,
        userName: row.nickName
      }
      this.open = true
    },
    LxStr(str) {
      var arr = str.split("");
      var flag = true;
      for (var i = 1; i < arr.length - 1; i++) {
        var firstIndex = arr[i - 1].charCodeAt();
        var secondIndex = arr[i].charCodeAt();
        var thirdIndex = arr[i + 1].charCodeAt();
        thirdIndex - secondIndex == 1;
        secondIndex - firstIndex == 1;
        if (thirdIndex - secondIndex == 1 && secondIndex - firstIndex == 1) {
          flag = false;
        }
      }
      if (!flag) {
        this.$message.error("密码不能连续字符（如123、abc）连续3位或3位以上");
        return flag;
      }
      return flag;
    },
    /** 重置密码按钮操作 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          var regex = new RegExp(
              "(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9]).{8,16}"
            );
            var re = /(\w)*(\w)\2{2}(\w)*/g;
            if (!regex.test(this.form.newPassword)) {
              this.submitLoading = false
              return this.$message.error(
                "您的密码复杂度太低,请仔细查看密码规则"
              );
            }
            if (re.test(this.form.newPassword)) {
              this.submitLoading = false
              return this.$message.error(
                "您的密码复杂度太低,请勿输入3个及3个以上相同字符"
              );
            }
            if (!this.LxStr(this.form.newPassword)) {
              this.submitLoading = false
              return;
            }
           
          resetUserPwd(this.form.userId, this.form.newPassword).then(response => {
            this.$message.success(
              '重置成功',
              3
            )
            this.open = false
          }).finally(() => {
            this.submitLoading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
