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

interface Column {
  label: string;
  value: string;
  width?: number;
  height?: number;
}
const defaultColumns: Column[] = [
  { label: '', value: 'reportTime' },
  { label: '女性特殊期', value: 'womenSpecialPeriod' },
  { label: '身高', value: 'height' },
  { label: '体重', value: 'weight' },
  { label: '饮酒情况', value: 'drinkState' },
  { label: '吸烟情况', value: 'smokeState' },
  { label: '食物过敏', value: 'foodAllergy' },
  { label: '喜好口味', value: 'hobbyFlavor' },
  { label: '职业', value: 'job' },
]

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

      const { sex } = await getUserInfoMethod();
      const columns = sex === '1'
        ? [...defaultColumns]
        : defaultColumns.filter(item => item.value !== 'womenSpecialPeriod');

      let maxLines = 3;
      if (orientation === 'portrait') {
        columns[0].height = 50;
        const length = columns.length;
        const rowHeight = Math.floor((height - columns[0].height) / (length - 1));
        for (let index = 1; index < length; index++) { columns[index].height = rowHeight; }

        maxLines = Math.floor(rowHeight / 22);
      }
      this.setData({
        columns, maxLines,
        orientation,
        containerStyle: `width: ${width}px;height: ${height}px;margin-left: ${left}px;margin-right: ${right}px;`
      })
    },
    async load() {
      wx.showLoading({ title: '加载中' });
      try {
        await this.getColumns();
        const { data } = await getUserRecordMethod();
        this.setData({ dataset: data });
      } catch (error) {
        getTickleContext.call(this).showErrorMessage(error.errMsg)
      }
      wx.hideLoading();
    }
  }
})