<template>
  <div class="home">
    <div class="top-btns">
      <div class="row">
        <button class="btn" @click="toInvitePeople">
          <img src="@/assets/1.png" alt="邀请百姓" class="btn-icon" />
          <span>邀请百姓</span>
        </button>
        <button class="btn" @click="toOfficialGroup">
          <img src="@/assets/2.png" alt="官方群聊" class="btn-icon" />
          <span>官方群聊</span>
        </button>
      </div>
      <div class="kefu" @click="toKefu">
        <img src="@/assets/kefu.jpg" alt="客服" />
      </div>
      <div class="row" >
        <button class="btn" @click="toAppDownload" style="padding:0 6px;">
          <img src="@/assets/3.png" alt="APP下载" class="btn-icon" style="width: 22px; height: 22px;" />
          <span>APP下载</span>
        </button>
        <button class="btn" @click="toCultureDetail">
          <img src="@/assets/4.png" alt="央企文化" class="btn-icon" />
          <span>央企文化</span>
        </button>
      </div>
    </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-table">
        <div class="reward-row header">
          <span>团队奖励</span>
          <span>一级</span>
          <span>二级</span>
          <span>三级</span>
        </div>
        <div class="reward-row value">
          <span></span>
          <span>20%</span>
          <span>10%</span>
          <span>5%</span>
        </div>
      </div>
      <button 
        class="party-btn" 
        :class="{ 'is-member': userInfo.isdangyuan === 1 }"
        @click="userInfo.isdangyuan === 0 && toApply()"
      >
        {{ userInfo.isdangyuan === 1 ? '已是党员' : '党员申请' }}
      </button>
    </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');
    },
    toCultureDetail() {
      this.$router.push('/culture/detail');
    },
    toInvitePeople() {
      this.$router.push('/dynamic');
    },
    toApply(){
      this.$router.push('/Apply');
    },
    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: url('~@/assets/dabag.png') no-repeat;
  background-size: cover;
  background-attachment: fixed;
  display: flex;
  flex-direction: column;
  padding:100px 18px 80px;
  box-sizing: border-box;
  position: relative;
}

.top-btns {
  width: 100%;
  display: flex;
  justify-content: space-between;
  margin-top: 15px;
  
  .row {
    display: flex;
    justify-content: center;
    flex-direction: column;
    margin-bottom: 12px;
    width: 28%;
  }

  .kefu {
    height: 80px;
    background: #fff;
    border: 2px solid #fff;
    width: 24%;
    border-radius: 50%;
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      border-radius: 50%;
    }
  }

  .btn {
    width: 100%;
    height: 36px;
    background: #fff;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    color: #000;
    font-weight: 500;
    box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    cursor: pointer;
    transition: background 0.2s;
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    padding: 0 8px;
    white-space: nowrap;

    &:hover {
      background: #ffeaea;
    }

    .btn-icon {
      width: 18px;
      height: 18px;
      margin-right: 4px;
      flex-shrink: 0;
    }

    span {
      flex: 0 1 auto;
      text-align: left;
    }
  }
}
.carousel {
  width: 100%;
  max-width: 500px;
  margin: 0 auto 16px auto;
  height: 200px;
  background: #fff;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #b20000;
  font-weight: 500;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
}

.reward-section {
  box-sizing: border-box;
  width: 100%;
  background: #fff;
  border-radius: 8px;
  margin: 0 auto 16px auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.04);
  margin-bottom: 10px;
  .reward-table {
    display: grid;
    grid-template-columns: 1.5fr 1fr 1fr 1fr;
    width: 100%;
    .reward-row {
      display: contents;
      &.header span,
      &.value span {
        padding: 4px 0;
        text-align: center;
        font-size: 12px;
        color: #000;
      }
      &.header span {
        font-weight: bold;
      }
    }
    .reward-row.value span {
      font-weight: 500;
    }
  }
  .party-btn {
    width: 130px;
    background: #ed4b39;
    color: #fff;
    border: none;
    border-radius: 20px;
    padding: 4px 8px;
    font-size: 14px;
    font-weight: 500;
    margin-left: 12px;
    cursor: pointer;
    transition: all 0.2s;
    
    &:hover {
      background: #c9301c;
    }

    &.is-member {
      background: #999;
      cursor: not-allowed;
      
      &:hover {
        background: #999;
      }
    }
  }
}

.news-title {
  width: 90%;
  color: #fff;
  font-size: 20px;
  font-weight: bold;
  text-shadow: 0 2px 8px rgba(0,0,0,0.12);
}
.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>
