import { connect } from 'herculex';
import { getIn } from 'herculex/dist/utils/manipulate';
import { openCardCase } from './../utils';
const setToast = {
  AUDITING() {
    my.showToast({
      content: '会员卡审核中'
    });
  },

  AUDIT_REJECT() {
    my.showToast({
      content: '会员卡审核驳回'
    });
  },

  WAIT_RELEASE() {
    my.showToast({
      content: '会员卡审核通过'
    });
  },

  CARD() {
    my.showToast({
      content: '会员卡已开卡'
    });
  }

};
const app = getApp();
Component(connect({
  mapStateToProps: {
    componentList: state => state.componentList
  }
})({
  props: {
    componentData: {}
  },
  data: {
    imgSrcPrefix: app.globalData.imgSrcPrefix
  },

  didMount() {},

  didUpdate(preProps) {
    // eslint-disable-next-line max-len
    if (preProps.componentData.componentExtInfo.memberInfo && this.props.componentData.componentExtInfo.memberInfo) {
      const preFlag = preProps.componentData.componentExtInfo.memberInfo.flag || false;
      const flag = this.props.componentData.componentExtInfo.memberInfo.flag || false;

      if (!preFlag && flag) {
        my.navigateBack();
      }
    }
  },

  methods: {
    setBack() {
      const pages = getCurrentPages();
      const lastpage = pages[pages.length - 2];
      lastpage.setData({
        naviBack: 'new-member-card'
      });
      my.navigateBack();
    },

    async openCardCase() {
      const {
        cardType,
        cardTemplateId: cardTemplateIdS
      } = this.$page.options;

      if (cardType === 'newMemberCardComponent') {
        // eslint-disable-next-line no-unused-vars
        const [_, res] = await openCardCase({
          cardTemplateId: cardTemplateIdS
        });

        if (res !== 'NOT_CARD') {
          // eslint-disable-next-line no-unused-expressions
          setToast[res]();
          this.setBack();
          return false;
        } else {
          return true;
        }
      }
    },

    async addCard(e) {
      const {
        componentExtInfo: {
          applyUrl,
          cardTemplateId
        }
      } = this.props.componentData;
      const {
        cardType,
        cardTemplateId: cardTemplateIdS
      } = this.$page.options;
      const index = getIn(e, ['target', 'dataset', 'index']);
      const query = cardType === 'newMemberCardComponent' ? {
        index,
        applyUrl,
        cardTemplateId: cardTemplateIdS,
        card: 'newMemberCardComponent'
      } : {
        index,
        applyUrl,
        cardTemplateId
      };

      if (cardType === 'newMemberCardComponent') {
        const results = await this.openCardCase();

        if (!results) {
          return false;
        }
      }

      const result = await this.dispatch('addCard', query);

      if (cardType === 'newMemberCardComponent') {
        // eslint-disable-next-line no-unused-vars
        await this.openCardCase();
        return;
      }

      if (result.code && result.code === 'USER_BACK') return; // 失败

      if (result !== 'addCardSuccess') {
        const {
          serviceList = []
        } = this.props.componentData;
        const {
          url,
          urlType
        } = serviceList[1] || {};
        const payload = {
          urlType,
          url
        };

        if (url) {
          return this.$page.dispatchGlobal('handleJumpService', payload);
        }
      }
    }

  }
}));