<template>
  <div class="home">
    <!-- 顶部标题 -->
    <div class="header-title">
      <div class="main-title">中国建科</div>
      <div class="sub-title">守正守望 致高致远</div>
    </div>
    
    <!-- 四个功能按钮 -->
    <div class="function-buttons">
      <button class="function-btn" @click="toInvitePeople">
        <img src="@/assets/1.png" alt="邀请人民" class="btn-icon" />
        <span style="font-weight: bold;">邀请人民</span>
      </button>
      <button class="function-btn" @click="toKefu">
        <img src="@/assets/2.png" alt="客服中心" class="btn-icon" />
        <span style="font-weight: bold;">客服中心</span>
      </button>
      <button class="function-btn" @click="toOfficialGroup">
        <img src="@/assets/3.png" alt="人民群聊" class="btn-icon" />
        <span style="font-weight: bold;">人民群聊</span>
      </button>
      <button class="function-btn" @click="toAppDownload">
        <img src="@/assets/4.png" alt="APP下载" class="btn-icon" />
        <span style="font-weight: bold;">APP下载</span>
      </button>
    </div>

    <!-- 轮播图 -->
    <div class="carousel">
      <van-swipe
        :autoplay="3000"
        indicator-color="#ed4b39"
        style="width: 100%; height: 200px; border-radius: 8px; overflow: hidden;"
      >
        <van-swipe-item v-for="(banner, index) in banners" :key="index">
          <img
            :src="banner"
            alt="轮播图"
            style="width: 100%; height: 200px; object-fit: cover;"
          />
        </van-swipe-item>
      </van-swipe>
    </div>
    
    <!-- 团队奖励部分 -->
    <div class="reward-section">
      <div class="reward-content">
        <span class="reward-label">团队奖励：</span>
        <span class="reward-item">一级20%</span>
        <span class="reward-item">二级15%</span>
        <span class="reward-item">三级10%</span>
        <span class="reward-more">更多</span>
      </div>
    </div>

    <!-- 新闻中心 -->
    <div class="news-title">新闻中心</div>
    <div class="news-list">
      <div
        class="news_content"
        v-for="(news, index) in processedNewsList"
        :key="index"
      >
        <template v-if="news.type === 'iframe'">
          <iframe :src="news.url" frameborder="0" class="news-iframe"></iframe>
        </template>
        <template v-else>
          <div v-html="news.content" @click="handleNewsClick($event)"></div>
        </template>
      </div>
    </div>

    <!-- <van-dialog
      v-model="showAuthDialog"
      :show-confirm-button="false"
      :show-cancel-button="false"
      :close-on-click-overlay="true"
      class="auth-dialog"
      v-if="userInfo.if_real === 0"
    >
      <div class="close-icon-wrapper">
        <van-icon name="cross" class="close-icon" @click="showAuthDialog = false" />
      </div>
      <div class="auth-content">
        <p class="auth-text">你还未进行实名认证</p>
        <button class="auth-btn" @click="toAuth">去认证</button>
      </div>
    </van-dialog> -->
    <van-dialog
      v-model="showDialog"
      :show-confirm-button="false"
      :show-cancel-button="false"
      :close-on-click-overlay="true"
      class="welcome-dialog"
      v-if="userInfo.gonggao"
    >
      <div class="close-icon-wrapper">
        <van-icon name="cross" class="close-icon" @click="showDialog = false" />
      </div>
      <div class="dialog-title">{{userInfo.gonggao.title}}</div>
      <div class="dialog-content" v-html="userInfo.gonggao.value"></div>
    </van-dialog>
  </div>
</template>

<script>
import { getBanners, getUserInfo, getNews } from '@/api/home';
import { Swipe, SwipeItem, Dialog, Icon } from 'vant';
import Vue from 'vue';

export default {
  name: "Home",
  data() {
    return {
      banners: [],
      userInfo: {
        gonggao: null,
        if_real: 0,
        link: []
      },
      newsList: [],
      showDialog: true,
      showAuthDialog: false,
    };
  },
  components: {
    [Swipe.name]: Swipe,
    [SwipeItem.name]: SwipeItem,
    [Icon.name]: Icon,
  },
  created() {
    Vue.use(Dialog);
  },
  mounted() {
    this.getBanners();
    this.getUserInfo();
    this.getNewsDetail()
  },
  computed: {
    processedNewsList() {
      return this.newsList.map(news => {
        // 尝试从content中提取URL
        const urlMatch = news.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: news.content
          };
        }
      });
    }
  },
  methods: {
    async getUserInfo() {
      const res = await getUserInfo();
      this.userInfo = res.data;
      if (this.userInfo.if_real === 0) {
        this.showAuthDialog = true;
      }
    },
    async getBanners() {
      const formData = new FormData();
      formData.append('cid', 5);
      const res = await getBanners(formData);
      this.banners = res.data
    },
    async getNewsDetail() {
      const res = await getNews();
      this.newsList = res.data;
    },
    toOfficialGroup() {
      window.open(this.userInfo.link[2].value, '_blank');
    },
    toAppDownload() {
      window.open(this.userInfo.link[1].value, '_blank');
    },
    toInvitePeople() {
      this.$router.push('/dynamic');
    },
    toAuth() {
      this.$router.push('/user-center');
      this.showAuthDialog = false;
    },
    handleNewsClick(event) {
      const link = event.target.closest('a');
      if (link) {
        event.preventDefault();
        if (link.href) {
          window.open(link.href, '_blank');
        }
      }
    },
    toKefu() {
      if (this.userInfo.link && this.userInfo.link[0] && this.userInfo.link[0].value) {
        window.open(this.userInfo.link[0].value, '_blank');
      }
    },
  }
};
</script>

<style lang="scss" scoped>
.home {
  min-height: 100vh;
  width: 100%;
  background: #b43a39;
  display: flex;
  flex-direction: column;
  padding: 18px 18px 80px;
  box-sizing: border-box;
  position: relative;
}

/* 顶部标题样式 */
.header-title {
  margin-bottom: 20px;
  
  .main-title {
    color: #fff;
    font-size: 24px;
    font-weight: bold;
    margin-bottom: 8px;
  }
  
  .sub-title {
    color: #fff;
    font-size: 18px;
    font-weight: bold;
    opacity: 0.9;
  }
}

/* 四个功能按钮样式 */
.function-buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  background: #fff;
  border-radius: 12px;
  padding: 0px 15px;
  margin-bottom: 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  
  .function-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    background: none;
    border: none;
    padding: 10px 5px;
    cursor: pointer;
    transition: all 0.2s;
    
    &:hover {
      background: #f5f5f5;
      border-radius: 8px;
    }
    
    .btn-icon {
      width: 32px;
      height: 32px;
      margin-bottom: 8px;
    }
    
    span {
      font-size: 12px;
      color: #333;
      text-align: center;
      white-space: nowrap;
    }
  }
}

/* 轮播图样式 */
.carousel {
  width: 100%;
  margin: 0 auto 16px auto;
  height: 200px;
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

/* 团队奖励样式 */
.reward-section {
  background: #fff;
  border-radius: 25px;
  padding: 12px 20px;
  margin-bottom: 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  .reward-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 12px;
    font-weight: bold;
    white-space: nowrap;
    .reward-label {
    color: #b53b38;
      font-weight: 500;
    }
    
    .reward-item {
    color: #b53b38;
      margin: 0 12px;
    }
    
    .reward-more {
    color: #b53b38;
      cursor: pointer;
      
      &:hover {
        color: #b43a39;
      }
    }
  }
}

/* 新闻中心标题 */
.news-title {
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 12px;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

/* 新闻列表样式 */
.news-list {
  width: 100%;
  padding-bottom: 40px;
  overflow-x: hidden;
  flex: 1;
  display: flex;
  flex-direction: column;
}

.news_content {
  width: 100%;
  min-height: 100px;
  border: none;
  margin: 16px auto 0;
  border-radius: 8px;
  background: #fff;
  padding: 15px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  box-sizing: border-box;
  
  &:last-child {
    margin-bottom: 20px;
  }

  .news-iframe {
    width: 100%;
    height: 500px;
    border: none;
    border-radius: 4px;
  }

  :deep(p) {
    margin: 0;
    padding: 0;
    display: flex;
    align-items: flex-start;
    gap: 15px;
    flex-wrap: wrap;
  }

  :deep(img) {
    width: 120px;
    height: 90px;
    object-fit: cover;
    border-radius: 4px;
    flex-shrink: 0;
  }

  :deep(span) {
    flex: 1;
    min-width: 200px;
    font-size: 14px;
    line-height: 1.6;
    color: #333;
  }

  :deep(div) {
    width: 100%;
    display: flex;
    align-items: flex-start;
    gap: 15px;
    margin-bottom: 10px;
  }

  :deep(*:not(img)) {
    max-width: 100%;
    word-break: break-word;
  }

  :deep(a) {
    color: #1890ff;
    text-decoration: none;
    cursor: pointer;
    
    &:hover {
      text-decoration: underline;
    }
  }
}

/* 弹窗样式 */
.close-icon-wrapper {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 1;
  
  .close-icon {
    font-size: 20px;
    color: #969799;
    cursor: pointer;
    padding: 4px;
    
    &:hover {
      color: #323233;
    }
  }
}

.dialog-title {
  text-align: center;
  font-size: 18px;
  font-weight: bold;
  padding: 16px 0;
  margin-top: 8px;
}

.welcome-dialog {
   width: 95% !important;
  :deep(.van-dialog__content) {
    padding: 20px;
    position: relative;
  }
}

.dialog-content {
  max-width: 90%;
  max-height: 70vh;
  overflow-y: auto;
  text-align: center;
  padding: 20px 0;
  margin: 0 auto;
  
  p {
    margin: 10px 0;
    font-size: 16px;
    color: #333;
  }
}

.auth-dialog {
  :deep(.van-dialog__content) {
    padding: 30px 20px;
    position: relative;
  }
}

.auth-content {
  text-align: center;
  height: 120px;
  
  .auth-text {
    font-size: 16px;
    color: #333;
    margin-bottom: 20px;
    margin-top:12px;
  }
  
  .auth-btn {
    width: 83%;
    height: 36px;
    background: #ed4b39;
    color: #fff;
    border: none;
    border-radius: 18px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s;
    margin-top:20px;
    
    &:hover {
      background: #c9301c;
    }
  }
}
</style>
