import store from './store';

Page(store.register({
  data: {
    defaultUrl: 'https://gw.alipayobjects.com/mdn/rms_a0c592/afts/img/A*BRkrSKYAcl0AAAAAAAAAAABkARQnAQ',
  },
  async onLoad(options) {
    // 由于此时不能调用$resetStore 手动清除
    this.onResetStore();
    // 根据传过来的options动态设置标题 动态加载样式
    const { title, componentUUID, backgroundUrl, templateUUID, componentType, whetherSetDynamicDatasource } = options;
    const { imgSrcPrefix } = this.data;
    my.setNavigationBar({
      // reset: true,
      title,
    });
    this.commit({
      componentUUID,
      templateUUID,
      componentType,
      whetherSetDynamicDatasource,
      // eslint-disable-next-line no-nested-ternary
      backgroundUrl: backgroundUrl ? (backgroundUrl.length === 32 ? imgSrcPrefix + backgroundUrl : backgroundUrl) : this.data.defaultUrl,
    });
    my.showLoading();
    await this.dispatch('getServiceData');
    my.hideLoading();
  },
  // 报错刷新
  onReloadHandle() {
    this.onResetStore();
    this.dispatch('getServiceData');
  },
  onResetStore() {
    this.commit({
      errorType: '',
      serviceList: [],
      pageNo: 1,
      needLoad: true,
    });
  },
  onReachBottom() {
    // 页面被拉到底部
    console.log('onReachBottom');
    this.dispatch('pullUpLoad');
  },
  /*
  * 增加下拉刷新
  * */
  async onPullDownRefresh() {
    await this.onReloadHandle();
    my.stopPullDownRefresh();
  },
}));
