const app = getApp()
import {
    getCategoryList,
    getResourceList
} from "~/api/works"
import {
    getBannerList,
    setSubscribe,
} from '~/api/global'
import event from '~/mixins/event'
import share from '~/mixins/share'
import {
    createStoreBindings
} from 'mobx-miniprogram-bindings'
import {
    store
} from '~/store/index'
Page({
    behaviors: [share, event],
    data: {
        navBarHeight: app.globalData.navBarHeight,
        desktopTips: app.globalData.desktopTips,
        bannerList: [],
        categoryList: [],
        listOptions: {},
        subscribeShow: false,
        tmplIds: []
    },
    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: 2
            })
        }
    },
    requestAgain() {
        this.getBannerList()
        this.getResource()
        this.getCategoryList()
        this.getSubscribe()
    },
    async getLocUserInfo() {
        this.storeBindings = createStoreBindings(this, {
            store,
            fields: {
                userInfo: 'userInfo'
            },
        })
        this.storeBindings.updateStoreBindings()
    },
    async getCategoryList() {
        let grade = this.data.userInfo.grade
        let categoryList = await getCategoryList({
            grade
        })
        this.setData({
            categoryList
        })
    },
    async getResource() {
        let data = await getResourceList({
            grade: this.data.userInfo.grade
        })
        this.setData({
            listOptions: data,
        })
    },
    async getBannerList() {
        let bannerList = await getBannerList({
            grade: this.data.userInfo.grade,
        })
        this.setData({
            bannerList,
        })
    },
    jumpChildClassify({
        currentTarget
    }) {
        let firstInfo = currentTarget.dataset.item
        wx.navigateTo({
            url: `/pages/childClassify/index?type=class&title=${firstInfo.title}&id=${firstInfo.id}`,
        })
    },
    showTips() {
        wx.showModal({
            title: '新栏目更新中',
            content: '敬请期待….',
            showCancel: false,
            confirmColor: '#333333',
            success(res) {}
        })
    },
    closeDesktop() {
        this.setData({
            desktopTips: false
        })
        wx.setStorage({
            key: "preDesktopTime",
            data: new Date()
        })
    },
    jumpSearch() {
        wx.navigateTo({
            url: '/pages/childClassify/index?type=search',
        })
    },
    async getSubscribe() {
        let tmplIds = await setSubscribe()
        this.setData({
            tmplIds: tmplIds ? tmplIds : [],
            subscribeShow: tmplIds ? true : false,
        })
    },
    requestMessage() {
        wx.requestSubscribeMessage({
            tmplIds: this.data.tmplIds,
            success: async (res) => {
                let accept = []
                this.data.tmplIds.forEach(item => {
                    if (res[item] == 'accept') {
                        accept.push(item)
                    }
                })
                await setSubscribe({
                    ids: accept
                }, 'post')
                this.getSubscribe()
            },
            fail: async (err) => {
                await setSubscribe({
                    ids: []
                }, 'post')
                this.getSubscribe()
                /*  console.log(err);
                 if (err.errCode == '20004') {
                     // 20004
                     wx.showModal({
                         title: '温馨提示',
                         content: '请同意允许我们向您发送订阅信息，请打开设置勾选订阅消息，这样能随时接到关于您作品的最新消息',
                         complete: (res) => {
                             if (res.cancel) {}
                             if (res.confirm) {}
                         }
                     })
                 } */
            }
        })
    },
    onUnload() {
        this.storeBindings.destroyStoreBindings()
    },
})