import { connect } from 'herculex';
import history from '../../../utils/history';
/* 默认头像 */

const defaultUrl = 'https://gw.alipayobjects.com/mdn/rms_373ab8/afts/img/A*TuMaTaHOpyUAAAAAAAAAAAAAARQnAQ';
Component(connect({
  mapStateToProps: {
    isLogin: ({
      $global
    }) => $global.userInfo.isLogin
  }
})({
  props: {
    item: {},
    isFlex: true,
    isRange: false,
    componentData: {}
  },
  data: {
    defaultUrl,
    rangeItem: {},
    routeState: {},
    showRange: false,
    showDoctor: false
  },

  didMount() {},

  methods: {
    onPopupClose() {
      this.props.onClose();
    },

    /* 检查剩余 */
    checkRemain(item) {
      const {
        remain = 0
      } = item;
      const disabled = remain < 1;

      if (disabled) {
        my.showToast({
          content: '该医生当前已约满，无法预约!'
        });
      }

      return !disabled;
    },

    async toDoctorPage({
      target
    }) {
      const {
        item
      } = target.dataset;
      const {
        doctorId
      } = item;
      const {
        componentData
      } = this.props;
      if (!this.checkRemain(item)) return;
      const {
        department: {
          departmentId
        }
      } = componentData;
      history.push({
        query: {
          doctorId,
          // 医生唯一识别号
          depId: departmentId // 科室id

        },
        title: '医生主页',
        pageType: 'doctor-page'
      });
    },

    async onItemFollow({
      target
    }) {
      const {
        item
      } = target.dataset;
      if (!this.checkRemain(item)) return;
      const {
        rangeItem
      } = this.data;
      const {
        componentData
      } = this.props;
      const params = { ...rangeItem,
        ...componentData
      };
      const {
        level,
        doctorId,
        onlinePay = false,
        name: doctorName
      } = this.props.item;
      const {
        area,
        department
      } = params;
      const {
        name: districtName
      } = area;
      const {
        name: depName
      } = department;
      const routeState = {
        query: {
          level,
          depName,
          doctorId,
          doctorName,
          onlinePay,
          districtName,
          payType: '就诊时支付'
        },
        title: '挂号确认',
        pageType: 'order-pay'
      };
      this.setData({
        showRange: true,
        rangeItem: item,
        routeState
      });
    },

    onTimeRangeClose() {
      this.setData({
        showRange: false
      });
    },

    onClosedDoctor() {
      this.setData({
        showDoctor: false
      });
    },

    onShowDoctor() {
      this.setData({
        showDoctor: true
      });
    }

  }
}));