const app = getApp()
import {
    getHotrecommendList,
    getAuthorityList,
    getCategoryList
} from "~/api/works"
import {
    getBannerList
} from '~/api/global'
import reachBottom from '~/mixins/reachBottom'
import share from '~/mixins/share'
import {
    createStoreBindings
} from 'mobx-miniprogram-bindings'
import {
    store
} from '~/store/index'
Page({
    behaviors: [reachBottom, share],
    data: {
        navBarHeight: app.globalData.navBarHeight,
        background: ['demo-text-1', 'demo-text-2', 'demo-text-3'],
        currentType: '2',
        // 控制一级分类是否固定
        isFixed: false,
        desktopTips: app.globalData.desktopTips,
        isIOS: app.globalData.isIOS,
        categoryList: [],
        bannerList: []
    },
    onLoad(options) {
        this.getLocUserInfo()
        if (Object.keys(this.data.userInfo).length > 0) {
            this.requestAgain()
        } else {
            getApp().callBack = (res) => {
                this.getLocUserInfo()
                this.requestAgain()
            }
        }
        let {
            desktopTips
        } = app.globalData
        if (desktopTips) {
            setTimeout(() => {
                this.setData({
                    desktopTips: false
                })
                wx.setStorage({
                    key: "preDesktopTime",
                    data: new Date()
                })
            }, 6000)
        }
    },
    onShow() {
        if (typeof this.getTabBar === 'function') {
            this.getTabBar().setData({
                selected: 3
            })
        }
    },
    onUnload() {
        this.storeBindings.destroyStoreBindings()
    },
    onReachBottom() {
        this.loadMore()
    },
    async getLocUserInfo() {
        this.storeBindings = createStoreBindings(this, {
            store,
            fields: {
                userInfo: 'userInfo'
            },
        })
        this.storeBindings.updateStoreBindings()
    },
    async loadMore() {
        if (!this.data.userInfo.grade) {
            return
        }
        if (this.data.currentType == '2') {
            this.localGetData(getHotrecommendList, {
                grade: this.data.userInfo.grade
            })
        } else if (this.data.currentType == '1') {
            let list = await getAuthorityList({
                grade: this.data.userInfo.grade
            })
            this.setData({
                list
            })
        }
    },
    jumpChildClassify({
        currentTarget
    }) {
        let firstInfo = currentTarget.dataset.item
        let params = firstInfo.childList.length > 0 ? `list=${encodeURIComponent(JSON.stringify(firstInfo.childList))}` : `id=${firstInfo.id}`
        wx.navigateTo({
            url: `/pages/childClassify/index?type=class&title=${firstInfo.title}&${params}`,
        })
    },
    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
            })
        }
    },
    async getCategoryList() {
        let grade = this.data.userInfo.grade
        let categoryList = await getCategoryList({
            grade
        })
        this.setData({
            categoryList
        })
    },
    closeDesktop() {
        this.setData({
            desktopTips: false
        })
        wx.setStorage({
            key: "preDesktopTime",
            data: new Date()
        })
    },
    async getBannerList() {
        let bannerList = await getBannerList({
            grade: this.data.userInfo.grade,
        })
        this.setData({
            bannerList,
        })
    },
    requestAgain() {
        this.resetData()
        this.getCategoryList()
        this.getBannerList()
        if (this.data.currentType == 7) {
            this.selectComponent('#activityList').getActivities()
        }
    },
    selectType({
        target
    }) {
        if (target.dataset.type) {
            if (this.selectComponent('#worksList')) {
                this.selectComponent('#worksList').resetAudio()
            }
            this.setData({
                currentType: target.dataset.type,
                list: [],
            })
            if (target.dataset.type != '7') {
                this.resetData()
            }
        }
    },
    onUnload() {
        this.storeBindings.destroyStoreBindings()
    },
})