import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
import { DraggableSheetBehavior, getDraggableSheetContext } from "../../../../core/behavior/draggableSheet.behavior";

// module/health/pages/home/home.ts
import { toPatientPage, toReportPage, toSchemePage } from "../../router";
import { healthIndexMethod, healthPatientMethod, healthReportListMethod, healthReportMethod } from "../../request";
import { healthIndex2Progress } from "../../tools/health-index";
import { getTickleContext } from "../../../../core/behavior/tickle.behavior";

Component({
  behaviors: [
    PageContainerBehavior,
    DraggableSheetBehavior('.health-report-list'),
  ],
  lifetimes: {
    attached() {
      this.getHealthPatient();
      this.getHealthReport();
      this.getHealthIndex();
    }
  },
  pageLifetimes: {
    hide() { this.hideDraggableSheet({}); }
  },
  properties: {},
  observers: {
    'healthReport.data'(data) {
      this.setData({ healthId: data?.healthAnalysisReportId })
    }
  },
  data: {
    healthId: '',
    healthPatient: { data: null, loading: false, message: '' },
    healthIndex: { data: [], loading: false, message: '' },
    healthReport: { data: null, loading: false, message: '' },
    healthReportList: { data: [], loaded: false },
  },
  methods: {
    async getHealthPatient() {
      this.setData({ 'healthPatient.loading': true, })
      try {
        const data = await healthPatientMethod();
        this.setData({
          'healthPatient.data': data,
          'healthPatient.loading': false,
        });
      } catch (error) {
        this.setData({
          'healthPatient.data': [],
          'healthPatient.loading': false,
          'healthPatient.message': error.errMsg,
        });
      }
    },
    async getHealthReport(id?: string) {
      this.setData({ 'healthReport.loading': true, })
      try {
        const data = await healthReportMethod(id);
        this.setData({
          'healthReport.data': data,
          'healthReport.loading': false,
        });
      } catch (error) {
        this.setData({
          'healthReport.data': null,
          'healthReport.loading': false,
          'healthReport.message': error.errMsg,
        });
      }
    },
    async getHealthIndex(id?: string) {
      this.setData({ 'healthIndex.loading': true, })
      try {
        const data = await healthIndexMethod(id);
        this.setData({
          'healthIndex.data': healthIndex2Progress(data),
          'healthIndex.loading': false,
        });
      } catch (error) {
        this.setData({
          'healthIndex.data': [],
          'healthIndex.loading': false,
          'healthIndex.message': error.errMsg,
        });
      }
    },
    onDraggableSizeUpdate(e) {
      'worklet'
      if (e.pixels < 1) {
        wx.worklet.runOnJS(this.hideDraggableSheet.bind(this))()
      }
    },
    async showReportList() {
      if (!this.data.healthReportList.loaded) {
        wx.showLoading({ title: '加载中' })
        try {
          const data = await healthReportListMethod();
          this.setData({
            'healthReportList.data': data,
            'healthReportList.loaded': true,
          })
        } catch (error) {
          getTickleContext.call(this).showErrorMessage(error.errMsg);
        }
        wx.hideLoading();
      }
      if (this.data.healthReportList.data.length) {
        getDraggableSheetContext.call(this).scrollTo({
          size: 0.5,
          pixels: 600,
          animated: true,
          duration: 300,
          easingFunction: 'ease'
        });
        this.setData({ shade: 'shade' });
      } else {
        wx.showToast({ title: '暂无更新记录' })
      }
    },
    hideDraggableSheet(e?: any) {
      if (e) {
        getDraggableSheetContext.call(this).scrollTo({
          size: 0,
          animated: true,
          duration: 300,
          easingFunction: 'ease'
        });
      }
      this.setData({ shade: '' });
    },

    toSchemePage(event: WechatMiniprogram.TouchEvent) { toSchemePage(event.mark?.id); },
    toReportPage(event: WechatMiniprogram.TouchEvent) { toReportPage(event.mark?.id); },
    toPatientPage: toPatientPage,
    toHealthIndexListPage() {
      wx.navigateTo({ url: `/module/charts/record-index/record-index` })
    },
    toHealthStatusListPage() {
      wx.navigateTo({ url: `/module/health/pages/status-record/status-record` })
    },
  }
})