<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="90px">
    <el-form-item label="旧密码" prop="oldPassword">
      <el-input v-model="user.oldPassword" placeholder="请输入旧密码" type="password" show-password/>
    </el-form-item>
    <el-form-item label="新密码" prop="newPassword">
      <el-input v-model="user.newPassword" placeholder="请输入新密码" type="password" show-password @input="checkPasswordStrength"/>
      <div v-if="user.newPassword" class="password-strength">
        <div :class="['strength-bar', strengthClass]"></div>
      </div>
      <div v-if="user.newPassword" class="password-tips">
        密码强度: {{ strengthText }}
        <div v-if="user.newPassword && !isPasswordStrong" class="weak-password-tips">
          密码必须包含大写字母、小写字母、数字和特殊字符
        </div>
      </div>
    </el-form-item>
    <el-form-item label="确认密码" prop="confirmPassword">
      <el-input v-model="user.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 { updateUserPwd } from "@/api/system/user";

export default {
  data() {
    // 验证密码复杂度
    const validatePasswordComplexity = (rule, value, callback) => {
      if (!value) {
        callback(new Error("新密码不能为空"));
        return;
      }

      // 检查密码复杂度
      const hasLowerCase = /[a-z]/.test(value);
      const hasUpperCase = /[A-Z]/.test(value);
      const hasNumber = /[0-9]/.test(value);
      const hasSpecialChar = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(value);

      if (!hasLowerCase || !hasUpperCase || !hasNumber || !hasSpecialChar) {
        callback(new Error("密码必须包含大小写字母、数字和特殊字符"));
        return;
      }

      callback();
    };

    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };

    return {
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined
      },
      strengthClass: 'weak',
      strengthText: '弱',
      isPasswordStrong: false,
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: "旧密码不能为空", trigger: "blur" }
        ],
        newPassword: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
          { min: 8, message: "密码长度至少为8个字符", trigger: "blur" },
          { validator: validatePasswordComplexity, trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 检查密码强度
    checkPasswordStrength() {
      if (!this.user.newPassword) {
        this.strengthClass = 'weak';
        this.strengthText = '弱';
        this.isPasswordStrong = false;
        return;
      }

      let strength = 0;

      // 长度检查
      if (this.user.newPassword.length >= 8) strength++;

      // 包含小写字母
      if (/[a-z]/.test(this.user.newPassword)) strength++;

      // 包含大写字母
      if (/[A-Z]/.test(this.user.newPassword)) strength++;

      // 包含数字
      if (/[0-9]/.test(this.user.newPassword)) strength++;

      // 包含特殊字符
      if (/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(this.user.newPassword)) strength++;

      // 设置强度等级
      if (strength <= 2) {
        this.strengthClass = 'weak';
        this.strengthText = '弱';
        this.isPasswordStrong = false;
      } else if (strength <= 4) {
        this.strengthClass = 'medium';
        this.strengthText = '中';
        this.isPasswordStrong = false;
      } else {
        this.strengthClass = 'strong';
        this.strengthText = '强';
        this.isPasswordStrong = true;
      }
    },
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 再次确认密码强度
          if (!this.isPasswordStrong) {
            this.$message.error("密码强度不足，请确保包含大小写字母、数字和特殊字符");
            return;
          }

          updateUserPwd(this.user.oldPassword, this.user.newPassword).then(response => {
            this.$modal.msgSuccess("修改成功");
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>

<style scoped>
.password-strength {
  height: 6px;
  margin-top: 8px;
  background: #eee;
  border-radius: 3px;
  overflow: hidden;
}

.strength-bar {
  height: 100%;
  transition: all 0.3s ease;
}

.strength-bar.weak {
  width: 33%;
  background: #f56c6c;
}

.strength-bar.medium {
  width: 66%;
  background: #e6a23c;
}

.strength-bar.strong {
  width: 100%;
  background: #67c23a;
}

.password-tips {
  font-size: 12px;
  color: #909399;
  margin-top: 6px;
}

.weak-password-tips {
  color: #f56c6c;
  margin-top: 4px;
}
</style>
