<template>
  <div class="activity-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="content-section">
      <div class="collection-list">
        <div class="collection-item" v-for="(item, index) in activeList" :key="item.id || item.activityId || index">
          <div class="item-image-container" @click="viewActivity(item)">
            <img :src="getFieldValue(item, 'infoImg')" :alt="getFieldValue(item, 'title') || '活动图片'"
              class="item-image" />
            <div class="view-button">
              <span>查看</span><el-icon>
                <Right />
              </el-icon>
            </div>
            <div class="item-info">
              <div class="item-title">{{ getFieldValue(item, 'title') || '暂无标题' }}</div>
              <div class="item-description">{{ getFieldValue(item, 'remark') ||
                '暂无描述' }}</div>
            </div>
          </div>
        </div>
      </div>
      <!-- 空状态 -->
      <div v-if="activeList.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 { activeList, getActiveList, loading } = useIndexPageApi()

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

// 查看活动详情
const viewActivity = (item) => {
  console.log('查看活动详情:', item)
  // 跳转到详情页，传递活动ID和类型参数
  router.push({
    path: '/allDetailsShow',
    query: {
      id: item.id || item.activityId,
      type: 'activity', // 标识这是活动详情
      isFromPage: 'indexPage/activity'
    }
  })
}
</script>

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

.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%);
  }
}

.content-section {}

.collection-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.collection-item {
  background-color: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
}

.item-image-container {
  position: relative;
  height: 218px;
  overflow: hidden;
  cursor: pointer;

  .item-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .view-button {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 80px;
    height: 36px;
    top: 12px;
    right: 12px;
    background-color: rgba(0, 0, 0, 0.2);
    color: #fff;
    padding: 4px 8px;
    border-radius: 50px;
    font-size: 12px;
    border: 1px solid #fff;

    span {
      font-size: 14px;
      margin-right: 6px;
    }
  }
}

.item-info {
  position: absolute;
  width: 90%;
  bottom: 16px;
  left: 24px;

  .item-category {
    font-size: 14px;
    color: #fff;
  }

  .item-title {
    font-size: 20px;
    font-weight: bold;
    color: #fff;
  }

  .item-description {
    font-size: 12px;
    color: #fff;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

// 加载状态样式
.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;
  }
}
</style>