<template>
  <aside class="panel-right" aria-label="上下文面板">
    <!-- Idle: doctor image -->
    <DoctorAssistantFigure v-if="store.contextPanelMode === 'idle'" />

    <!-- Loading: skeleton timeline -->
    <template v-else-if="store.contextPanelMode === 'loading'">
      <div class="pr-header">
        <div class="pr-label">分析中</div>
      </div>
      <div class="timeline-skeleton">
        <div class="sk-item" v-for="i in 6" :key="i">
          <div class="sk-dot" />
          <div class="sk-line" />
        </div>
      </div>
    </template>

    <!-- Task / Completed: timeline + card -->
    <template v-else-if="store.contextPanelMode === 'task' || store.contextPanelMode === 'completed'">
      <div class="pr-header">
        <div class="pr-label">
          {{ store.displayedCard?.cardKey === 'appointment-success' ? '挂号结果' : '挂号进度' }}
        </div>
      </div>
      <RegistrationProgress />
      <div class="panel-card-container">
        <CardRenderer
          v-if="store.displayedCard"
          :card="store.displayedCard"
          @action="handleAction"
          @finish="store.resetToIdle"
        />
      </div>
    </template>

    <!-- Error -->
    <template v-else>
      <div class="pr-header">
        <div class="pr-label">异常</div>
      </div>
      <ErrorCard
        :message="store.errorMessage"
        :trace-id="store.currentTraceId"
        @restart="store.resetToIdle"
      />
    </template>
  </aside>
</template>

<script setup lang="ts">
import CardRenderer from '../cards/CardRenderer.vue';
import ErrorCard from '../cards/ErrorCard.vue';
import { useTerminalStore } from '../state/terminalStore';
import DoctorAssistantFigure from './DoctorAssistantFigure.vue';
import RegistrationProgress from './RegistrationProgress.vue';

const store = useTerminalStore();

async function handleAction(data: { actionName: string; payload: Record<string, unknown> }) {
  store.saveSelection(data.payload);
  try {
    await store.submitCard(data.payload);
  } catch {
    // error already set in store
  }
}
</script>

<style scoped>
.panel-right {
  background: var(--surface);
  border-left: 1px solid var(--border);
  padding: 24px 20px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.pr-header {
  margin-bottom: 20px;
}

.pr-label {
  font-size: var(--fs-xs);
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 500;
}

.panel-card-container {
  flex: 1;
  overflow-y: auto;
}

/* ─── Loading skeleton timeline ─── */
.timeline-skeleton {
  position: relative;
  padding-left: 28px;
}

.timeline-skeleton::before {
  content: '';
  position: absolute;
  left: 10px;
  top: 8px;
  bottom: 8px;
  width: 0;
  border-left: 2px dotted var(--border);
}

.sk-item {
  position: relative;
  padding: 8px 0 8px 14px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.sk-dot {
  position: absolute;
  left: -22px;
  top: 12px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--border);
  border: 2px solid var(--surface);
  z-index: 1;
}

.sk-line {
  height: 12px;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--border) 25%, var(--bg) 50%, var(--border) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s ease-in-out infinite;
  width: 100%;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
</style>
