import { getCardList } from '../utils/service';
import { createSubscribe } from 'applet-page-component';
import { history, replaceStar, openCardForm } from '../utils';
import { reportCmPV_YL } from '../utils/cloudMonitorHelper';
Component(createSubscribe({
  onShow() {
    this.scrollFetch && this.scrollFetch.fetchList({
      pageIndex: 1
    }, true, true);
  }

})({
  data: {
    serviceList: []
  },
  props: {
    componentData: {}
  },

  didMount() {
    const {
      serviceCode
    } = getCurrentPages().pop().options;
    reportCmPV_YL({
      title: '在线充值',
      query: {
        serviceCode
      }
    });
  },

  methods: {
    saveRef(ref) {
      this.scrollFetch = ref;
    },

    toRechargeCard(params, cardInfo) {
      history.openPage({
        pageType: 'recharge-page',
        data: {
          cardNum: params,
          balance: String(cardInfo.balance || 0)
        }
      });
    },

    async getCardLists(params) {
      const [err, res] = await getCardList({
        type: 0,
        ...params
      });

      if (res) {
        res.list = res.list.map(item => ({ ...item,
          viewCardNum: replaceStar(item.cardNum, 3, 4)
        }));
      }

      return [err, res];
    },

    createCard(e) {
      openCardForm(e.target.dataset.item);
    }

  }
}));