<template>
  <div v-if="showStrengthMeter" class="password-strength-container" :class="{ compact: compact }">
    <!-- 加载状态显示 -->
    <div v-if="!configLoaded" class="config-loading">
      <el-icon class="is-loading"><Loading /></el-icon>
      <span>加载配置中...</span>
    </div>

    <!-- 密码强度显示 -->
    <template v-else>
      <div class="password-strength-header">
        <span class="strength-label">密码强度：</span>
        <div class="strength-meter">
          <div class="strength-meter-fill" :style="{ width: strengthScore + '%', backgroundColor: strengthColor }"></div>
        </div>
        <span class="strength-level" :style="{ color: strengthColor }">{{ strengthLevel }}</span>
      </div>

      <!-- 密码规则详情 -->
      <div v-if="showRules" class="password-rules-container">
        <div class="password-rules">
          <div v-for="rule in passwordRules" :key="rule.key" class="rule-item" :class="{ 'rule-met': rule.valid, 'rule-empty': !currentPassword }">
            <el-icon>
              <component :is="rule.icon" />
            </el-icon>
            <span>{{ rule.label }}</span>
          </div>
        </div>
      </div>
    </template>
  </div>
</template>

<script setup lang="ts">
import { Loading } from '@element-plus/icons-vue';
import type { PasswordRule } from '@/api/system/security/types';

interface Props {
  // 密码强度数据
  strengthScore: number;
  strengthLevel: string;
  strengthColor: string;

  // 密码规则验证状态
  passwordRules: PasswordRule[];

  // 当前密码值
  currentPassword: string;

  // 配置加载状态
  configLoaded: boolean;

  // 显示控制
  showStrengthMeter?: boolean;
  showRules?: boolean;
  compact?: boolean; // 紧凑模式
}

const props = withDefaults(defineProps<Props>(), {
  showStrengthMeter: true,
  showRules: true,
  compact: false,
  configLoaded: true
});
</script>

<style scoped>
.config-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  color: #909399;
  font-size: 14px;
}

.config-loading .el-icon {
  margin-right: 8px;
}

/* 密码强度样式 */
.password-strength-container {
  margin-top: 12px;
  background-color: #f8f9fa;
  border-radius: 6px;
  padding: 12px;
  border: 1px solid #e9ecef;
}

.password-strength-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.strength-label {
  font-size: 13px;
  color: #606266;
  font-weight: 500;
  white-space: nowrap;
}

.strength-meter {
  flex: 1;
  height: 6px;
  background-color: #e9ecef;
  border-radius: 3px;
  overflow: hidden;
}

.strength-meter-fill {
  height: 100%;
  border-radius: 3px;
  transition:
    width 0.3s ease,
    background-color 0.3s ease;
}

.strength-level {
  font-size: 13px;
  font-weight: 600;
  min-width: 20px;
  text-align: center;
}

.password-rules-container {
  border-top: 1px solid #e9ecef;
  padding-top: 12px;
}

.password-rules {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.rule-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #909399;
  transition: color 0.3s ease;
}

.rule-item.rule-met {
  color: #67c23a;
  font-weight: 500;
}

.rule-item.rule-empty {
  color: #c0c4cc;
}

.rule-item .el-icon {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
}

/* 紧凑模式样式 */
.password-strength-container.compact {
  padding: 8px;
  margin-top: 8px;
}

.password-strength-container.compact .password-strength-header {
  margin-bottom: 8px;
}

.password-strength-container.compact .password-rules {
  grid-template-columns: 1fr;
  gap: 4px;
}

@media (max-width: 768px) {
  .password-rules {
    grid-template-columns: 1fr;
  }
}
</style>
