const app = getApp()
import {
  getHotrecommendList
} from "~/api/works"
import {
  getBannerList
} from '~/api/global'
import reachBottom from '~/mixins/reachBottom'
import {
  createStoreBindings
} from 'mobx-miniprogram-bindings'
import {
  store
} from '~/store/index'
Page({
  behaviors: [reachBottom],
  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: app.globalData.navBarHeight,
    background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
    currentType: '1',
    // 控制一级分类是否固定
    isFixed: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow() {
    if (typeof this.getTabBar === 'function') {
      this.getTabBar().setData({
        selected: 1
      })
    }
    getApp().callBack = (res) => {
      this.getLocUserInfo()
      this.loadMore()
    }
  },
  onUnload() {
    this.storeBindings.destroyStoreBindings()
  },
  async getLocUserInfo() {
    this.storeBindings = createStoreBindings(this, {
      store,
      fields: {
        userInfo: 'userInfo'
      },
    })
    this.storeBindings.updateStoreBindings()
    // let res = await getBannerList()
    // console.log(res);
  },
  loadMore() {
    if (this.data.currentType == '1') {
      this.getData(getHotrecommendList, {
        grade: this.data.userInfo.grade
      })
    } else if (this.data.currentType == '2') {
      this.getData(getHotrecommendList, {
        grade: 'PRESCHOOL'
      })
    }
  },
  jumpChildClassify() {
    wx.navigateTo({
      url: `/pages/childClassify/index?type=class&id=123`,
    })
  },
  jumpSearch() {
    wx.navigateTo({
      url: '/pages/childClassify/index?type=search',
    })
  },
  /**
   * 监听页面滚动事件
   */
  onPageScroll(e) {
    if (e.scrollTop >= 103 && !this.data.isFixed) {
      this.setData({
        isFixed: true
      })
    } else if (e.scrollTop < 103 && this.data.isFixed) {
      this.setData({
        isFixed: false
      })
    }
  },
  onReachBottom() {
    this.loadMore()
  },
  selectType({
    target
  }) {
    if (target.dataset.type) {
      this.setData({
        currentType: target.dataset.type
      })
      this.resetData()
    }
  },
})