import { enrollRecord } from '../../api/other';

Page({
  data: {
    cardList: [],        // 报名记录列表
    page: 1,             // 当前页
    pageSize: 2,         // 每页条数
    loadingMore: false,  // 是否正在加载
    noMore: false,       // 是否已没有更多数据
    refreshing: false,    // 下拉刷新状态
  },

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

  // 请求数据
  async loadData(isRefresh = false) {
    if (this.data.loadingMore) return;
    let page = isRefresh ? 1 : this.data.page;
    this.setData({ loadingMore: true });
    try {
      const res = await enrollRecord({ page, pageSize: this.data.pageSize });
      const statusImgMap = {
        0: '/static/image/register/wks.png',
        1: '/static/image/register/yjs.png',
        2: '/static/image/register/jxz.png'
      };
      const newList = res.data.list || [];
      let allList = isRefresh ? newList : [...this.data.cardList, ...newList];
      allList = allList.map(item => ({
        ...item,
        img: statusImgMap[item.status]
      }));
      this.setData({
        cardList: allList,
        loadingMore: false,
        page: page + 1,
        noMore: allList.length >= res.data.total,
        refreshing: false
      });
    } catch (err) {
      console.error(err);
      wx.showToast({ title: '加载失败', icon: 'none' });
      this.setData({
        loadingMore: false,
        refreshing: false
      });
    }
  },
  // 上拉触底
  onReachBottom() {
    if (this.data.noMore) return;
    this.loadData();
  },
  // 下拉刷新
  onPullDownRefresh() {
    if (this.data.refreshing) return;
    this.setData({ refreshing: true, noMore: false, page: 1 });
    this.loadData(true);
  }
});
