<template>
    <div class="modal-container">
      <!-- 加载状态 -->
      <div v-if="loading" class="loading-container">
        <el-icon class="loading-icon"><Loading /></el-icon>
        <span>正在加载验证码...</span>
      </div>
  
      <!-- 验证码显示 -->
      <div v-else class="code-container">
        <div class="verification-code">
          {{ verificationCode }}
        </div>
        <p class="code-tip">请使用该验证码在VR设备中查看视图（有效时间5分钟）</p>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, onMounted } from 'vue'
  import { postData, getData } from '@/api/axios.js'
  import { ElMessage } from 'element-plus'
  import { Loading } from '@element-plus/icons-vue'
  
  const props = defineProps({
    result: {
      type: Number,
      required: true,
    }
  })
  
  const loading = ref(true)
  const verificationCode = ref('') // 存储6位验证码
  
  onMounted(() => {
    getData('/generateGraph', { method: 'VR', result: props.result })
      .then(response => {
        // 提取并格式化验证码
        verificationCode.value = response.data.token.match(/\d{6}/)?.[0] || ''
        loading.value = false
      })
      .catch(error => {
        ElMessage.error("验证码获取失败")
        console.error(error)
        loading.value = false
      })
  })
  </script>
  
  <style scoped>
  .modal-container {
    min-height: 200px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  
  .loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
  }
  
  .loading-icon {
    font-size: 32px;
    animation: rotating 2s linear infinite;
  }
  
  @keyframes rotating {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
  }
  
  .code-container {
    text-align: center;
  }
  
  .verification-code {
    font-family: 'Courier New', monospace;
    font-size: 42px;
    font-weight: 700;
    letter-spacing: 8px;
    color: #409EFF;
    background: #f5f7fa;
    padding: 20px 40px;
    border-radius: 8px;
    display: inline-block;
    box-shadow: 0 2px 12px rgba(0,0,0,0.1);
  }
  
  .code-tip {
    color: #909399;
    margin-top: 16px;
    font-size: 14px;
  }
  </style>