import {
  getBannerList
} from '~/api/global'
import {
  createStoreBindings
} from 'mobx-miniprogram-bindings'
import {
  store
} from '~/store/index'
Page({
  data: {
    activityList: false,
    bannerList: []
  },
  onShow() {
    if (typeof this.getTabBar === 'function') {
      this.getTabBar().setData({
        selected: 1
      })
      this.getLocUserInfo()
      if (Object.keys(this.data.userInfo).length > 0) {
        this.getBannerList()
        this.setData({
          activityList: true
        })
      } else {
        getApp().callBack = (res) => {
          this.getLocUserInfo()
          this.getBannerList()
          this.setData({
            activityList: true
          })
        }
      }

    }
  },
  onHide() {
    this.setData({
      activityList: false
    })
  },
  async getBannerList() {
    let bannerList = await getBannerList({
      grade: this.data.userInfo.grade,
    })
    this.setData({
      bannerList,
    })
  },
  resetData() {
    this.selectComponent('#activityList').getActivities()
    this.getBannerList()
  },
  async getLocUserInfo() {
    this.storeBindings = createStoreBindings(this, {
      store,
      fields: {
        userInfo: 'userInfo'
      },
    })
    this.storeBindings.updateStoreBindings()
  },
  onUnload() {
    this.storeBindings.destroyStoreBindings()
  },
})