<template>
  <div class="image-test-container">
    <h1>图片URL测试</h1>
    
    <div class="test-section">
      <h2>测试图片URL</h2>
      <div class="input-section">
        <el-input v-model="imageUrl" placeholder="输入图片URL" clearable></el-input>
        <el-button type="primary" @click="processUrl">处理URL</el-button>
      </div>
      
      <div class="result-section">
        <h3>处理结果</h3>
        <div class="url-display">
          <p><strong>原始URL:</strong> {{ imageUrl }}</p>
          <p><strong>处理后URL:</strong> {{ processedUrl }}</p>
        </div>
        
        <div class="image-display">
          <h3>图片预览</h3>
          <div v-if="processedUrl" class="image-container">
            <img :src="processedUrl" alt="测试图片" @error="handleImageError" />
            <p v-if="imageError" class="error-message">图片加载失败: {{ imageError }}</p>
          </div>
          <p v-else>请输入图片URL并点击处理</p>
        </div>
      </div>
    </div>
    
    <div class="test-section">
      <h2>测试示例</h2>
      <div class="examples">
        <div v-for="(example, index) in examples" :key="index" class="example-item">
          <p>{{ example.description }}</p>
          <el-button @click="testExample(example.url)">测试 {{ example.url }}</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { processImageUrl } from '../utils/imageUtils'
import { ElMessage } from 'element-plus'

const imageUrl = ref('')
const processedUrl = ref('')
const imageError = ref('')

// 示例URL
const examples = [
  { 
    description: '相对路径 - 不带前导斜杠', 
    url: 'upload/images/569f8ce5-09d2-4151-a6b1-f56a4d0b2c84.png' 
  },
  { 
    description: '相对路径 - 带前导斜杠', 
    url: '/upload/images/362d1d68-fcc2-4a74-a0f1-44bd38e9cb6d.jpg' 
  },
  { 
    description: '完整URL - localhost:8080', 
    url: 'http://localhost:8080/upload/images/3efa486a-610d-4745-a54c-61f55ab3f1d5.jpg' 
  },
  { 
    description: '完整URL - localhost:8085', 
    url: 'http://localhost:8085/upload/images/6c8c8d04-e583-4f12-8c56-d618b2bb06f4.jpg' 
  },
  { 
    description: '外部URL - 占位图片', 
    url: 'https://via.placeholder.com/150?text=测试图片' 
  }
]

// 处理URL
const processUrl = () => {
  if (!imageUrl.value) {
    ElMessage.warning('请输入图片URL')
    return
  }
  
  imageError.value = ''
  processedUrl.value = processImageUrl(imageUrl.value)
}

// 测试示例URL
const testExample = (url) => {
  imageUrl.value = url
  processUrl()
}

// 处理图片加载错误
const handleImageError = (e) => {
  imageError.value = `无法加载图片 (${e.type})`
  console.error('图片加载失败:', e)
}
</script>

<style scoped>
.image-test-container {
  padding: 20px 20px 80px 20px;
  min-height: calc(100vh - 140px);
  background-color: transparent;
  border-radius: 15px;
}

.test-section {
  margin-bottom: 30px;
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.85);
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.test-section:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

h1, h2, h3 {
  color: #ff6b81;
}

.input-section {
  display: flex;
  gap: 15px;
  margin-bottom: 20px;
}

.input-section .el-button {
  background-color: #ffb6c1;
  border: none;
  border-radius: 20px;
  color: white;
  transition: background-color 0.3s;
}

.input-section .el-button:hover {
  background-color: #ff8a9b;
}

.result-section {
  margin-top: 25px;
}

.url-display {
  background-color: #fff9f9;
  padding: 15px;
  border-radius: 8px;
  border: none;
  margin-bottom: 20px;
  word-break: break-all;
  box-shadow: 0 2px 5px rgba(255, 107, 129, 0.1);
}

.image-container {
  margin-top: 15px;
  padding: 15px;
  border-radius: 8px;
  background-color: #fff9f9;
  box-shadow: 0 2px 5px rgba(255, 107, 129, 0.1);
}

.image-container img {
  max-width: 100%;
  max-height: 300px;
  display: block;
  margin: 0 auto;
  border-radius: 8px;
}

.error-message {
  color: #f56c6c;
  margin-top: 10px;
}

.examples {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.example-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  background-color: #fff9f9;
  border-radius: 8px;
  border: none;
  box-shadow: 0 2px 5px rgba(255, 107, 129, 0.1);
  transition: all 0.3s ease;
}

.example-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 8px rgba(255, 107, 129, 0.15);
}

.example-item .el-button {
  background-color: #ffb6c1;
  border: none;
  border-radius: 20px;
  color: white;
  transition: all 0.3s;
}

.example-item .el-button:hover {
  background-color: #ff8a9b;
  transform: scale(1.05);
}
</style>
