import history from '../../../utils/history';
import { getPatientList } from '../../../service/common';
Component({
  props: {
    params: {},
    rangeItem: {},
    show: false,
    onClose: () => null,
    onItemChange: () => null
  },
  data: {},
  methods: {
    onPopupClose() {
      this.props.onClose();
    },

    async onItemTap({
      target
    }) {
      const {
        params
      } = this.props;
      const {
        item
      } = target.dataset;
      const {
        cost,
        orderNo,
        startTime,
        orderSourceId,
        onlinePay
      } = item;
      const routeState = { ...params,
        query: { ...params.query,
          onlinePay,
          startTime,
          orderSourceId,
          amount: cost,
          queueNum: orderNo
        }
      };
      this.onPopupClose(); // this.props.onItemChange(dataset.item);

      /* 检查是不是有就诊人，没有就诊人跳转添加就诊人页面 */

      const hasPatient = await this.checkPerson(routeState);
      if (!hasPatient) return;
      history.push(routeState);
    },

    /* 检查是否有就诊人 */
    async checkPerson(routeState) {
      const patientList = await getPatientList();
      /* 如果没有就诊人, 去添加就诊人 */

      if (!patientList.length) {
        return this.toPatient(routeState);
      }

      return true;
    },

    toPatient(routeState) {
      history.push({
        title: '添加就诊人',
        pageType: 'edit-patient',
        componentData: {
          routeState: { ...routeState,
            routeType: 'replace'
          }
        }
      });
      return false;
    }

  }
});