import { createSubscribe } from 'applet-page-component';
import history from '../../../utils/history';
import { getContentHeight } from '../../../utils';
import { getPatientList } from '../../../service/common';
Component(createSubscribe({
  async onShow() {
    await this.onRefresh();
  }

})({
  props: {
    showAll: true,
    className: ''
  },
  data: {
    list: [],
    nativeList: [],
    loading: true,
    contentHeight: getContentHeight()
  },

  async didMount() {
    await this.onRefresh();
  },

  methods: {
    formatList(list) {
      const {
        showAll
      } = this.props;

      const _showAll = showAll && list.length > 1;

      const tabs = list.map(item => ({
        extData: item,
        title: item.name,
        id: item.cardNum || item.bindCardNum
      }));

      if (_showAll) {
        tabs.unshift({
          id: '',
          title: '全部',
          extData: {}
        });
      }

      return tabs;
    },

    async onRefresh() {
      let list = [];
      let cardList = [];

      try {
        list = await getPatientList();
        cardList = this.formatList(list);
      } catch (e) {
        my.showToast({
          duration: 2000,
          type: 'exception',
          content: '获取数据失败'
        });
      }

      this.setData({
        list: cardList,
        loading: false,
        nativeList: list
      });
    },

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

  }
}));