import tickleBehavior, { getTickleContext } from "../../../../core/behavior/tickle.behavior";
import DictionariesBehavior from "../../../../core/behavior/dictionaries.behavior";
import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
import { PageLoadBehavior } from "../../../../core/behavior/page-loading.behavior";
import { getUserInfoMethod, updateUserInfoMethod } from "../../request"

// module/user/pages/user-edit/user-edit.ts
Component({
  behaviors: [
    PageContainerBehavior,
    PageLoadBehavior<App.Patient.Model>(getUserInfoMethod),
    DictionariesBehavior,
    tickleBehavior
  ],
  lifetimes: {
    attached() { }
  },
  properties: {},
  data: {
    model: null as App.Patient.Model | null,
    dirty: false,
  },
  methods: {
    onSubmit(event: WechatMiniprogram.FormSubmit) {
      this.setData({ dirty: true })

      const data = { ...this.data.model, ...event.detail.value };
      if (!data.patientId) data.patientId = wx.getStorageSync('patientId');
      if (data.sex.toString() === '1' && !data.womenSpecialPeriod) return getTickleContext.call(this).showWarnMessage('请至少选择一项女性特殊期');
      if (!data.height) return getTickleContext.call(this).showWarnMessage('请填写身高');
      if (!data.weight) return getTickleContext.call(this).showWarnMessage('请填写体重');

      updateUserInfoMethod(<any>data)
        .then(() => wx.navigateBack())
        .then(() => { this.getOpenerEventChannel().emit('update2', event.detail.value); })
        .catch((error) => getTickleContext.call(this).showWarnMessage(error.errMsg))
    },
  },
})