import { connect } from 'herculex';
import { setPagesQuery } from "../../../../core/utils";
import { createSubscribe } from 'applet-page-component';
import { getMyCardInfo } from './service';
import history from '../../utils/history';
import doLogin from '../../utils/doLogin';
const app = getApp();
const qrImage = 'https://gw.alipayobjects.com/mdn/rms_373ab8/afts/img/A*z8duR6vAPp0AAAAAAAAAAAAAARQnAQ';
const addIcon = 'https://gw.alipayobjects.com/mdn/rms_373ab8/afts/img/A*dqcsTZEuGTgAAAAAAAAAAAAAARQnAQ';
Component(createSubscribe({
  onShow() {
    const {
      isLogin
    } = this.data;
    if (isLogin) this.fetchCardInfo();
  }

})(connect({
  mapStateToProps: {
    scopes: ({
      $global
    }) => $global.scopes,
    userInfo: ({
      $global
    }) => $global.userInfo || {},
    isLogin: ({
      $global
    }) => $global.userInfo.isLogin
  }
})({
  props: {
    componentData: {}
  },
  data: {
    addIcon,
    qrImage,
    cardInfo: null,
    showPatient: false,
    imgSrcPrefix: app.globalData.imgSrcPrefix
  },

  async didUpdate(_, preState) {
    const {
      isLogin = false
    } = preState;
    const {
      isLogin: curLogin
    } = this.data;

    if (isLogin !== curLogin && curLogin) {
      await this.fetchCardInfo();
    }
  },

  methods: {
    async fetchCardInfo() {
      const info = await getMyCardInfo();
      this.setData({
        cardInfo: info
      });
    },

    setQuery(patientId) {
      setPagesQuery({
        currentUserId: patientId
      });
    },

    /**
     * 去登陆
     */
    async onLogin() {
      await doLogin.call(this, {
        scopes: 'auth_user'
      });
    },

    /**
     * 切换就诊人
     */
    changeBtn() {
      this.setData({
        showPatient: true
      });
    },

    onClose() {
      this.setData({
        showPatient: false
      });
    },

    onChange(item) {
      const {
        cardInfo
      } = this.data;
      const {
        id: patientId,
        bindCardNum: cardNum
      } = item;
      this.setData({
        showPatient: false,
        cardInfo: { ...cardInfo,
          cardNum,
          patientId
        }
      });
      my.showToast({
        duration: 1500,
        content: '就诊人切换成功'
      });
    },

    toPatientDetail() {
      const {
        patientId
      } = this.data.cardInfo;
      history.push({
        title: '就诊人详情',
        query: {
          id: patientId
        },
        pageType: 'patient-detail'
      });
    },

    toAddCard() {
      const {
        userInfo
      } = this.data;
      const {
        phone,
        gender,
        idCardNo,
        fullName
      } = userInfo || {};
      const editForm = {
        idCardNo,
        name: fullName,
        defaultCard: '是',
        phoneNumber: phone,
        relationShip: '本人',
        sex: gender === 'm' ? '男' : '女'
      };
      history.push({
        title: '添加就诊卡',
        pageType: 'edit-patient',
        query: {
          editType: 'card'
        },
        componentData: {
          editForm
        }
      });
    }

  }
})));