<template>
  <ScreenLayout :show-back-btn="true" back-text="返回" back-target="/idle">
    <div class="page-recognition">
      <div class="recognition-content">
        <!-- 结果图标 -->
        <div class="result-icon" :class="resultClass">
          <svg v-if="isMatched" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14" />
            <polyline points="22 4 12 14.01 9 11.01" />
          </svg>
          <svg v-else viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <circle cx="12" cy="12" r="10" />
            <line x1="12" y1="8" x2="12" y2="12" />
            <line x1="12" y1="16" x2="12.01" y2="16" />
          </svg>
        </div>

        <!-- 结果标题 -->
        <h1 class="result-title">{{ resultTitle }}</h1>
        <p class="result-subtitle">{{ resultSubtitle }}</p>

        <!-- 详细信息 -->
        <div v-if="isMatched && result" class="result-details">
          <div class="detail-row">
            <span class="detail-label">姓名</span>
            <span class="detail-value">{{ result.visitorName }}</span>
          </div>
          <div class="detail-row">
            <span class="detail-label">预约对象</span>
            <span class="detail-value">{{ result.visitedPerson }}</span>
          </div>
          <div class="detail-row">
            <span class="detail-label">预约时间</span>
            <span class="detail-value">{{ result.appointmentTime }}</span>
          </div>
        </div>

        <!-- 操作按钮 -->
        <div class="result-actions">
          <button v-if="isMatched" class="btn-primary" @click="handleConfirm">
            确认登记
          </button>
          <button v-else class="btn-secondary" @click="goToVisitor">
            前往登记
          </button>
        </div>
      </div>
    </div>
  </ScreenLayout>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useVisitorStore } from '@/stores/visitor'
import { useScreenStore } from '@/stores/screen'
import * as api from '@/api/screen'
import ScreenLayout from '@/layouts/ScreenLayout.vue'

const router = useRouter()
const visitorStore = useVisitorStore()
const screenStore = useScreenStore()

const result = ref(null)
const loading = ref(true)

const isMatched = computed(() => {
  return result.value && result.value.matchStatus === 'matched'
})

const resultClass = computed(() => {
  if (isMatched.value) return 'success'
  return 'warning'
})

const resultTitle = computed(() => {
  if (!result.value) return '识别中...'
  if (isMatched.value) return '身份已确认'
  return '未匹配到预约'
})

const resultSubtitle = computed(() => {
  if (!result.value) return '正在验证身份信息'
  if (isMatched.value) return '欢迎您的到来'
  return '请选择其他方式进行登记'
})

const handleConfirm = () => {
  if (result.value) {
    visitorStore.appointmentInfo = {
      appointmentNo: result.value.appointmentNo,
      visitorName: result.value.visitorName,
      visitedPerson: result.value.visitedPerson,
      appointmentTime: result.value.appointmentTime
    }
  }
  router.push('/visitor/appointment-confirm')
}

const goToVisitor = () => {
  router.push('/visitor')
}

onMounted(async () => {
  try {
    result.value = await api.getRecognitionResult()
  } catch {
    screenStore.showAlert({
      type: 'error',
      message: '获取识别结果失败'
    })
  } finally {
    loading.value = false
  }
})
</script>

<style scoped>
.page-recognition {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #e9f8ff 0%, #dff8f5 50%, #f7fbff 100%);
}

.recognition-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  text-align: center;
  padding: 40px;
  animation: fadeInUp 0.6s ease-out;
}

.result-icon {
  width: 100px;
  height: 100px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-card);
  border-radius: 50%;
  box-shadow: var(--shadow-lg);
}

.result-icon svg {
  width: 60px;
  height: 60px;
}

.result-icon.success {
  color: var(--success);
  background: var(--success-soft);
}

.result-icon.warning {
  color: var(--warning);
  background: var(--warning-soft);
}

.result-title {
  font-size: 36px;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}

.result-subtitle {
  font-size: 20px;
  color: var(--text-secondary);
  margin: 0;
}

.result-details {
  background: var(--bg-card);
  border-radius: var(--radius-xl);
  padding: 24px 40px;
  box-shadow: var(--shadow-md);
  margin: 16px 0;
  min-width: 320px;
}

.detail-row {
  display: flex;
  justify-content: space-between;
  gap: 40px;
  padding: 12px 0;
}

.detail-row:not(:last-child) {
  border-bottom: 1px solid var(--border-light);
}

.detail-label {
  font-size: 16px;
  color: var(--text-muted);
}

.detail-value {
  font-size: 18px;
  font-weight: 500;
  color: var(--text-primary);
}

.result-actions {
  margin-top: 16px;
}

.btn-primary,
.btn-secondary {
  min-width: 240px;
  height: 64px;
  padding: 0 48px;
  font-size: 22px;
  font-weight: 500;
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.btn-primary {
  background: var(--primary);
  color: white;
  border: none;
  box-shadow: var(--shadow-md);
}

.btn-primary:hover {
  background: var(--primary-dark);
  transform: translateY(-2px);
  box-shadow: var(--shadow-lg);
}

.btn-secondary {
  background: var(--bg-card);
  color: var(--primary);
  border: 2px solid var(--primary);
}

.btn-secondary:hover {
  background: var(--primary);
  color: white;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
