import tickleBehavior, { getTickleContext } from "../../../../core/behavior/tickle.behavior";
// module/health/pages/status-record/status-record.ts
import { getStatusRecordMethod } from "../../request";
import { getPageOrientation } from "../../../../lib/wx/page";

interface Column {
  label: string;
  value: string;
  width?: number;
  height?: number;
  type?: 'picture' | 'text';
}
const defaultColumns: Column[] = [
  { label: '', value: 'reportTime', height: 50 },
  { label: '症状', value: 'pickedSymptom' },
  { label: '联想症状', value: 'algorithmInferSymptom' },
  { label: '舌面', value: 'upImg', type: 'picture', width: 120, height: 120 },
  { label: '舌底', value: 'downImg', type: 'picture', width: 120, height: 120 },
  { label: '面部', value: 'faceImg', type: 'picture', width: 120, height: 120 },
]

Component({
  behaviors: [
    tickleBehavior,
  ],
  lifetimes: {
    attached() { this.load(); }
  },
  properties: {},
  data: {
    orientation: '' as 'portrait' | 'landscape',
    containerStyle: '',
    columns: [] as Column[],
  },
  methods: {
    async getColumns() {
      const { orientation, width, height, left = 0, right = 0, } = getPageOrientation();

      const columns = defaultColumns;
      let maxLines = 3;

      if (orientation === 'portrait') {
        const { defaultHeight, count } = columns.reduce((obj, column) => {
          if (column.height) {
            obj.defaultHeight += column.height;
            obj.count += 1;
          }
          return obj;
        }, { defaultHeight: 0, count: 0 })
        const length = columns.length - count;
        const rowHeight = Math.floor((height - defaultHeight) / length);
        for (const column of columns) { if (column.height == null) column.height = rowHeight; }

        maxLines = Math.floor(rowHeight / 22);
      }

      this.setData({
        columns,
        orientation,
        containerStyle: `width: ${width}px;height: ${height}px;margin-left: ${left}px;margin-right: ${right}px;`,

        maxLines,
      })
    },
    async load() {
      wx.showLoading({ title: '加载中' });
      try {
        await this.getColumns();
        const { data } = await getStatusRecordMethod();
        this.setData({ dataset: data });
      } catch (error) {
        getTickleContext.call(this).showErrorMessage(error.errMsg)
      }
      wx.hideLoading();
    },
    loadMore() {
      console.log('加载更多');

    },
    preview(event: WechatMiniprogram.TouchEvent) {
      const { url, item } = event.target.dataset
      const sources = [item.upImg, item.downImg, item.faceImg].filter(Boolean);
      const current = sources.findIndex(s => s === url);
      if (current !== -1) wx.previewMedia({ sources: sources.map(url => ({ url, type: 'image' })), current });
    }
  }
})