import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
import tickleBehavior, {
  getTickleContext,
} from "../../../../core/behavior/tickle.behavior";
import { getScienceMethod } from "../../request";
// module/diet/pages/science-info/science-info.ts
Component({
  behaviors: [PageContainerBehavior, tickleBehavior],
  lifetimes: {
    attached() {
      console.log(this.data, "this.data===获取宣教传来的参数111");
      const channel = this.getOpenerEventChannel();
      channel.on?.("load", (data: AnyObject) => {
        console.log(data, "data===获取宣教传来的参数");
        // 支持直接传入外链 URL，直接使用 web-view 打开 新加的
        if (data?.url) {
          this.setData({
            dataset: { content: data.url },
            title: data?.title ?? "",
          });
          return;
        }
        // ===end===

        this.setData({ title: data?.title ?? "" });
      });
      if (this.data.id) {
        this.load();
      }
      // this.load();
    },
  },
  properties: {
    id: { type: String, value: "" },
  },
  data: {
    title: "",
  },
  methods: {
    async load() {
      wx.showLoading({ title: "加载中" });
      try {
        const dataset = await getScienceMethod(this.data.id);
        this.setData({ dataset, title: dataset.name });
      } catch (error) {
        getTickleContext.call(this).showWarnMessage(error.errMsg);
      }
      wx.hideLoading();
    },
  },
});
