<template>
  <div class="app-page history-workbench">
    <div class="data-toolbar">
      <span class="data-toolbar__title">历史绩效</span>
      <span class="data-toolbar__sep">|</span>
      <span class="data-toolbar__meta">已归档周期的绩效记录，只读浏览</span>
      <span class="data-toolbar__spacer"></span>
      <el-button @click="$router.push('/scores/my')" size="small">返回当期绩效</el-button>
    </div>

    <div class="history-layout" v-if="archivedPeriods.length">
      <div class="hl-left">
        <div class="hll-head">已归档周期（{{ archivedPeriods.length }}）</div>
        <div v-for="p in archivedPeriods" :key="p.id"
          class="hll-item" :class="{ selected: selectedId === p.id }"
          @click="selectPeriod(p.id)">
          <div class="hlli-name">{{ p.name }}</div>
          <div class="hlli-date">{{ p.startDate }} ~ {{ p.endDate }}</div>
        </div>
      </div>
      <div class="hl-right">
        <div v-if="!selectedId" class="hlr-empty">请选择一个已归档周期</div>
        <template v-else-if="loading">加载中...</template>
        <template v-else-if="data">
          <div class="score-hero" v-if="data.finalScore">
            <div class="grade-ring" :class="'grade-'+data.finalScore.grade">
              <span class="grade-letter">{{ data.finalScore.grade }}</span>
            </div>
            <div class="score-main">
              <div class="score-number">{{ data.finalScore.total }}<small> / 100</small></div>
              <div class="score-label">{{ gradeDesc(data.finalScore.grade) }}</div>
              <div class="score-meta">{{ data.period?.name }} · {{ data.period?.startDate }} ~ {{ data.period?.endDate }}</div>
            </div>
            <div class="score-breakdown">
              <div class="break-item">
                <span class="break-label">OKR 得分</span>
                <span class="break-value">{{ data.finalScore.okrScore }}</span>
              </div>
              <div class="break-item">
                <span class="break-label">工作成果</span>
                <span class="break-value">{{ data.finalScore.kpiScore }}</span>
              </div>
              <div class="break-item bonus">
                <span class="break-label">专项加分</span>
                <span class="break-value">+{{ data.finalScore.bonus }}</span>
              </div>
              <div class="break-item penalty">
                <span class="break-label">专项扣分</span>
                <span class="break-value">-{{ data.finalScore.penalty }}</span>
              </div>
            </div>
          </div>
          <div v-else class="no-data">该周期暂无绩效数据</div>

          <div class="compare-row" v-if="data.selfScore && data.finalScore">
            <div class="compare-card self">
              <div class="compare-label">自评</div>
              <div class="compare-score">{{ data.selfScore.total }}<small>分</small></div>
            </div>
            <div class="compare-arrow">&rarr;</div>
            <div class="compare-card final">
              <div class="compare-label">终审</div>
              <div class="compare-score">{{ data.finalScore.total }}<small>分</small></div>
            </div>
          </div>

          <div class="two-col">
            <div class="col okr-col">
              <div class="col-head">当期 OKR 记录</div>
              <div v-for="o in data.okrRecords || []" :key="o.id" class="mini-okr">
                <div class="mini-o">{{ o.title }}</div>
                <div v-for="kr in o.keyResults" :key="kr.id" class="mini-kr">
                  <span>{{ kr.title }}</span>
                  <span class="mini-val">{{ kr.currentValue }}/{{ kr.targetValue }}</span>
                </div>
              </div>
            </div>
            <div class="col feedback-col">
              <div class="col-head">面谈反馈</div>
              <div v-if="data.feedback?.interviewNotes" class="fb-content">
                <div class="fb-block" v-if="data.feedback.highlights"><div class="fb-label">亮点</div><div class="fb-text">{{ data.feedback.highlights }}</div></div>
                <div class="fb-block" v-if="data.feedback.issues"><div class="fb-label">待改进</div><div class="fb-text">{{ data.feedback.issues }}</div></div>
                <div class="fb-block" v-if="data.feedback.improvements"><div class="fb-label">改进建议</div><div class="fb-text">{{ data.feedback.improvements }}</div></div>
              </div>
              <div v-else class="fb-empty">暂无面谈记录</div>
            </div>
          </div>
        </template>
      </div>
    </div>
    <div v-else class="empty-state">暂无已归档的考核周期</div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { periodApi, scoreApi } from '../../api'

const periods = ref([])
const selectedId = ref(null)
const data = ref(null)
const loading = ref(false)

const archivedPeriods = computed(() => periods.value.filter(p => p.status === 'ARCHIVED').reverse())

onMounted(async () => {
  periods.value = await periodApi.list()
  if (archivedPeriods.value.length) selectPeriod(archivedPeriods.value[0].id)
})

async function selectPeriod(pid) {
  selectedId.value = pid
  loading.value = true
  try {
    data.value = await scoreApi.getMyResult(pid)
  } catch { data.value = null }
  finally { loading.value = false }
}

function gradeDesc(g) {
  return { S: '卓越产出 · 团队标杆', A: '超出预期 · 高质量完成', B: '符合预期 · 满足岗位要求', C: '未达预期 · 需整改辅导' }[g] || ''
}
</script>

<style scoped>
.page { width: 100%; }
.page-hero { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 24px; }
.hero-info h2 { font-size: 28px; font-weight: 700; color: var(--color-text); margin: 0; }
.hero-info p { font-size: 14px; color: var(--color-text-secondary); margin: 4px 0 0; }
.empty-state { display: flex; align-items: center; justify-content: center; height: 300px; color: var(--color-text-muted); font-size: 15px; }

.history-layout { display: flex; gap: 20px; align-items: flex-start; }
.hl-left { width: 240px; flex-shrink: 0; background: var(--color-surface); border-radius: 10px; padding: 16px; box-shadow: 0 1px 4px rgba(0,0,0,0.04); border: 1px solid var(--color-border-soft); max-height: calc(100vh - 200px); overflow-y: auto; }
.hll-head { font-size: 13px; font-weight: 700; color: var(--color-text-secondary); margin-bottom: 10px; padding-bottom: 8px; border-bottom: 1px solid var(--color-border-soft); }
.hll-item { padding: 10px 12px; border-radius: 8px; cursor: pointer; margin-bottom: 4px; transition: all 0.15s; }
.hll-item:hover { background: var(--color-surface-tint-brand); }
.hll-item.selected { background: var(--color-surface-tint-brand); border: 1px solid var(--color-brand-soft); }
.hlli-name { font-size: 14px; font-weight: 600; color: var(--color-text); }
.hlli-date { font-size: 11px; color: var(--color-text-muted); margin-top: 2px; }

.hl-right { flex: 1; min-width: 0; }
.hlr-empty { display: flex; align-items: center; justify-content: center; height: 300px; color: var(--color-text-muted); font-size: 15px; }

.no-data { text-align: center; padding: 60px; color: var(--color-text-muted); font-size: 14px; background: var(--color-surface); border-radius: 10px; }

.score-hero {
  display: flex; align-items: center; gap: 32px;
  background: var(--color-surface-tint-brand); border-radius: 8px;
  padding: 32px 40px; margin-bottom: 24px; border: 1px solid var(--color-border-soft);
}
.grade-ring { width: 100px; height: 100px; border-radius: 50%; display: flex; align-items: center; justify-content: center; border: 4px solid var(--color-border); flex-shrink: 0; }
.grade-ring.grade-S { border-color: var(--color-grade-s); background: var(--color-surface-tint-warning); }
.grade-ring.grade-A { border-color: var(--color-success); background: var(--color-surface-tint-success); }
.grade-ring.grade-B { border-color: var(--color-info); background: var(--color-surface-tint-info); }
.grade-ring.grade-C { border-color: var(--color-danger); background: var(--color-surface-tint-danger); }
.grade-letter { font-size: 48px; font-weight: 800; color: var(--color-text); }
.score-main { flex: 1; }
.score-number { font-size: 48px; font-weight: 700; color: var(--color-text); line-height: 1; }
.score-number small { font-size: 20px; color: var(--color-text-muted); font-weight: 400; }
.score-label { font-size: 15px; color: var(--color-text-secondary); margin: 6px 0; }
.score-meta { font-size: 13px; color: var(--color-text-muted); }
.score-breakdown { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 16px; flex-shrink: 0; }
.break-item { text-align: right; }
.break-label { font-size: 12px; color: var(--color-text-muted); display: block; }
.break-value { font-size: 18px; font-weight: 600; color: var(--color-text); }
.break-item.bonus .break-value { color: var(--color-success); }
.break-item.penalty .break-value { color: var(--color-danger); }

.compare-row { display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 24px; }
.compare-card { background: var(--color-surface); border-radius: 8px; padding: 16px 28px; text-align: center; box-shadow: 0 1px 4px rgba(0,0,0,0.04); border: 1px solid var(--color-border-soft); }
.compare-card.self { border-left: 4px solid var(--color-info); }
.compare-card.final { border-left: 4px solid var(--color-success); }
.compare-label { font-size: 12px; color: var(--color-text-muted); margin-bottom: 4px; }
.compare-score { font-size: 28px; font-weight: 700; color: var(--color-text); }
.compare-score small { font-size: 14px; color: var(--color-text-muted); font-weight: 400; }
.compare-arrow { font-size: 24px; color: var(--color-text-muted); }

.two-col { display: flex; gap: 20px; }
.col { flex: 1; background: var(--color-surface); border-radius: 8px; padding: 20px 24px; box-shadow: 0 1px 4px rgba(0,0,0,0.04); border: 1px solid var(--color-border-soft); }
.col-head { font-size: 15px; font-weight: 700; color: var(--color-text); margin-bottom: 16px; }
.mini-okr { padding: 10px; margin-bottom: 10px; background: var(--color-surface-subtle); border-radius: 8px; border: 1px solid var(--color-border-soft); }
.mini-o { font-weight: 600; font-size: 13px; color: var(--color-brand); margin-bottom: 6px; }
.mini-kr { display: flex; justify-content: space-between; font-size: 12px; color: var(--color-text-secondary); padding: 2px 0; }
.mini-val { color: var(--color-text-muted); }
.fb-content { }
.fb-block { margin-bottom: 14px; }
.fb-label { font-size: 11px; font-weight: 600; color: var(--color-accent); letter-spacing: 1px; margin-bottom: 4px; }
.fb-text { font-size: 13px; color: var(--color-text-secondary); line-height: 1.6; }
.fb-empty { color: var(--color-text-muted); font-size: 13px; text-align: center; padding: 30px; }
</style>
