<template>
  <div class="profile-detail">
    <div class="header">
      <div class="back" @click="goBack">
        <img src="@/assets/back.png" alt="">
      </div>
      <div class="title">央企文化</div>
    </div>
    <div class="content">
      <!-- <div class="title">{{newsDetail.title}}</div> -->
      <template v-if="processedContent.type === 'iframe'">
        <iframe :src="processedContent.url" frameborder="0" class="content-iframe"></iframe>
      </template>
      <template v-else>
        <div class="content" v-html="newsDetail.content"></div>
      </template>
    </div>
  </div>
</template>

<script>
import { getNewsDetail } from '@/api/home';
export default {
  name: 'ProfileDetail',
  data() {
    return {
      newsDetail: {}
    }
  },
  computed: {
    processedContent() {
      if (!this.newsDetail.content) return { type: 'content', content: '' };
      
      // 尝试从content中提取URL
      const urlMatch = this.newsDetail.content.match(/https?:\/\/[^\s<>"']+/g);
      
      if (urlMatch && (urlMatch[0].includes('.html') || urlMatch[0].includes('.com'))) {
        // 如果找到URL并且是HTML页面或.com网站，使用iframe展示
        return {
          type: 'iframe',
          url: urlMatch[0]
        };
      } else {
        // 否则保持原有的content展示方式
        return {
          type: 'content',
          content: this.newsDetail.content
        };
      }
    }
  },
  methods: {
    goBack() {
      this.$router.go(-1)
    },
    async getNewsDetail() {
      const formData = new FormData();
      formData.append('id', 8)
      const res = await getNewsDetail(formData)
      this.newsDetail = res.data
    }
  },
  mounted() {
    this.getNewsDetail()
  }
}
</script>

<style lang="scss" scoped>
.profile-detail {
  min-height: 100vh;
  background-color: #f5f5f5;

  .header {
    position: relative;
    height: 44px;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    border-bottom: 1px solid #eee;

    .back {
      position: absolute;
      left: 15px;
      font-size: 20px;
      img {
        width: 20px;
        height: 20px;
      }
    }

    .title {
      font-size: 18px;
      font-weight: 500;
    }
  }

  .content {
    padding: 10px;
    .title {
      margin-bottom: 10px;
      text-align: center;
      font-size: 18px;
      font-weight: 500;
    }
    .content {
      font-size: 16px;
      line-height: 24px;
    }
    .content-iframe {
      width: 100%;
      height: calc(100vh - 120px); // 减去header和padding的高度
      border: none;
      border-radius: 4px;
      background: #fff;
    }
  }
}
</style> 