// pages/index/activity/activity.js
const { museumApi } = require('../../../utils/api.js');
const { navigateToWebview, urlImg } = require('../../../utils/util.js');

Page({
  data: {
    exhibitionList: [], // 展览列表
    loading: false,
    urlImg: urlImg,
    currentPage: 1,
    pageSize: 10,
    hasMore: true,
    loadingMore: false
  },

  onLoad() {
    this.getExhibitionList(true);
  },

  onPullDownRefresh() {
    this.getExhibitionList(true);
  },

  // 页面滚动到底部时触发
  onReachBottom() {
    if (this.data.hasMore && !this.data.loadingMore) {
      this.loadMore();
    }
  },

    /**
   * 获取展览列表
   */
    async getExhibitionList(isRefresh = false, params = {}) {
        const page = isRefresh ? 1 : this.data.currentPage;
        
        if (isRefresh) {
          this.setData({ loading: true });
        } else {
          this.setData({ loadingMore: true });
        }

        try {
          const response = await museumApi.getExhibitionList({
            pageNum: page,
            pageSize: this.data.pageSize,
            status: 1,
            ...params
          });
          
          console.log('展览数据:', response);
          
          // 获取展览数据并进行筛选
          let exhibitionList = response.records || response.list || response.data || response || [];
          
          // 筛选有效的展览数据 - 确保有exhibitId和title
          exhibitionList = exhibitionList.filter(item => {
            return item && item.exhibitId && item.webSite;
          });

          if (isRefresh) {
            // 刷新时替换数据
            this.setData({ 
              exhibitionList,
              currentPage: 1,
              hasMore: exhibitionList.length >= this.data.pageSize
            });
          } else {
            // 加载更多时追加数据
            this.setData({ 
              exhibitionList: [...this.data.exhibitionList, ...exhibitionList],
              hasMore: exhibitionList.length >= this.data.pageSize
            });
          }

          return response;
        } catch (error) {
          console.error('获取展览数据失败:', error);
          wx.showToast({
            title: '加载失败',
            icon: 'none'
          });
          if (isRefresh) {
            this.setData({ exhibitionList: [] });
          }
        } finally {
          this.setData({ 
            loading: false,
            loadingMore: false
          });
          if (isRefresh) {
            wx.stopPullDownRefresh();
          }
        }
      },

  // 加载更多数据
  loadMore() {
    const nextPage = this.data.currentPage + 1;
    this.setData({ currentPage: nextPage });
    this.getExhibitionList(false);
  },


  // 返回首页
  goBack() {
    wx.navigateBack();
  },

   // 展览项点击事件
   viewExhibition(e) {
      const item = e.currentTarget.dataset.item;
      console.log('查看展览详情:', item);
      wx.navigateTo({
        url: `/pages/exhibition/activeDetails/index?isFrom=weixin&id=${item.exhibitId}&type=exhibition`
      });
   },

   // 组件：线上观展点击
   viewExhibition(e) {
    const item = e.currentTarget.dataset.item;
    if (item) {
      console.log('跳转到线上观展:', item.webSite);
      navigateToWebview(item.webSite, 'open');
    } else {
      console.log('webSite为空');
    }
  },
});