import { connect } from 'herculex';
import { getIn } from 'herculex/dist/utils/manipulate';
import { getAppId, aliTracertTa } from '../../utils/jsapi';
import { createSubscribe } from 'applet-page-component';

const app = getApp();
Component(
  createSubscribe({
    onShow() {
      const { componentData: { componentType } } = this.props;
      const { data } = my.getStorageSync({ key: componentType });
      // 缓存中的页面id是当前id 执行onshow
      if (this.$id === data) {
        this.showTracertTa();
      }
    },
  })(
    connect({
      mapStateToProps: {
        regionCode: state => state.$global.regionCode,
      },
    })({
      data: {
        defaultAtvar:
          'https://gw.alipayobjects.com/mdn/rms_47fd3e/afts/img/A*8THwT7aSeiYAAAAAAAAAAABkARQnAQ',
        imgSrcPrefix: app.globalData.imgSrcPrefix,
        rightTagImg:
          'https://gw.alipayobjects.com/mdn/rms_8ed4a1/afts/img/A*YPsUTLMf-AwAAAAAAAAAAAAAARQnAQ',
        componentConfigure: null,
        APPID: getAppId(),
      },
      props: {
        componentData: null,
      },
      didMount() {
        const { componentData = {} } = this.props;
        this.setData({
          componentConfigure: componentData.componentConfigure || {},
        });
        // 页面打开 埋点
        this.pageOpenTracertTa();

        // 将页面id缓存
        const { componentType } = componentData;
        my.setStorageSync({
          key: componentType,
          data: this.$id,
        });
      },
      didUpdate(preProps) {
        const {
          componentData: {
            componentExtInfo: { memberInfo: preMenberInfo },
          },
        } = preProps;
        const {
          componentData: {
            componentExtInfo: { memberInfo },
          },
        } = this.props;
        if (!preMenberInfo && memberInfo) {
          this.showTracertTa();
        }
      },
      methods: {
        addCard(e) {
          const {
            componentConfigure: { applyUrl, cardTemplateId },
            componentExtInfo: { ver = 'v3' },
          } = this.props.componentData;
          const { regionCode, APPID } = this.data;

          // 点击卡面开卡埋点
          const TracertTa = aliTracertTa({
            spmAPos: 'a2829', // spma位，必填
            spmBPos: 'b38644', // spmb位，必填
          });
          TracertTa.click('c98311.d203402', {
            APPID,
          });

          // 开卡
          const index = getIn(e, ['target', 'dataset', 'index']);
          this.dispatch('addCard', {
            index,
            applyUrl,
            cardTemplateId,
            regionCode,
            version: ver,
          });
        },
        goToCardDetail(e) {
          const { APPID } = this.data;
          // 点击卡面进入卡详情
          const TracertTa = aliTracertTa({
            spmAPos: 'a2829', // spma位，必填
            spmBPos: 'b38644', // spmb位，必填
          });
          TracertTa.click('c98313.d203404', {
            APPID,
          });
          const memberInfo = getIn(e, ['target', 'dataset', 'memberInfo'], {});
          const { passId } = memberInfo;
          console.log('卡实例ID passId===', passId);
          if (passId) {
            this.commit({
              memberInfo: {
                isOpenCardDeail: true,
                ...memberInfo,
              },
            });
            my.openCardDetail({
              passId,
            });
          }
        },
        // 会员卡面曝光
        showTracertTa() {
          const {
            componentExtInfo: { memberInfo: { flag = false } = {} },
            serviceList = [],
          } = this.props.componentData || {};
          const { APPID } = this.data;
          const TracertTa = aliTracertTa({
            spmAPos: 'a2829', // spma位，必填
            spmBPos: 'b38644', // spmb位，必填
          });

          // 已经开通
          if (flag) {
            TracertTa.expo('c98313', {
              APPID,
            });
            // 有服务, 服务列表曝光
            if (serviceList.length) {
              TracertTa.expo('c98313.d203405', {
                APPID,
              });
            }
          } else {
            // 未开通 卡面曝光
            TracertTa.expo('c98311', {
              APPID,
            });
          }
        },
        // 页面打开 埋点
        pageOpenTracertTa() {
          const TracertTa = aliTracertTa({
            spmAPos: 'a2829', // spma位，必填
            spmBPos: 'b38644', // spmb位，必填
          });
          TracertTa.logPv({});
        },
        // 快捷入口点击埋点
        bottomPointClick() {
          const { APPID } = this.data;
          const TracertTa = aliTracertTa({
            spmAPos: 'a2829', // spma位，必填
            spmBPos: 'b38644', // spmb位，必填
          });
          TracertTa.click('c98313.d203405', {
            APPID,
          });
        },
      },
    }),
  ),
);
