<template>
  <div class="success-layout">
    <StatusBar :title="robotName" />

    <div class="layout-main">
      <div class="success-page">
        <!-- 动态背景层 -->
        <div class="bg-layer">
          <div class="bg-orb orb-1"></div>
          <div class="bg-orb orb-2"></div>
          <div class="bg-orb orb-3"></div>
          <div class="bg-grid-overlay"></div>
        </div>

        <!-- 内容区 -->
        <div class="success-content">
          <!-- 成功图标 -->
          <div class="success-icon-wrap">
            <svg class="success-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
              <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14" />
              <polyline points="22 4 12 14.01 9 11.01" />
            </svg>
          </div>

          <!-- 标题 -->
          <h1 class="success-title">登记成功</h1>
          <p class="success-subtitle">欢迎您的到来</p>

          <!-- 信息卡片 -->
          <div class="info-card">
            <div class="info-row">
              <span class="info-label">访客姓名</span>
              <span class="info-value">{{ visitorName }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">手机号码</span>
              <span class="info-value">{{ maskedMobile }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">被访人</span>
              <span class="info-value">{{ visitedPerson }}</span>
            </div>
            <div class="info-row">
              <span class="info-label">登记时间</span>
              <span class="info-value">{{ registerTime }}</span>
            </div>
          </div>

          <!-- 温馨提示 -->
          <div class="hint-text">
            请前往 {{ visitedPerson || '被访人' }} 处办理来访事项
          </div>

          <!-- 自动返回提示 -->
          <div v-if="!isDev && showCountdown" class="auto-hint">
            {{ countdown }} 秒后自动返回待机
          </div>

          <!-- 底部按钮 -->
          <button class="btn-return" @click="goToIdle">
            返回待机
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { useVisitorStore } from '@/stores/visitor'
import { useScreenStore } from '@/stores/screen'
import { formatDateTime } from '@/utils/time'
import StatusBar from '@/components/StatusBar.vue'

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

const robotName = computed(() => screenStore.robotName || '智能迎宾机器人')
const isDev = import.meta.env.DEV

// 开发环境 Mock 数据
const mockInfo = {
  visitorName: '张三',
  mobile: '13800000000',
  visitedPerson: '李经理',
  registerTime: formatDateTime(),
  registerType: 'walk_in'
}

// 获取登记信息（优先真实数据，DEV 环境兜底 Mock）
const info = computed(() => {
  const real = visitorStore.registrationInfo
  if (real?.visitorName) return real
  if (isDev) return mockInfo
  return { visitorName: '访客', mobile: '', visitedPerson: '--', registerTime: formatDateTime() }
})

const visitorName = computed(() => info.value.visitorName || '访客')
const maskedMobile = computed(() => {
  const mobile = info.value.mobile || ''
  if (!mobile) return '--'
  if (mobile.length === 11) {
    return `${mobile.slice(0, 3)}****${mobile.slice(7)}`
  }
  return mobile
})
const visitedPerson = computed(() => info.value.visitedPerson || '--')
const registerTime = computed(() => info.value.registerTime || info.value.registeredTime || formatDateTime())

// 自动返回计时
const countdown = ref(10)
const showCountdown = ref(false)
let countdownTimer = null
let autoReturnTimer = null

const goToIdle = () => {
  visitorStore.clearVisitorData()
  router.push('/idle')
}

const startCountdown = () => {
  showCountdown.value = true
  countdown.value = 10
  countdownTimer = setInterval(() => {
    countdown.value--
    if (countdown.value <= 0) {
      clearInterval(countdownTimer)
    }
  }, 1000)
}

onMounted(() => {
  // PROD 环境：10 秒后自动返回待机
  if (!isDev) {
    startCountdown()
    autoReturnTimer = setTimeout(() => {
      goToIdle()
    }, 10000)
  }
})

onUnmounted(() => {
  if (countdownTimer) clearInterval(countdownTimer)
  if (autoReturnTimer) clearTimeout(autoReturnTimer)
})
</script>

<style scoped>
/* ===== 整体布局 ===== */
.success-layout {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.layout-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  position: relative;
}

/* ===== 页面容器 ===== */
.success-page {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 0 32px 28px;
  position: relative;
  z-index: 1;
  overflow-y: auto;
}

/* ===== 动态背景层 ===== */
.bg-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 0;
}

.bg-orb {
  position: absolute;
  border-radius: 50%;
  filter: blur(60px);
  opacity: 0.5;
}

.orb-1 {
  width: 400px;
  height: 400px;
  background: radial-gradient(circle, rgba(59, 130, 246, 0.35) 0%, transparent 70%);
  top: -100px;
  right: -80px;
  animation: float 8s ease-in-out infinite;
}

.orb-2 {
  width: 350px;
  height: 350px;
  background: radial-gradient(circle, rgba(16, 185, 129, 0.28) 0%, transparent 70%);
  bottom: -60px;
  left: -60px;
  animation: float 10s ease-in-out infinite reverse;
}

.orb-3 {
  width: 280px;
  height: 280px;
  background: radial-gradient(circle, rgba(99, 102, 241, 0.22) 0%, transparent 70%);
  top: 40%;
  left: 30%;
  animation: float 12s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0) scale(1); }
  50% { transform: translateY(-20px) scale(1.05); }
}

.bg-grid-overlay {
  position: absolute;
  inset: 0;
  background-image:
    linear-gradient(rgba(59, 130, 246, 0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(59, 130, 246, 0.04) 1px, transparent 1px);
  background-size: 48px 48px;
}

/* ===== 内容区 ===== */
.success-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 680px;
  gap: 20px;
  animation: fadeInUp 0.5s ease-out;
}

@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(24px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== 成功图标 ===== */
.success-icon-wrap {
  width: 130px;
  height: 130px;
  background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 16px 40px rgba(16, 185, 129, 0.30);
  animation: scaleIn 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275) 0.1s backwards;
}

@keyframes scaleIn {
  from { opacity: 0; transform: scale(0.4); }
  to { opacity: 1; transform: scale(1); }
}

.success-icon {
  width: 70px;
  height: 70px;
  color: white;
  stroke-width: 2.5;
}

/* ===== 标题 ===== */
.success-title {
  font-size: 56px;
  font-weight: 900;
  color: var(--text-primary);
  margin: 0;
  letter-spacing: 3px;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.success-subtitle {
  font-size: 26px;
  color: var(--text-secondary);
  margin: 0;
  font-weight: 500;
  letter-spacing: 1px;
}

/* ===== 信息卡片 ===== */
.info-card {
  width: 100%;
  background: rgba(255, 255, 255, 0.90);
  border-radius: 28px;
  padding: 8px 0;
  box-shadow:
    0 12px 40px rgba(30, 64, 175, 0.10),
    0 4px 12px rgba(0, 0, 0, 0.06);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.6);
  margin-top: 8px;
}

.info-row {
  display: flex;
  align-items: center;
  padding: 18px 36px;
  gap: 24px;
}

.info-row + .info-row {
  border-top: 1px solid rgba(0, 0, 0, 0.05);
}

.info-label {
  font-size: 24px;
  color: var(--text-muted);
  font-weight: 600;
  flex-shrink: 0;
  min-width: 100px;
}

.info-value {
  font-size: 26px;
  color: var(--text-primary);
  font-weight: 700;
  text-align: right;
  flex: 1;
  word-break: break-all;
}

/* ===== 温馨提示 ===== */
.hint-text {
  font-size: 22px;
  color: var(--text-secondary);
  font-weight: 500;
  text-align: center;
  margin-top: 4px;
}

/* ===== 自动返回提示 ===== */
.auto-hint {
  font-size: 18px;
  color: var(--text-muted);
  text-align: center;
  margin-top: -8px;
}

/* ===== 返回按钮 ===== */
.btn-return {
  width: 380px;
  height: 80px;
  font-size: 28px;
  font-weight: 800;
  letter-spacing: 2px;
  background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  color: white;
  border: none;
  border-radius: 999px;
  cursor: pointer;
  transition: all 0.22s ease;
  box-shadow: 0 12px 28px rgba(37, 99, 235, 0.30);
  margin-top: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-return:hover {
  box-shadow: 0 16px 36px rgba(37, 99, 235, 0.38);
  transform: translateY(-2px);
}

.btn-return:active {
  transform: scale(0.97);
}
</style>
