<template>
  <ScreenLayout :show-back-btn="true" back-text="返回" back-target="/visitor/appointment">
    <div class="page-confirm">
      <h1 class="page-title">预约信息确认</h1>

      <div v-if="appointment" class="info-card">
        <div class="info-row">
          <span class="info-label">访客姓名</span>
          <span class="info-value">{{ appointment.visitorName }}</span>
        </div>
        <div class="info-row">
          <span class="info-label">手机号码</span>
          <span class="info-value">{{ maskMobile(appointment.mobile) }}</span>
        </div>
        <div class="info-row">
          <span class="info-label">身份证号</span>
          <span class="info-value">{{ maskIdCard(appointment.idCardNo) }}</span>
        </div>
        <div class="info-row">
          <span class="info-label">被访人</span>
          <span class="info-value">{{ appointment.visitedPerson }}</span>
        </div>
        <div class="info-row">
          <span class="info-label">被访部门</span>
          <span class="info-value">{{ appointment.visitedDepartment }}</span>
        </div>
        <div class="info-row">
          <span class="info-label">预约时间</span>
          <span class="info-value">{{ appointment.appointmentTime }}</span>
        </div>
        <div class="info-row">
          <span class="info-label">来访事由</span>
          <span class="info-value">{{ appointment.visitPurpose }}</span>
        </div>
      </div>

      <div class="confirm-actions">
        <button class="btn-back" @click="goBack">信息有误</button>
        <button class="btn-confirm" @click="handleConfirm" :disabled="loading">
          <span v-if="loading" class="loading-spinner"></span>
          <span v-else>确认登记</span>
        </button>
      </div>
    </div>
  </ScreenLayout>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useScreenStore } from '@/stores/screen'
import { useVisitorStore } from '@/stores/visitor'
import { maskMobile, maskIdCard } from '@/utils/device'
import ScreenLayout from '@/layouts/ScreenLayout.vue'

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

const loading = ref(false)

const appointment = visitorStore.appointmentInfo

const goBack = () => {
  router.push('/visitor/walk-in')
}

const handleConfirm = async () => {
  if (loading.value) return
  loading.value = true

  try {
    await visitorStore.submitRegistration()
    router.push('/visitor/success')
  } catch (error) {
    screenStore.showAlert({
      type: 'error',
      message: '登记失败，请重试'
    })
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.page-confirm {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
}

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

.info-card {
  width: 100%;
  max-width: 500px;
  background: var(--bg-card);
  border-radius: var(--radius-xl);
  box-shadow: var(--shadow-md);
  padding: 24px;
  margin-bottom: 32px;
}

.info-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0;
  border-bottom: 1px solid var(--border-light);
}

.info-row:last-child {
  border-bottom: none;
}

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

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

.confirm-actions {
  display: grid;
  grid-template-columns: 1fr 2fr;
  gap: 20px;
  width: 100%;
  max-width: 500px;
}

.btn-back,
.btn-confirm {
  height: 64px;
  font-size: 20px;
  font-weight: 500;
  border-radius: var(--radius-lg);
  cursor: pointer;
  transition: all var(--transition-fast);
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.btn-back:hover {
  border-color: var(--text-muted);
}

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

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

.btn-confirm:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.loading-spinner {
  width: 24px;
  height: 24px;
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
</style>
