import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
import tickleBehavior, {
  getTickleContext,
} from "../../../../core/behavior/tickle.behavior";
import { getFootPrintListMethod } from "../../request";
import { Get } from "../../../../lib/request/method";
Page({
  behaviors: [PageContainerBehavior, tickleBehavior],
  lifetimes: {
    attached() {},
  },
  properties: {},
  data: {
    articleList: [] as any[],
  },
  async goDetail(e: any) {
    const type = e.currentTarget.dataset.type;
    const item = e.currentTarget.dataset.item;
    // type ，1-健康宣教 2-药膳 3-膳食 4-茶饮
    // 药膳详情
    if (type === "1") {
      try {
        const res: any = await Get(`/psarticle/clickPsarticle`, {
          params: { popularScienceArticleId: item.sourceId },
        });
        const url = res?.data;
        const detailPayload = {
          url,
        };
        wx.navigateTo({
          url: `/module/article/pages/science-info/science-info`,
        }).then((res) => {
          res.eventChannel.emit("load", detailPayload);
        });
      } catch (error) {
        console.log(error);
      }
    } else if (type === "2" || type === "4") {
      wx.navigateTo({
        url: `/module/article/pages/diet-info/diet-info?id=${item.sourceId}`,
      }).then((res) => {
        res.eventChannel.emit("load", item);
      });
    }
  },
  async load() {
    wx.showLoading({ title: "加载中" });
    try {
      const res = await getFootPrintListMethod();
      const list = res?.data?.data ?? [];
      this.setData({ articleList: Array.isArray(list) ? list : [] });
    } catch (error: any) {
      getTickleContext.call(this).showWarnMessage(error.errMsg);
    }
    wx.hideLoading();
  },
  onShow() {
    this.load();
  },
});
