import { saveAdmissionRegistration } from '../add-inpatient-information/service';
import history from '../../utils/history';
Component({
  data: {
    personItem: {},
    // 就诊人信息
    inpatientNo: '',
    showOtherButton: 'false'
  },

  didMount() {
    // 获取用户id
    const {
      personItem,
      showOtherButton
    } = this.$page.data.query;
    console.log(showOtherButton);
    this.setData({
      personItem: personItem ? JSON.parse(personItem) : {},
      inpatientNo: '',
      showOtherButton
    });
  },

  methods: {
    // 表单提交
    async onSubmit() {
      const {
        inpatientNo,
        personItem
      } = this.data;

      if (/.*[\u4e00-\u9fa5]+.*$/.test(inpatientNo)) {
        my.showToast({
          content: '住院号不能有汉字'
        });
        return;
      } // 调用方法 - 添加住院人


      delete personItem.id;
      const {
        inpatientId
      } = await saveAdmissionRegistration({
        inpatientNo,
        ...personItem,
        patientId: this.data.personItem.id
      }); // 如果添加失败，toast提示

      if (!inpatientId) {
        my.showToast({
          content: '住院号信息有误，请重新输入！'
        });
        return;
      } // 如果添加成功，跳转至登记结果页


      if (inpatientId) {
        my.showToast({
          content: '添加成功',
          duration: 1000,
          success: () => {
            history.replace({
              query: {
                inpatientId,
                showOtherButton: this.data.showOtherButton
              },
              title: '登记结果',
              pageType: 'registration-result'
            });
          }
        });
      }
    },

    handleInput(e) {
      this.setData({
        inpatientNo: e.detail.value
      });
    }

  }
});