<template>
  <view class="my-collect-page">
    <view class="header-fixed-wrapper" :style="{ height: headerHeight + 'px' }">
      <NavHeader title="我的收藏" />
    </view>
    <view class="main-wrap" :style="{ paddingTop: headerHeight + 'px' }">
    <scroll-view
      class="collect-scroll"
      scroll-y="true"
      :refresher-enabled="true"
      :refresher-triggered="refreshing"
      @refresherrefresh="handleRefresh"
      @scrolltolower="handleLoadMore"
      :lower-threshold="100"
    >
      <view class="collect-list">
        <!-- 空数据提示 -->
        <view class="empty-state" v-if="!loading && collectList.length === 0">
          <image
            class="empty-icon"
            src="/static/home/entry_icon.png"
            mode="aspectFit"
          ></image>
          <text class="empty-text">暂无收藏数据</text>
        </view>

        <!-- 收藏列表项 -->
        <view
          class="collect-item"
          v-for="(item, index) in collectList"
          :key="index"
          @click="handleCollectClick(item)"
        >
          <view class="collect-image-wrapper" v-if="item.coverUrl">
            <image
              class="collect-image"
              :src="item.coverUrl"
              mode="aspectFill"
            ></image>
          </view>
          <view class="collect-content">
            <view class="collect-title">{{
              item.title || item.collectName || "未命名"
            }}</view>
            <view class="collect-desc" v-if="item.info">{{ item.info }}</view>
            <view class="collect-time">{{ formatTime(item.createTime) }}</view>
          </view>
        </view>

        <!-- 加载更多提示 -->
        <view class="load-more" v-if="collectList.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>
  </view>
</template>

<script>
import { getMyCollect,getBusinessData } from "@/api/profile.js";
import NavHeader from "@/components/NavHeader/NavHeader.vue";

export default {
  components: {
    NavHeader,
  },
  data() {
    return {
      statusBarHeight: 0,
      collectList: [],
      // 分页相关
      pageNo: 1,
      pageSize: 10,
      total: 0,
      hasMore: true,
      loading: false,
      loadingMore: false,
      refreshing: false,
    };
  },
  computed: {
    headerHeight() {
      return this.statusBarHeight + 44;
    },
  },
  onLoad() {
    const systemInfo = uni.getSystemInfoSync();
    this.statusBarHeight = systemInfo.statusBarHeight || 0;
    this.loadCollectList();
  },
  methods: {
    // 加载收藏列表
    async loadCollectList(isLoadMore = false) {
      // 如果正在加载或没有更多数据，则不加载
      if (this.loading || this.loadingMore || (!isLoadMore && !this.hasMore)) {
        return;
      }

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

        const res = await getMyCollect({
          pageNo: this.pageNo,
          pageSize: this.pageSize,
        });

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

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

          // 判断是否还有更多数据
          this.hasMore = this.collectList.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.pageNo = 1;
      this.hasMore = true;
      this.loadCollectList(false);
    },

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

    // 格式化时间
    formatTime(timeStr) {
      if (!timeStr) return "";
      const date = new Date(timeStr);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");
      const hours = String(date.getHours()).padStart(2, "0");
      const minutes = String(date.getMinutes()).padStart(2, "0");
      return `${year}-${month}-${day} ${hours}:${minutes}`;
    },

    // 收藏项点击：跳转详情，详情页通过 getBusinessData(id: collectId, type: collectType) 拉取数据
    handleCollectClick(item) {
      const collectId = item.collectId ?? item.id;
      const collectType = item.collectType ?? item.type ?? "";
      if (collectId == null || collectId === "") {
        uni.showToast({ title: "数据异常", icon: "none" });
        return;
      }
      uni.navigateTo({
        url: `/pages/detail/richTextDetail?collectId=${encodeURIComponent(collectId)}&collectType=${encodeURIComponent(collectType)}&hideBar=1`,
      });
    },
  },
};
</script>

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

.header-fixed-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background: #e2e8f1;
}

.main-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.collect-scroll {
  flex: 1;
  height: 0; // 配合 flex: 1 使用，让 scroll-view 可以滚动
}

.collect-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: 356rpx;
      height: 266rpx;
      margin-bottom: 40rpx;
    }

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

  .collect-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;
    }

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

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

    .collect-content {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .collect-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;
      }

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

      .collect-time {
        font-family: PingFang-SC, PingFang-SC;
        font-weight: 400;
        font-size: 22rpx;
        color: #999999;
      }
    }
  }
}
</style>
