<template>
  <div class="ocr-image-wrapper" :class="[statusClass, { 'is-loading': isLoading, 'is-success': isSuccess, 'is-failed': isFailed }]">
    <img :src="src" :alt="alt" loading="lazy" />

    <!-- Loading overlay -->
    <div v-if="isLoading" class="overlay loading-overlay">
      <svg class="spinner-svg" viewBox="0 0 40 40">
        <circle class="spinner-track" cx="20" cy="20" r="16" />
        <circle class="spinner-arc" cx="20" cy="20" r="16" />
      </svg>
      <span class="overlay-label">OCR 识别中...</span>
    </div>

    <!-- Success overlay -->
    <div v-if="isSuccess" class="overlay success-overlay">
      <div class="success-badge">
        <svg viewBox="0 0 24 24" class="checkmark-svg">
          <path d="M9 16.2l-3.5-3.5L4 14.2l5 5 10-10-1.8-1.8z" fill="none" stroke="#fff" stroke-width="2" />
        </svg>
      </div>
      <div class="text-preview" v-if="ocrText">
        <span class="preview-label">{{ ocrText }}</span>
      </div>
    </div>

    <!-- Failed overlay -->
    <div v-if="isFailed" class="overlay failed-overlay">
      <div class="error-message">{{ errorMsg || 'OCR 识别失败' }}</div>
      <button class="retry-button" @click="$emit('retry')">
        🔄 重试
      </button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

const props = withDefaults(defineProps<{
  src: string
  alt?: string
  ocrStatus?: 'loading' | 'success' | 'failed'
  ocrText?: string
  errorMsg?: string
}>(), {
  ocrStatus: 'loading',
  ocrText: '',
  errorMsg: ''
})

defineEmits<{
  retry: []
}>()

const isLoading = computed(() => props.ocrStatus === 'loading')
const isSuccess = computed(() => props.ocrStatus === 'success')
const isFailed = computed(() => props.ocrStatus === 'failed')
const statusClass = computed(() => `status-${props.ocrStatus}`)
</script>

<style scoped>
.ocr-image-wrapper {
  position: relative;
  display: inline-block;
  border-radius: 8px;
  overflow: hidden;
  transition: filter 0.3s ease, border-color 0.3s ease;
}

.ocr-image-wrapper img {
  display: block;
  max-width: 100%;
  height: auto;
}

.is-loading img {
  filter: grayscale(100%);
}

.is-failed {
  border: 2px solid #ef4444;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(2px);
  transition: opacity 0.3s ease;
}

.success-overlay {
  background: rgba(0, 0, 0, 0.15);
}

/* Spinner */
.spinner-svg {
  width: 32px;
  height: 32px;
  animation: spinner-rotate 1s linear infinite;
}

.spinner-track {
  fill: none;
  stroke: rgba(255, 255, 255, 0.3);
  stroke-width: 3;
}

.spinner-arc {
  fill: none;
  stroke: #fff;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-dasharray: 60% 72%;
  animation: spinner-dash 1.5s ease-in-out infinite;
}

@keyframes spinner-rotate {
  100% { transform: rotate(360deg); }
}

@keyframes spinner-dash {
  0%   { stroke-dasharray: 1% 72%;    stroke-dashoffset: 0; }
  50%  { stroke-dasharray: 60% 72%;   stroke-dashoffset: -30%; }
  100% { stroke-dasharray: 60% 72%;   stroke-dashoffset: -120%; }
}

.overlay-label {
  color: #fff;
  font-size: 13px;
  margin-top: 4px;
}

/* Success badge */
.success-badge {
  position: absolute;
  bottom: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
  background: #22c55e;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.checkmark-svg {
  width: 16px;
  height: 16px;
}

.text-preview {
  margin-top: 8px;
  max-width: 90%;
}

.preview-label {
  color: rgba(255, 255, 255, 0.8);
  font-size: 12px;
}

/* Failed state */
.error-message {
  color: #fca5a5;
  font-size: 13px;
  text-align: center;
}

.retry-button {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 14px;
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 6px;
  color: #fff;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.2s ease, transform 0.1s ease;
}

.retry-button:hover {
  background: rgba(255, 255, 255, 0.25);
}

.retry-button:active {
  transform: scale(0.96);
}
</style>
