import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
import I18nBehavior from "../../../../i18n/behavior";
import TickleBehavior, { getTickleContext } from "../../../../core/behavior/tickle.behavior";

// module/health/pages/analysis/analysis.ts
import { toHomePage } from "../../router";
import { healthAnalysisMethod } from "../../request";
import { AnalysisModel } from "../../model/health.model";


Page({
  behaviors: [
    I18nBehavior,
    PageContainerBehavior,
    TickleBehavior
  ],
  data: {
    i18n: {
      analysis: { title: '图片结果', date: '' },
    },
    id: '',
    tongue: null as unknown as AnalysisModel,
    face: null as unknown as AnalysisModel,
    dataset: null as unknown as AnyObject,
    switchType: "",
  },
  onLoad(query: any) {
    if (query.scene) {
      this.setData({ switchType: query.scene });
      wx.showLoading({ title: "加载中" });
    } else this._load(query);
  },
  load(event: AnyObject) { this._load(event.detail, false); },
  async _load(query: Record<'id' | 'scene', string>, loading = true) {
    if (loading) wx.showLoading({ title: '加载中' });
    try {
      const { tongue, face, ...dataset } = await healthAnalysisMethod(query);
      this.setData({ tongue, face, dataset });
    } catch (error) {
      getTickleContext.call(this).showErrorMessage(error.errMsg, 0);
    }
    wx.hideLoading();
  },

  toHomePage() { toHomePage(this.data.switchType as string); },
  openMessage(event: any) {
    const message = event?.detail?.content;
    if (message) getTickleContext.call(this).showMessage(event.detail.type || 'info', message);
  }
});