<template>
  <aside class="project-analysis-sidebar" :class="{ 'is-collapsed': collapsed }">
    <div v-if="collapsed" class="sidebar-collapsed-card">
      <button
        v-if="collapsible"
        class="sidebar-card-toggle"
        type="button"
        title="展开"
        aria-label="展开"
        @click="$emit('toggle')"
      >
        <i class="el-icon-arrow-right" />
      </button>
      <span>人物档案</span>
      <strong>{{ sidebarData.basicInfo.name || "-" }}</strong>
    </div>
    <div v-else class="sidebar-profile-card">
      <button
        v-if="collapsible"
        class="sidebar-card-toggle"
        type="button"
        title="收起"
        aria-label="收起"
        @click="$emit('toggle')"
      >
        <i class="el-icon-arrow-left" />
      </button>
      <section class="sidebar-profile">
        <div class="sidebar-profile__identity">
          <div class="sidebar-profile__identity-label">人物档案</div>
          <div class="sidebar-profile__name-row">
            <div class="sidebar-profile__name">{{ sidebarData.basicInfo.name || "-" }}</div>
            <div class="sidebar-risk-badge">{{ sidebarData.basicInfo.riskLevel || "-" }}</div>
          </div>
        </div>
        <div class="sidebar-profile__meta">
          <div class="sidebar-profile__item">
            <span class="sidebar-profile__label">{{ normalizedFieldLabels.staffCode }}</span>
            <span class="sidebar-profile__value">{{ sidebarData.basicInfo.staffCode || "-" }}</span>
          </div>
          <div class="sidebar-profile__item">
            <span class="sidebar-profile__label">{{ normalizedFieldLabels.department }}</span>
            <span class="sidebar-profile__value">{{ sidebarData.basicInfo.department || "-" }}</span>
          </div>
          <div class="sidebar-profile__item">
            <span class="sidebar-profile__label">{{ normalizedFieldLabels.projectName }}</span>
            <span class="sidebar-profile__value">{{ sidebarData.basicInfo.projectName || "-" }}</span>
          </div>
        </div>
      </section>

      <section class="sidebar-summary">
        <div class="sidebar-summary__count">
          <span class="sidebar-summary__count-label">命中模型</span>
          <span class="sidebar-summary__count-value">{{ sidebarData.modelSummary.modelCount || "-" }}</span>
        </div>
        <div class="sidebar-summary__tags">
          <span class="sidebar-profile__label">异常标签</span>
          <div v-if="sidebarData.modelSummary.riskTags.length" class="sidebar-tag-list">
            <el-tag
              v-for="(tag, index) in sidebarData.modelSummary.riskTags"
              :key="`${formatRiskTag(tag)}-${index}`"
              size="mini"
              effect="plain"
            >
              {{ formatRiskTag(tag) }}
            </el-tag>
          </div>
          <span v-else class="sidebar-profile__value">暂无异常标签</span>
        </div>
      </section>
    </div>
  </aside>
</template>

<script>
export default {
  name: "ProjectAnalysisSidebar",
  props: {
    sidebarData: {
      type: Object,
      default: () => ({
        basicInfo: {},
        modelSummary: {
          riskTags: [],
        },
      }),
    },
    fieldLabels: {
      type: Object,
      default: () => ({
        staffCode: "工号",
        department: "部门",
        projectName: "所属项目",
      }),
    },
    collapsible: {
      type: Boolean,
      default: false,
    },
    collapsed: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    normalizedFieldLabels() {
      return {
        staffCode: this.fieldLabels.staffCode || "工号",
        department: this.fieldLabels.department || "部门",
        projectName: this.fieldLabels.projectName || "所属项目",
      };
    },
  },
  methods: {
    formatRiskTag(tag) {
      if (typeof tag === "string") {
        return tag;
      }
      if (tag && typeof tag === "object") {
        return tag.ruleName || tag.label || tag.name || "";
      }
      return "";
    },
  },
};
</script>

<style lang="scss" scoped>
.project-analysis-sidebar {
  flex: 1;
  min-width: 0;
  width: 100%;
  align-self: flex-start;
  background: #ffffff;
}

.project-analysis-sidebar.is-collapsed {
  width: 46px;
}

.sidebar-profile-card {
  position: relative;
  border: 1px solid #dbe4ef;
  border-radius: 6px;
  background: #ffffff;
  box-shadow: none;
  overflow: hidden;
}

.sidebar-card-toggle {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 2;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  padding: 0;
  border: 1px solid #d5dfeb;
  border-radius: 6px;
  background: #ffffff;
  color: #4f6077;
  font-size: 12px;
  cursor: pointer;
}

.sidebar-card-toggle:hover {
  border-color: #9db7d4;
  color: #245b8f;
  background: #eef5fb;
}

.sidebar-profile {
  padding: 22px 22px 6px;
  border-bottom: 0;
}

.sidebar-summary {
  padding: 0 22px 22px;
  border-top: 0;
  background: transparent;
}

.sidebar-profile__identity-label {
  padding-right: 32px;
  color: #637187;
  font-size: 14px;
  font-weight: 700;
  margin-bottom: 18px;
}

.sidebar-profile__name-row {
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.sidebar-profile__name {
  margin-top: 8px;
  font-size: 24px;
  font-weight: 600;
  line-height: 1.2;
  color: #0f172a;
  min-width: 0;
  word-break: break-all;
}

.sidebar-risk-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 26px;
  padding: 0 10px;
  border: 1px solid #edcaca;
  border-radius: 6px;
  background: #fbefef;
  font-size: 12px;
  font-weight: 600;
  line-height: 24px;
  color: #ad2f2f;
}

.sidebar-profile__meta {
  display: grid;
  gap: 0;
  margin-top: 20px;
}

.sidebar-profile__item {
  display: grid;
  grid-template-columns: 72px minmax(0, 1fr);
  gap: 10px;
  padding: 12px 0;
  border-bottom: 1px solid #edf1f5;
}

.sidebar-profile__item:last-child {
  border-bottom: 0;
}

.sidebar-profile__label,
.sidebar-summary__count-label {
  font-size: 12px;
  color: #64748b;
}

.sidebar-profile__value,
.sidebar-summary__count-value {
  font-size: 13px;
  line-height: 1.6;
  color: #0f172a;
  word-break: break-all;
}

.sidebar-summary__count {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin: 4px 0 14px;
}

.sidebar-summary__tags {
  display: grid;
  gap: 10px;
  margin-top: 0;
}

.sidebar-summary__count-label {
  font-size: 13px;
}

.sidebar-summary__count-value {
  color: #172033;
  font-size: 24px;
  font-weight: 700;
  line-height: 1;
}

.sidebar-tag-list {
  display: flex;
  width: 100%;
  flex-wrap: wrap;
  gap: 8px;
}

.sidebar-tag-list ::v-deep(.el-tag) {
  height: 26px;
  padding: 0 9px;
  border: 1px solid #bfd0e2;
  border-radius: 6px;
  background: #ffffff;
  color: #245b8f;
  font-size: 12px;
  font-weight: 600;
  line-height: 24px;
}

.sidebar-collapsed-card {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 220px;
  width: 46px;
  padding: 44px 0 14px;
  border: 1px solid #dbe4ef;
  border-radius: 6px;
  background: #ffffff;
  color: #637187;
  writing-mode: vertical-rl;
  text-orientation: mixed;
}

.sidebar-collapsed-card .sidebar-card-toggle {
  top: 10px;
  left: 50%;
  right: auto;
  transform: translateX(-50%);
  writing-mode: horizontal-tb;
}

.sidebar-collapsed-card span {
  font-size: 13px;
  font-weight: 700;
}

.sidebar-collapsed-card strong {
  max-height: 136px;
  color: #172033;
  font-size: 16px;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
