<template>
  <div class="app-page history-workbench">
    <div class="page-hero">
      <div class="hero-info">
        <h2>历史绩效</h2>
        <p>已归档周期的绩效记录，只读浏览</p>
      </div>
      <el-button @click="$router.push('/scores/my')" round>← 返回当期绩效</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">→</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: #1a1a2e; margin: 0; }
.hero-info p { font-size: 14px; color: #888; margin: 4px 0 0; }
.empty-state { display: flex; align-items: center; justify-content: center; height: 300px; color: #bbb; font-size: 15px; }

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

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

.no-data { text-align: center; padding: 60px; color: #ccc; font-size: 14px; background: #fff; border-radius: 10px; }

.score-hero {
  display: flex; align-items: center; gap: 32px;
  background: #f6f6fb; border-radius: 8px;
  padding: 32px 40px; margin-bottom: 24px; border: 1px solid #e8e8f0;
}
.grade-ring { width: 100px; height: 100px; border-radius: 50%; display: flex; align-items: center; justify-content: center; border: 4px solid #ddd; flex-shrink: 0; }
.grade-ring.grade-S { border-color: #f1c40f; background: #fef9e7; }
.grade-ring.grade-A { border-color: #27ae60; background: #eafaf1; }
.grade-ring.grade-B { border-color: #3498db; background: #ebf5fb; }
.grade-ring.grade-C { border-color: #e74c3c; background: #fdedec; }
.grade-letter { font-size: 48px; font-weight: 800; color: #1a1a2e; }
.score-main { flex: 1; }
.score-number { font-size: 48px; font-weight: 700; color: #1a1a2e; line-height: 1; }
.score-number small { font-size: 20px; color: #999; font-weight: 400; }
.score-label { font-size: 15px; color: #555; margin: 6px 0; }
.score-meta { font-size: 13px; color: #aaa; }
.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: #999; display: block; }
.break-value { font-size: 18px; font-weight: 600; color: #333; }
.break-item.bonus .break-value { color: #27ae60; }
.break-item.penalty .break-value { color: #e74c3c; }

.compare-row { display: flex; align-items: center; justify-content: center; gap: 20px; margin-bottom: 24px; }
.compare-card { background: #fff; border-radius: 8px; padding: 16px 28px; text-align: center; box-shadow: 0 1px 4px rgba(0,0,0,0.04); border: 1px solid #f0f0f0; }
.compare-card.self { border-left: 4px solid #3498db; }
.compare-card.final { border-left: 4px solid #27ae60; }
.compare-label { font-size: 12px; color: #999; margin-bottom: 4px; }
.compare-score { font-size: 28px; font-weight: 700; color: #1a1a2e; }
.compare-score small { font-size: 14px; color: #999; font-weight: 400; }
.compare-arrow { font-size: 24px; color: #ccc; }

.two-col { display: flex; gap: 20px; }
.col { flex: 1; background: #fff; border-radius: 8px; padding: 20px 24px; box-shadow: 0 1px 4px rgba(0,0,0,0.04); border: 1px solid #f0f0f0; }
.col-head { font-size: 15px; font-weight: 700; color: #1a1a2e; margin-bottom: 16px; }
.mini-okr { padding: 10px; margin-bottom: 10px; background: #fafbfc; border-radius: 8px; border: 1px solid #f0f0f0; }
.mini-o { font-weight: 600; font-size: 13px; color: #2b1f99; margin-bottom: 6px; }
.mini-kr { display: flex; justify-content: space-between; font-size: 12px; color: #666; padding: 2px 0; }
.mini-val { color: #999; }
.fb-content { }
.fb-block { margin-bottom: 14px; }
.fb-label { font-size: 11px; font-weight: 600; color: #3ad4d8; letter-spacing: 1px; margin-bottom: 4px; }
.fb-text { font-size: 13px; color: #555; line-height: 1.6; }
.fb-empty { color: #ccc; font-size: 13px; text-align: center; padding: 30px; }
</style>
