<template>
  <div class="tos-container">
    <el-card>
      <div class="tos-content" v-html="tosContent"></div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import DOMPurify from 'dompurify';
import { getTermsServiceListMax } from '@/api/system/business/ofService';

const tosContent = ref('');
const language = ref('zh-CN');

onMounted(() => {
  fetchTos();
});

const fetchTos = async () => {
  try {
    const res = await getTermsServiceListMax();
    // 处理获取到的内容，确保格式良好
    const formattedContent = handleContentFormatting(res.data.content || '<p>暂无服务条款内容</p>');
    tosContent.value = DOMPurify.sanitize(formattedContent);
  } catch (error) {
    console.error('获取服务条款失败', error);
    tosContent.value = '<p>服务条款内容加载失败</p>';
  }
};

// 完善内容格式化处理
const handleContentFormatting = (content: string): string => {
  if (!content) return '<p>暂无内容</p>';

  // 移除多余的空白字符，但保留必要的换行
  let formattedContent = content
    .replace(/\r\n/g, '\n')
    .replace(/\r/g, '\n')
    .replace(/\n\s*\n/g, '\n\n') // 合并多个空行为两个换行
    .trim();

  // 如果内容已经是HTML格式（包含标签），则不进行额外处理
  if (/<[^>]+>/.test(formattedContent)) {
    return formattedContent;
  }

  // 如果是纯文本，转换为基本的HTML格式
  // 将连续的换行转换为段落
  formattedContent = formattedContent
    .split('\n\n')
    .map((paragraph) => `<p>${paragraph.replace(/\n/g, '<br>')}</p>`)
    .join('');

  return formattedContent;
};
</script>

<style scoped>
.tos-container {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.tos-content {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 14px;
  line-height: 1.6;
  color: #333;
  word-break: break-word;
}

/* 标题样式 */
.tos-content h1 {
  font-size: 24px;
  font-weight: bold;
  margin: 24px 0 16px 0;
  color: #2c3e50;
  text-align: center;
}

.tos-content h2 {
  font-size: 20px;
  font-weight: bold;
  margin: 20px 0 12px 0;
  color: #34495e;
  border-bottom: 1px solid #eee;
  padding-bottom: 8px;
}

.tos-content h3 {
  font-size: 18px;
  font-weight: bold;
  margin: 16px 0 10px 0;
  color: #555;
}

/* 段落样式 */
.tos-content p {
  margin: 12px 0;
  line-height: 1.7;
}

/* 列表样式 */
.tos-content ul,
.tos-content ol {
  margin: 12px 0;
  padding-left: 24px;
}

.tos-content li {
  margin: 8px 0;
  line-height: 1.6;
}

.tos-content ul li {
  list-style-type: disc;
}

.tos-content ol li {
  list-style-type: decimal;
}

/* 强调文本 */
.tos-content strong {
  font-weight: bold;
  color: #2c3e50;
}

.tos-content em {
  font-style: italic;
}

/* 链接样式 */
.tos-content a {
  color: #3498db;
  text-decoration: none;
}

.tos-content a:hover {
  text-decoration: underline;
}

/* 代码样式 */
.tos-content code {
  background-color: #f8f9fa;
  padding: 2px 4px;
  border-radius: 3px;
  font-family: 'Courier New', monospace;
  font-size: 13px;
}

/* 引用样式 */
.tos-content blockquote {
  border-left: 4px solid #ddd;
  margin: 16px 0;
  padding: 8px 16px;
  background-color: #f9f9f9;
  color: #666;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .tos-container {
    padding: 15px;
  }

  .tos-content {
    font-size: 13px;
  }

  .tos-content h1 {
    font-size: 20px;
    margin: 20px 0 14px 0;
  }

  .tos-content h2 {
    font-size: 18px;
    margin: 18px 0 10px 0;
  }

  .tos-content h3 {
    font-size: 16px;
    margin: 14px 0 8px 0;
  }

  .el-card {
    width: 100%;
    margin: 0 auto;
  }
}

/* 处理可能的空白内容 */
.tos-content:empty::before {
  content: '暂无内容';
  color: #999;
  font-style: italic;
}
</style>
