import share from '~/mixins/share'
import {
    getHotrecommendList,
    getWorksNum
} from "~/api/works"
import event from '~/mixins/event'
import reachBottom from '~/mixins/reachBottom'
import {
    createStoreBindings
} from 'mobx-miniprogram-bindings'
import {
    store
} from '~/store/index'
Page({
    behaviors: [reachBottom, share, event],
    data: {
        isFixed: false,
        count:{}
    },
    onShow() {
        if (typeof this.getTabBar === 'function') {
            this.getTabBar().setData({
                selected: 1
            })
        }
        this.getLocUserInfo()
        if (Object.keys(this.data.userInfo).length > 0) {
            this.requestAgain()
        } else {
            getApp().callBack = (res) => {
                this.getLocUserInfo()
                this.requestAgain()
            }
        }
    },
    loadMore() {
        if (!this.data.userInfo.grade) {
            return
        }
        this.localGetData(getHotrecommendList, {
            grade: this.data.userInfo.grade
        })
    },
    async requestAgain() {
        this.resetData()
        let count = await getWorksNum()
        this.setData({
            count
        })
    },
    async getLocUserInfo() {
        this.storeBindings = createStoreBindings(this, {
            store,
            fields: {
                userInfo: 'userInfo'
            },
        })
        this.storeBindings.updateStoreBindings()
    },
    jump({
        currentTarget
    }) {
        let type = currentTarget.dataset.type
        wx.navigateTo({
            url: `/pages/userWorks/index?type=${type}`,
        })
    },
    onReachBottom() {
        this.loadMore()
    },
    onUnload() {
        this.storeBindings.destroyStoreBindings()
    },
})