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

Page({
  data: {
    newsList: [],
    loading: false,
    urlImg: urlImg
  },

  onLoad() {
    this.getNewsList();
  },

  onPullDownRefresh() {
    this.getNewsList();
  },

  // 获取资讯列表
  async getNewsList() {
    this.setData({ loading: true });
    
    try {
      const response = await museumApi.getNewsList({
        pageNum: 1,
        pageSize: 10
      });
      
      if (response && response.records) {
        this.setData({
          newsList: response.records
        });
      } else {
        this.setData({
          newsList: []
        });
      }
    } catch (error) {
      console.error('获取资讯数据失败:', error);
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      });
    } finally {
      this.setData({ loading: false });
      wx.stopPullDownRefresh();
    }
  },

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

  // 查看资讯详情
  viewNews(e) {
    const item = e.currentTarget.dataset.item;
    console.log('查看资讯详情:', item);
    navigateToWebview(`/allDetailsShow?isFrom=weixin&id=${item.informationId}&type=information`);
  }
});