<template>
  <view class="activities-list-page">
    <!-- 顶部导航栏 -->
    <NavHeader title="工会活动" :show-placeholder="true" />

    <!-- 活动列表 -->
    <scroll-view
      class="activities-scroll"
      scroll-y="true"
      :refresher-enabled="true"
      :refresher-triggered="refreshing"
      @refresherrefresh="handleRefresh"
      @scrolltolower="handleLoadMore"
      :lower-threshold="100"
    >
      <view class="activities-list">
        <!-- 空数据提示 -->
        <view class="empty-state" v-if="!loading && activitiesList.length === 0">
          <image
            class="empty-icon"
            src="/static/home/entry_icon.png"
            mode="aspectFit"
          ></image>
          <text class="empty-text">暂无活动数据</text>
        </view>

        <!-- 活动列表项 -->
        <view
          class="activity-item"
          v-for="(item, index) in activitiesList"
          :key="index"
          @click="handleActivityClick(item)"
        >
          <view class="activity-image-wrapper">
            <image
              class="activity-image"
              :src="item.coverUrl"
              mode="aspectFill"
            ></image>
          </view>
          <view class="activity-content">
            <view class="activity-title">{{ item.title }}</view>
            <view class="activity-desc">{{ item.summary }}</view>
            <view class="activity-time"
              >结束时间:{{ formatTime(item.endTime) }}</view
            >
            <view
              class="join-activity-btn"
              @click.stop="handleJoinActivity(item)"
            >
              <text>立即参与</text>
            </view>
          </view>
        </view>

        <!-- 加载更多提示 -->
        <view class="load-more" v-if="activitiesList.length > 0">
          <text v-if="loadingMore" class="load-more-text">加载中...</text>
          <text v-else-if="!hasMore" class="load-more-text">没有更多数据了</text>
          <text v-else class="load-more-text">上拉加载更多</text>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import { getHomeData } from "@/api/home.js";
import { formatTime } from "@/utils/date.js";
import NavHeader from "@/components/NavHeader/NavHeader.vue";

export default {
  components: {
    NavHeader
  },
  data() {
    return {
      activitiesList: [],
      // 分页相关
      page: 1,
      pageSize: 10,
      total: 0,
      hasMore: true,
      loading: false,
      loadingMore: false,
      refreshing: false,
    };
  },
  onLoad() {
    this.loadActivities();
  },
  methods: {
    // 加载活动列表
    async loadActivities(isLoadMore = false) {
      // 如果正在加载或没有更多数据，则不加载
      if (this.loading || this.loadingMore || (!isLoadMore && !this.hasMore)) {
        return;
      }

      try {
        if (isLoadMore) {
          this.loadingMore = true;
        } else {
          this.loading = true;
        }

        const res = await getHomeData({
          page: this.page,
          pageSize: this.pageSize,
          noticeType: 2, // 活动类型
        });

        if (res) {
          const newList = res.list || [];
          this.total = res.total || 0;

          if (isLoadMore) {
            // 加载更多，追加数据
            this.activitiesList = [...this.activitiesList, ...newList];
          } else {
            // 首次加载或刷新，替换数据
            this.activitiesList = newList;
          }

          // 判断是否还有更多数据
          this.hasMore = this.activitiesList.length < this.total;
        }
      } catch (error) {
        console.error("加载活动列表失败:", error);
        uni.showToast({
          title: "加载失败，请重试",
          icon: "none",
        });
      } finally {
        this.loading = false;
        this.loadingMore = false;
        this.refreshing = false;
      }
    },

    // 下拉刷新
    handleRefresh() {
      this.refreshing = true;
      this.page = 1;
      this.hasMore = true;
      this.loadActivities(false);
    },

    // 上拉加载更多
    handleLoadMore() {
      if (this.hasMore && !this.loadingMore && !this.loading) {
        this.page += 1;
        this.loadActivities(true);
      }
    },

    // 格式化时间戳
    formatTime,

    // 活动项点击
    handleActivityClick(item) {
      // 后续可以跳转到活动详情页
      // uni.navigateTo({
      //   url: `/pages/activities/detail?id=${item.id}`
      // })
    },

    // 参与活动
    async handleJoinActivity(item) {
      // 跳转到活动详情页
      uni.navigateTo({
        url: `/pages/detail/activitiesDetail?id=${item.id}`
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.activities-list-page {
  height: 100vh;
  background-color: #e2e8f1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 活动列表 */
.activities-scroll {
  flex: 1;
  height: 0; // 配合 flex: 1 使用，让 scroll-view 可以滚动
}

.activities-list {
  padding: 20rpx;
  background-color: #e2e8f1;

  /* 空数据提示 */
  .empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 200rpx 0;
    min-height: 500rpx;

    .empty-icon {
      width: 200rpx;
      height: 200rpx;
      margin-bottom: 40rpx;
      opacity: 0.5;
    }

    .empty-text {
      font-family: PingFang-SC, PingFang-SC;
      font-weight: 500;
      font-size: 28rpx;
      color: #999999;
    }
  }

  /* 加载更多提示 */
  .load-more {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 40rpx 0;
    min-height: 80rpx;

    .load-more-text {
      font-family: PingFang-SC, PingFang-SC;
      font-weight: 400;
      font-size: 24rpx;
      color: #999999;
    }
  }

  .activity-item {
    display: flex;
    background-color: #ffffff;
    border-radius: 20rpx;
    margin-bottom: 30rpx;
    padding: 25rpx 20rpx;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;

    &:active {
      transform: scale(0.98);
      box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
    }

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

    .activity-image-wrapper {
      width: 186rpx;
      height: 200rpx;
      flex-shrink: 0;
      border: 1rpx solid #e2e8f1;
      border-radius: 10rpx;
      margin-right: 20rpx;

      .activity-image {
        width: 100%;
        height: 100%;
      }
    }

    .activity-content {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      position: relative;

      .activity-title {
        font-family: PingFang-SC, PingFang-SC;
        font-weight: bold;
        font-size: 30rpx;
        color: #1a1819;
        margin-bottom: 12rpx;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
      }

      .activity-desc {
        font-family: PingFang-SC, PingFang-SC;
        font-weight: 500;
        font-size: 22rpx;
        color: #888888;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        margin-bottom: 12rpx;
      }

      .activity-time {
        width: 260rpx;
        height: 33rpx;
        padding: 0 10rpx;
        background: rgba(0, 66, 148, 0.1);
        color: #004294;
        font-size: 20rpx;
        display: flex;
        align-items: center;
        margin-bottom: 12rpx;
      }

      .join-activity-btn {
        position: absolute;
        right: 0;
        bottom: 0;
        background: #004294;
        border-radius: 25rpx;
        color: #ffffff;
        font-size: 24rpx;
        padding: 12rpx 24rpx;
        cursor: pointer;
        font-family: PingFang-SC, PingFang-SC;
        font-weight: 500;
      }
    }
  }
}
</style>
