const app = getApp()
import {
    setUserInfo
} from '~/api/user'
import {
    storeBindingsBehavior
} from 'mobx-miniprogram-bindings'
import {
    store
} from '~/store/index'
let timer
Component({
    // 自动绑定
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        fields: {
            userInfo: 'userInfo'
        },
        actions: {
            setUser: 'setUser'
        }
    },
    properties: {
        title: {
            type: String,
            value: '朗读小咖秀',
        },
        showNav: {
            type: Boolean,
            value: true
        }
    },
    data: {
        navBarHeight: app.globalData.navBarHeight,
        menuRight: app.globalData.menuRight,
        menuTop: app.globalData.menuTop,
        menuHeight: app.globalData.menuHeight,
        isGradeShow: false,
        temporaryGrade: null
    },
    pageLifetimes: {
        show() {
            if (!this.data.userInfo.grade) {
                timer = setInterval(() => {
                    if (this.data.userInfo.uid && !this.data.userInfo.grade) {
                        this.showGrade()
                        clearInterval(timer)
                    } else if (this.data.userInfo.uid && this.data.userInfo.grade) {
                        clearInterval(timer)
                    }
                }, 500)
            }
        },
        hide() {
            clearInterval(timer)
        }
    },
    methods: {
        closeGrade() {
            if (!this.data.userInfo.grade) {
                return
            }
            this.setData({
                isGradeShow: false,
            })
            if (typeof this.getTabBar === 'function') {
                this.getTabBar().setData({
                    mask: false
                })
            }
        },
        // 选择年级
        selectGrade({
            target
        }) {
            let code = target.dataset.code
            if (!code) {
                return
            }
            this.setData({
                temporaryGrade: code
            })
        },
        showGrade() {
            if (typeof this.getTabBar === 'function') {
                this.getTabBar().setData({
                    mask: true
                })
            }
            if (this.data.isGradeShow) {
                return
            }
            this.setData({
                isGradeShow: true,
                temporaryGrade: this.data.userInfo.grade
            })
        },
        // 修改年级
        async changeGrade(e) {
            const grade = this.data.temporaryGrade
            if (!grade) {
                return wx.showToast({
                    title: '请选择年级',
                    icon: 'none',
                    duration: 2000
                })
            }
            let res = await setUserInfo({
                grade
            }, 'put')
            this.setUser(res)
            this.closeGrade()
            setTimeout(() => {
                this.triggerEvent('reload')
            }, 300)
        },
        agree(e) {
            console.log("用户同意隐私授权, 接下来可以调用隐私协议中声明的隐私接口")
        },
    }
})