const { works } = require("../../mock/photoBook");

Page({
  data: {
    tabs: works.tabs,
    activeTab: works.activeTab,
    list: works.list,
    leftList: [],
    rightList: [],
    currentList: [],
    currentCount: works.list.length,
  },

  onLoad() {
    this.applyFilter(this.data.activeTab);
  },

  splitList(list) {
    const leftList = [];
    const rightList = [];
    list.forEach((item, index) => {
      if (index % 2 === 0) {
        leftList.push(item);
      } else {
        rightList.push(item);
      }
    });
    this.setData({
      leftList,
      rightList,
      currentList: list,
      currentCount: list.length,
    });
  },

  applyFilter(tab) {
    const source = this.data.list || [];
    const filtered = tab === "全部" ? source : source.filter((item) => item.category === tab);
    this.splitList(filtered);
  },

  onTapTabFilter(e) {
    const tab = e.currentTarget.dataset.tab;
    this.setData({ activeTab: tab });
    this.applyFilter(tab);
  },

  onTapBook() {
    wx.navigateTo({
      url: "/pages/booking/index",
    });
  },

  onTapPreview(e) {
    const id = e.currentTarget.dataset.id;
    const list = this.data.currentList || [];
    const urls = list.map((item) => item.cover).filter(Boolean);
    if (!urls.length) {
      return;
    }
    const current = (list.find((item) => item.id === id) || list[0]).cover;
    wx.previewImage({
      current,
      urls,
    });
  },
});
