<template>
  <section class="success-card">
    <div class="success-icon">
      <svg width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="var(--accent-light)" stroke-width="2.2">
        <polyline points="20,6 9,17 4,12" />
      </svg>
    </div>
    <div class="success-title">挂号成功</div>
    <div class="success-subtitle">联调演示</div>

    <div class="appt-grid">
      <div
        v-for="(item, idx) in gridItems"
        :key="item.label"
        :class="['appt-cell', { full: isLastOddCell(idx) }]"
      >
        <div class="lb">{{ item.label }}</div>
        <div :class="['vl', { hl: item.highlight }]">{{ item.value }}</div>
      </div>
      <div v-if="queueNo" class="appt-cell full">
        <div class="lb">就诊序号</div>
        <div class="vl queue-no">{{ queueNo }}</div>
      </div>
    </div>

    <div v-if="appointmentNo" class="appt-no-line">预约号 <span class="apt-no">{{ appointmentNo }}</span></div>

    <div class="tips-box">
      <div class="tl">温馨提示</div>
      <div class="tt">请提前 <strong>15 分钟</strong>到达候诊区签到。就诊时请出示二维码凭证。</div>
    </div>

    <button class="btn-accent" @click="$emit('finish')">完成</button>
  </section>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import type { CardInstance } from '../api/types';
import { useTerminalStore } from '../state/terminalStore';

const props = defineProps<{ card: CardInstance }>();
defineEmits<{ finish: [] }>();

const store = useTerminalStore();
const data = computed(() => props.card.cardData);

const patientName = computed(() => (data.value.patientName as string) || '');
const departmentName = computed(() => (data.value.departmentName as string) || store.selection.departmentName || '');
const doctorDisplay = computed(() => {
  const name = (data.value.doctorName as string) || store.selection.doctorName || '';
  const title = (data.value.doctorTitle as string) || store.selection.doctorTitle || '';
  return name ? (name + (title ? ` ${title}` : '')) : '';
});
const visitDate = computed(() => (data.value.visitDate as string) || '');
const visitTime = computed(() => (data.value.timePeriod as string) || store.selection.timePeriod || '');
const room = computed(() => (data.value.room as string) || store.selection.room || '');
const queueNo = computed(() => (data.value.queueNo as string) || '');
const appointmentNo = computed(() => (data.value.appointmentNo as string) || (data.value.appointmentNoStr as string) || '');

interface GridItem {
  label: string;
  value: string;
  highlight?: boolean;
}

const gridItems = computed<GridItem[]>(() => {
  const items: GridItem[] = [];
  if (patientName.value) items.push({ label: '患者', value: patientName.value });
  if (departmentName.value) items.push({ label: '科室', value: departmentName.value });
  if (doctorDisplay.value) items.push({ label: '医生', value: doctorDisplay.value });
  if (visitDate.value) items.push({ label: '就诊日期', value: visitDate.value, highlight: true });
  if (visitTime.value) items.push({ label: '时段', value: visitTime.value });
  if (room.value) items.push({ label: '诊室', value: room.value });
  return items;
});

function isLastOddCell(index: number) {
  const total = gridItems.value.length;
  return total % 2 === 1 && index === total - 1;
}
</script>

<style scoped>
.success-card {
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px 18px 20px;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.success-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, var(--accent), var(--accent-light));
}

.success-icon {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: color-mix(in oklch, var(--accent-light) 25%, transparent);
  display: grid;
  place-items: center;
  margin: 0 auto 12px;
}

.success-title {
  font-family: var(--font-display);
  font-size: var(--fs-h3);
  font-weight: 600;
  letter-spacing: -0.01em;
  margin-bottom: 4px;
}

.success-subtitle {
  font-size: 11px;
  color: var(--muted);
  font-family: var(--font-mono);
  margin-bottom: 14px;
}

.appt-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  text-align: left;
}

.appt-cell {
  background: var(--surface);
  padding: 10px 14px;
}

.appt-cell .lb {
  font-size: 10px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 1px;
}

.appt-cell .vl {
  font-size: var(--fs-sm);
  font-weight: 500;
}

.appt-cell .vl.hl {
  font-family: var(--font-display);
  color: var(--accent);
  font-size: 18px;
}

.appt-cell .vl.queue-no {
  font-family: var(--font-mono);
  font-weight: 600;
  color: var(--accent);
}

.appt-cell.full {
  grid-column: 1 / -1;
}

.appt-no-line {
  margin-top: 14px;
  font-size: var(--fs-sm);
  color: var(--muted);
}

.apt-no {
  font-family: var(--font-mono);
  font-weight: 500;
  color: var(--fg);
}

.tips-box {
  margin-top: 12px;
  padding: 12px 14px;
  background: var(--accent-faint);
  border-radius: var(--radius-sm);
  text-align: left;
}

.tips-box .tl {
  font-size: 11px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 2px;
}

.tips-box .tt {
  font-size: var(--fs-xs);
  color: var(--muted);
  line-height: 1.5;
}

.btn-accent {
  display: block;
  width: 100%;
  padding: 12px;
  background: var(--accent);
  color: var(--surface);
  border-radius: var(--radius-sm);
  font-size: var(--fs-body);
  font-weight: 500;
  text-align: center;
  margin-top: 14px;
  transition: background 0.15s ease;
}

.btn-accent:hover {
  background: color-mix(in oklch, var(--accent) 85%, black);
}
</style>
