<template>
  <section class="confirm-card">
    <div class="confirm-title">确认预约信息</div>
    <div class="confirm-summary">
      <div class="confirm-row" v-if="store.selection.departmentName || summaryObj.departmentName">
        <span class="lbl">科室</span>
        <span class="val">{{ store.selection.departmentName || summaryObj.departmentName }}</span>
      </div>
      <div class="confirm-row" v-if="doctorDisplay">
        <span class="lbl">医生</span>
        <span class="val">{{ doctorDisplay }}</span>
      </div>
      <div class="confirm-row" v-if="store.selection.timePeriod || summaryObj.visitTime">
        <span class="lbl">时段</span>
        <span class="val">{{ store.selection.timePeriod || summaryObj.visitTime }}</span>
      </div>
      <div class="confirm-row" v-if="room">
        <span class="lbl">诊室</span>
        <span class="val">{{ room }}</span>
      </div>
      <div class="confirm-row">
        <span class="lbl">费用</span>
        <span class="val price">¥{{ amount }}</span>
      </div>
    </div>
    <div class="confirm-btns">
      <button class="btn-accent" :disabled="store.cardActionSubmitting" @click="$emit('action', { actionName: 'confirm_appointment', payload: {} })">确认挂号</button>
      <button class="btn-secondary" @click="$emit('action', { actionName: 'restart', payload: {} })">重新选择</button>
    </div>
    <p v-if="card.cardData.lockExpiresAt" class="lock-notice">号源锁定至 {{ card.cardData.lockExpiresAt }}，超时自动释放</p>
  </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<{ action: [data: { actionName: string; payload: Record<string, unknown> }] }>();

const store = useTerminalStore();

interface SummaryObj {
  departmentName?: string;
  doctorName?: string;
  visitTime?: string;
  room?: string;
  amount?: number;
}

const rawSummary = computed(() => props.card.cardData.summary);
const summaryObj = computed<SummaryObj>(() => {
  if (typeof rawSummary.value === 'object' && rawSummary.value !== null) {
    return rawSummary.value as SummaryObj;
  }
  return {};
});

const doctorDisplay = computed(() => {
  const name = store.selection.doctorName || summaryObj.value.doctorName;
  const title = store.selection.doctorTitle;
  return name ? (name + (title ? ` ${title}` : '')) : '';
});

const room = computed(() => store.selection.room || summaryObj.value.room || '');
const amount = computed(() => summaryObj.value.amount ?? props.card.cardData.amount ?? '25.00');
</script>

<style scoped>
.confirm-card {
  display: flex;
  flex-direction: column;
}

.confirm-title {
  font-size: var(--fs-body);
  font-weight: 600;
  margin-bottom: 14px;
}

.confirm-summary {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.confirm-row {
  display: flex;
  justify-content: space-between;
  padding: 12px 14px;
  font-size: var(--fs-sm);
  align-items: center;
}

.confirm-row + .confirm-row {
  border-top: 1px solid var(--border);
}

.confirm-row .lbl {
  color: var(--muted);
}

.confirm-row .val {
  font-weight: 500;
  text-align: right;
}

.confirm-row .val.price {
  font-family: var(--font-mono);
  color: var(--accent);
  font-weight: 600;
  font-size: var(--fs-body);
}

.confirm-btns {
  margin-top: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.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;
  transition: background 0.15s ease;
}

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

.btn-secondary {
  display: block;
  width: 100%;
  padding: 10px;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: var(--fs-sm);
  text-align: center;
  transition: background 0.15s ease;
}

.btn-secondary:hover {
  background: var(--bg);
}

.lock-notice {
  margin-top: 12px;
  font-size: var(--fs-xs);
  color: var(--warn-text);
  text-align: center;
}
</style>
