<template>
  <div class="home">
    <!-- 顶部功能区域 -->
    <div class="header-section">
      <div class="function-grid">
        <div class="function-row">
          <button class="function-btn" @click="toInvitePeople">
            <img src="@/assets/1.png" alt="邀请百姓" class="btn-icon" />
            <span>邀请百姓</span>
          </button>
          <button class="function-btn kefu-btn" @click="toKefu">
            <img src="@/assets/kefu.jpg" alt="客服" class="kefu-avatar" />
            <span>在线客服</span>
          </button>
          <button class="function-btn" @click="toOfficialGroup">
            <img src="@/assets/2.png" alt="官方群聊" class="btn-icon" />
            <span>官方群聊</span>
          </button>
        </div>
        <div class="function-row">
          <button class="function-btn" @click="toAppDownload">
            <img src="@/assets/3.png" alt="APP下载" class="btn-icon" />
            <span>APP下载</span>
          </button>
          <button class="function-btn" @click="toCultureDetail">
            <img src="@/assets/4.png" alt="央企文化" class="btn-icon" />
            <span>央企文化</span>
          </button>
        </div>
      </div>
    </div>

    <!-- 轮播图区域 -->
    <div class="carousel-section">
      <div class="section-title">信息公告</div>
      <div class="carousel-container">
        <van-swipe
          :autoplay="3000"
          indicator-color="#2f9580"
          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>

    <!-- 团队奖励区域 -->
    <div class="reward-section">
      <div class="section-title">团队奖励政策</div>
      <div class="reward-card">
        <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>

    <!-- 新闻中心区域 -->
    <div class="news-section">
      <div class="section-title">新闻中心</div>
      <div class="news-list">
        <div
          class="news-item"
          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>
    </div>

    <!-- 实名认证弹窗 -->
    <van-dialog
      v-model="showAuthDialog"
      :show-confirm-button="false"
      :show-cancel-button="false"
      :close-on-click-overlay="true"
      class="gov-dialog auth-dialog"
      v-if="userInfo.if_real === 0"
    >
      <div class="dialog-header">
        <h3 class="dialog-title">系统提醒</h3>
        <van-icon name="cross" class="close-icon" @click="showAuthDialog = false" />
      </div>
      <div class="dialog-body">
        <div class="auth-icon">⚠️</div>
        <p class="auth-text">您还未进行实名认证，请及时完成认证以确保账户安全。</p>
        <button class="gov-btn primary" @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="gov-dialog notice-dialog"
      v-if="userInfo.gonggao"
    >
      <div class="dialog-header">
        <h3 class="dialog-title">{{userInfo.gonggao.title}}</h3>
        <van-icon name="cross" class="close-icon" @click="showDialog = false" />
      </div>
      <div class="dialog-body">
        <div class="dialog-content" v-html="userInfo.gonggao.value"></div>
      </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 => {
        const urlMatch = news.content.match(/https?:\/\/[^\s<>"']+/g);
        
        if (urlMatch && (urlMatch[0].includes('.html') || urlMatch[0].includes('.com'))) {
          return {
            type: 'iframe',
            url: urlMatch[0]
          };
        } else {
          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;
  background: linear-gradient(to bottom, #2f9580 0%, #4fb3a1 100%);
  padding: 20px 16px 80px;
  box-sizing: border-box;
}

.header-section {
  margin-bottom: 24px;
  
  .function-grid {
    .function-row {
      display: flex;
      gap: 12px;
      margin-bottom: 12px;
      
      .function-btn {
        flex: 1;
        height: 64px;
        background: #ffffff;
        border: 1px solid #e5e7eb;
        border-radius: 8px;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        transition: all 0.3s ease;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
        
        &:hover {
          background: #f8fffe;
          border-color: #2f9580;
          transform: translateY(-1px);
          box-shadow: 0 4px 12px rgba(47, 149, 128, 0.15);
        }
        
        .btn-icon {
          width: 24px;
          height: 24px;
          margin-bottom: 4px;
        }
        
        .kefu-avatar {
          width: 32px;
          height: 32px;
          border-radius: 50%;
          margin-bottom: 4px;
        }
        
        span {
          font-size: 12px;
          color: #374151;
          font-weight: 500;
        }
        
        &.kefu-btn {
          background: linear-gradient(135deg, #2f9580 0%, #4fb3a1 100%);
          border-color: #2f9580;
          color: #ffffff;
          
          span {
            color: #ffffff;
          }
          
          &:hover {
            background: linear-gradient(135deg, #268371 0%, #3a9d8e 100%);
          }
        }
      }
    }
  }
}

.carousel-section, .reward-section, .news-section {
  margin-bottom: 24px;
  
  .section-title {
    color: #ffffff;
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 12px;
    padding-left: 8px;
    position: relative;
    
    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 4px;
      height: 16px;
      background: #ffffff;
      border-radius: 2px;
    }
  }
}

.carousel-container {
  background: #ffffff;
  border-radius: 8px;
  padding: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.reward-card {
  background: #ffffff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
  
  .reward-table {
    display: grid;
    grid-template-columns: 1.5fr 1fr 1fr 1fr;
    flex: 1;
    
    .reward-row {
      display: contents;
      
      &.header span {
        font-weight: 600;
        color: #2f9580;
        font-size: 14px;
        padding: 8px 4px;
        text-align: center;
      }
      
      &.value span {
        font-weight: 500;
        color: #374151;
        font-size: 16px;
        padding: 4px;
        text-align: center;
      }
    }
  }
  
  .party-btn {
    width: 100px;
    height: 40px;
    background: #2f9580;
    color: #ffffff;
    border: 1px solid #2f9580;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    margin-left: 16px;
    cursor: pointer;
    transition: all 0.3s ease;
    
    &:hover {
      background: #268371;
      border-color: #268371;
    }

    &.is-member {
      background: #94a3b8;
      border-color: #94a3b8;
      cursor: not-allowed;
      
      &:hover {
        background: #94a3b8;
        border-color: #94a3b8;
      }
    }
  }
}

.news-list {
  .news-item {
    background: #ffffff;
    border-radius: 8px;
    padding: 16px;
    margin-bottom: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.3s ease;
    
    &:hover {
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
      transform: translateY(-1px);
    }

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

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

    :deep(img) {
      width: 100px;
      height: 80px;
      object-fit: cover;
      border-radius: 6px;
      flex-shrink: 0;
    }

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

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

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

    :deep(a) {
      color: #2f9580;
      text-decoration: none;
      cursor: pointer;
      
      &:hover {
        text-decoration: underline;
        color: #268371;
      }
    }
  }
}

/* 政府风格弹窗样式 */
.gov-dialog {
  :deep(.van-dialog) {
    border-radius: 8px;
    overflow: hidden;
  }
  
  :deep(.van-dialog__content) {
    padding: 0;
    background: #ffffff;
  }
  
  .dialog-header {
    background: linear-gradient(135deg, #f8fffe 0%, #f0fdfb 100%);
    padding: 16px 20px;
    border-bottom: 1px solid #e5e7eb;
    display: flex;
    align-items: center;
    justify-content: space-between;
    
    .dialog-title {
      font-size: 16px;
      font-weight: 600;
      color: #2f9580;
      margin: 0;
    }
    
    .close-icon {
      font-size: 18px;
      color: #6b7280;
      cursor: pointer;
      padding: 4px;
      border-radius: 4px;
      transition: all 0.3s ease;
      
      &:hover {
        background: rgba(47, 149, 128, 0.1);
        color: #2f9580;
      }
    }
  }
  
  .dialog-body {
    padding: 20px;
    text-align: center;
  }
}

.auth-dialog {
  .dialog-body {
    .auth-icon {
      font-size: 48px;
      margin-bottom: 16px;
    }
    
    .auth-text {
      font-size: 14px;
      color: #6b7280;
      margin-bottom: 24px;
      line-height: 1.5;
    }
  }
}

.notice-dialog {
  .dialog-content {
    max-height: 60vh;
    overflow-y: auto;
    text-align: left;
    
    :deep(p) {
      margin: 12px 0;
      font-size: 14px;
      color: #374151;
      line-height: 1.6;
    }
  }
}

.gov-btn {
  padding: 12px 24px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid transparent;
  
  &.primary {
    background: #2f9580;
    color: #ffffff;
    border-color: #2f9580;
    
    &:hover {
      background: #268371;
      border-color: #268371;
    }
  }
}

/* 移动端适配 */
@media (max-width: 768px) {
  .home {
    padding: 16px 12px 80px;
  }
  
  .function-grid {
    .function-row {
      gap: 8px;
      
      .function-btn {
        height: 56px;
        
        .btn-icon {
          width: 20px;
          height: 20px;
        }
        
        .kefu-avatar {
          width: 28px;
          height: 28px;
        }
        
        span {
          font-size: 11px;
        }
      }
    }
  }
  
  .reward-card {
    padding: 16px;
    flex-direction: column;
    gap: 16px;
    
    .party-btn {
      margin-left: 0;
      width: 120px;
    }
  }
}
</style>
