import { getPatientList } from '../../../service/common';
import history from '../../../utils/history';
Component({
  props: {
    onChange() {},

    showAll: false
  },
  data: {
    checkIndex: 0,
    patientList: []
  },

  didMount() {
    this.getPatientList();
  },

  methods: {
    // getPatientList
    getPatientList() {
      getPatientList({
        type: 9,
        authCode: 9
      }).then(list => {
        let {
          checkIndex
        } = this.data;

        if (list.length === 1) {
          checkIndex = 0;
        } else if (this.props.showAll) {
          checkIndex = -1;
        }

        this.setData({
          checkIndex,
          patientList: list
        });
        this.onSelected({
          target: {
            dataset: {
              index: checkIndex,
              type: checkIndex === -1 ? 'all' : ''
            }
          }
        }, 'init');
      });
    },

    // 添加就诊人
    addPatient() {
      history.push({
        title: '添加就诊人',
        pageType: 'edit-patient'
      });
    },

    // 选中就诊人
    onSelected(e, mode = 'change') {
      const {
        index,
        type
      } = e.target.dataset;

      if (type === 'all') {
        this.setData({
          checkIndex: index
        });
        this.props.onChange(this.data.patientList, mode);
        return;
      }

      if (index > -1) {
        this.setData({
          checkIndex: index
        });
        typeof this.props.onChange === 'function' && this.props.onChange(this.data.patientList.slice(index, index + 1), mode);
      }
    }

  }
});