import request from '../../../../utils/request';
import { activeStatusUrl } from '../../../../utils/constants';
import { getIn } from 'herculex/dist/utils/manipulate';

// eslint-disable-next-line import/prefer-default-export
// eslint-disable-next-line complexity
export async function onResume({ state, dispatch, commit }) {
  try {
    const cardTemplateId = getIn(state, ['memberCardComponent', 'componentExtInfo', 'cardTemplateId'], '') || getIn(state, ['memberCardComponent', 'componentConfigure', 'cardTemplateId'], '');
    const { componentList = [] } = state;
    if (cardTemplateId) {
      // // const { memberInfo } = state;
      // // todo templateUUID改变时的操作
      const componentData = {
        componentExtInfo: {
          cardTemplateId,
        },
      };
      const el = await dispatch('getMemberInfo', { componentData });
      let index = '';
      for (let i = 0; i < componentList.length; i++) {
        // eslint-disable-next-line max-len
        const { componentData: { componentType, componentExtInfo = {}, componentConfigure = {} } } = componentList[i];
        const desId = componentExtInfo.cardTemplateId || componentConfigure.cardTemplateId;
        if ((componentType === 'newMemberCardComponent' || componentType === 'memberCardUserCenter' || componentType === 'memberCardComponent' || componentType === 'ownerGetCard' || componentType === 'ownerGetCardNew' || componentType === 'advertiseComponent') && desId === cardTemplateId) {
          index = i;
          break;
        }
      }
      commit('updateMemberCard', {
        item: {
          ...el,
          index,
        },
      });
      commit('updateMemberCardComponent', el);
      commit('updateNewMemberCardComponent', el);
      commit('updateMemberCardUserCenter', el);
    }

    // 医保未激活则返回上个页面
    if (componentList.some(v => v.componentType === 'medicareQueryComponent')) {
      const [, res = {}] = await request({
        isShowLoadig: true,
        isShowError: true,
        method: 'GET',
        url: activeStatusUrl,
      });
      const { bindStatus } = res;
      if (bindStatus === 'BINDED') return;
      // eslint-disable-next-line no-undef
      const len = getCurrentPages().length;
      if (len && len > 1) {
        my.navigateBack();
      } else {
        my.call('exitApp');
      }
    }
  } catch (error) {
    my.hideLoading();
  }
  // 未认证重新回到页面时查询下状态
  const { errorType } = state;
  if (errorType === 'NOCERTIFIED') {
    commit({ isLoading: true });
    await dispatch('$global:checkUserLogin');
    if (state.$global.userInfo.isCertified !== 'F') {
      commit({ errorType: null, errorMsg: null, errorBrief: null, isLoading: false });
    }
  }
}
