<template>
  <div class="news-container">
    <!-- 返回按钮 -->
    <div class="back-button" @click="router.replace('/indexPage')">
      <img src="@/assets/indexPage/icon_back.png" alt="">
    </div>

    <!-- 标题 -->
    <div class="section-title">展馆资讯</div>

    <!-- 资讯列表部分 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-text">加载中...</div>
    </div>
    <div v-else class="scroll-container">
      <div class="scroll-wrapper">
        <div class="all-item" v-for="item in newsList" :key="item.informationId">
          <div class="exhibition-item" @click="viewExhibition(item)">
            <img :src="getFieldValue(item, 'infoImg')" :alt="getFieldValue(item, 'title') || '资讯图片'"
              class="exhibition-img">
            <img src="@/assets/indexPage/Frame.png" alt="kuang" class="bg-img">
            <div class="exhibition-info">
              <h4 class="exhibition-title">{{ getFieldValue(item, 'title') || '暂无标题' }}</h4>
            </div>
          </div>
          <!-- 查看详情按钮 -->
          <div class="view-details-button" @click="viewExhibition(item)">
            <span>查看详情</span>
            <el-icon>
              <Right />
            </el-icon>
          </div>
        </div>
      </div>
      <!-- 空状态 -->
      <div v-if="newsList.length === 0" class="empty-state">
        <div class="empty-text">暂无资讯数据</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useIndexPageApi } from '@/setup/useIndexPageApi'

const router = useRouter()
const route = useRoute()

// 检查是否为预览模式
const isPreviewMode = computed(() => {
  return route.query.preview === '1'
})

// 获取字段值的通用方法，优先获取带B后缀的字段
const getFieldValue = (item, fieldName) => {
  if (isPreviewMode.value) {
    return item[fieldName + 'B'] || item[fieldName]
  }
  return item[fieldName]
}

// 使用组合式函数获取API方法和响应式数据
const { newsList, getNewsList, loading } = useIndexPageApi()

// 页面初始化时获取资讯数据
onMounted(async () => {
  try {
    const params = { pageNum: 1, pageSize: 10 }
    // 如果是预览模式，设置status为-1
    if (isPreviewMode.value) {
      params.status = -1
    }
    await getNewsList(params)
  } catch (error) {
    console.error('获取资讯数据失败:', error)
  }
})

// 查看资讯详情
const viewExhibition = (item) => {
  console.log('查看资讯详情:', item)
  // 跳转到详情页，传递资讯ID和类型参数
  router.push({
    path: `/allDetailsShow`,
    query: {
      id: item.informationId,
      type: 'information', // 标识这是资讯详情
      isFromPage: 'indexPage/news'
    }
  })
}
</script>

<style lang="scss" scoped>
.news-container {
  position: relative;
  height: 100vh;
  padding: 20px;
  background: url('@/assets/indexPage/bg.png') no-repeat;
  background-size: cover;
  overflow: auto;
}

@media (min-width: 1024px) {
  .news-container {
    width: 400px;
  }
}

.back-button {
  position: absolute;
  top: 20px;
  left: 20px;
  width: 40px;
  height: 40px;
  border-radius: 50%;

  img {
    width: 40px;
    height: 40px;
  }
}

.section-title {
  font-size: 20px;
  font-weight: bold;
  color: #584735;
  margin: 54px 0 20px 0;
  position: relative;
  padding-bottom: 10px;

  &::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 2px;
    background: linear-gradient(90deg, rgba(91, 71, 46, 0.5) 0%, rgba(91, 71, 46, 0) 100%)
  }
}

// 加载状态样式
.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;

  .loading-text {
    font-size: 18px;
    color: #584735;
    opacity: 0.8;
  }
}

// 空状态样式
.empty-state {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
  margin-top: 50px;

  .empty-text {
    font-size: 18px;
    color: #584735;
    opacity: 0.6;
  }
}

// 滚动容器
.scroll-container {
  .scroll-wrapper {
    display: flex;
    padding-bottom: 5px;
  }

  /* 仅在移动端隐藏滚动条 */
  @media (max-width: 768px) {
    scrollbar-width: none;
    -ms-overflow-style: none;

    &::-webkit-scrollbar {
      display: none;
    }
  }

  /* 桌面端显示并美化滚动条 */
  @media (min-width: 769px) {
    scrollbar-width: thin;
    scrollbar-color: #B1967B #f1f1f1;

    &::-webkit-scrollbar {
      height: 8px;
    }

    &::-webkit-scrollbar-track {
      background: #f1f1f1;
      border-radius: 4px;
    }

    &::-webkit-scrollbar-thumb {
      background: #B1967B;
      border-radius: 4px;
    }

    &::-webkit-scrollbar-thumb:hover {
      background: #9d8469;
    }
  }
}

.all-item {
  position: relative;
  flex: 0 0 auto;
  width: 327px;
  height: 600px;
  margin-right: 10px;

  .exhibition-item {
    position: relative;
    flex: 0 0 auto;
    width: 327px;
    height: 459px;
    border-radius: 5px;
    overflow: hidden;

    &:last-child {
      margin-right: 0;
    }

    .exhibition-img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .bg-img {
      position: absolute;
      width: 301px;
      height: 431px;
      top: 14px;
      left: 14px;
    }

    .exhibition-info {
      width: 345px;
      height: 30px;
      display: flex;
      justify-content: center;
      align-items: center;
      opacity: 0.8;
      position: absolute;
      bottom: 47px;

      .exhibition-title {
        font-size: 24px;
        margin: 0;
        color: #fff;
        font-weight: 800;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }

  .view-details-button {
    position: absolute;
    bottom: 40px;
    left: 50%;
    transform: translateX(-50%);
    width: 215px;
    height: 58px;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(90deg, #B1967B 0%, #9F6F3F 100%);
    border-radius: 50px 50px 50px 50px;
    font-size: 20px;
    cursor: pointer;

    span {
      margin-right: 16px;
    }
  }
}
</style>