import {
    getSelfRead
} from '~/api/user'
import {
    isActivityWork
} from '~/api/works'
import {
    getModelTexts,
    getReadRanking,
    getActivityInfo,
    getSelfReadRanking
} from '~/api/global'
import {
    store
} from '~/store/index'
import {
    createStoreBindings
} from 'mobx-miniprogram-bindings'
import event from '~/mixins/share'
import share from '~/mixins/share'
Page({
    behaviors: [share,event],
    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        // true是人气榜,false是参赛作品
        currentType: true,
        activityUserList: [],
        modelList: [],
        myActivityUser: {},
        activityId: '',
        bannerList: [],
        //1是收费0是免费
        free: 1
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            activityId: options.activityId
        })
        getActivityInfo(options.activityId).then(res => {
            let {
                free
            } = res
            this.setData({
                free
            })
        })
        this.getLocUserInfo()
        if (Object.keys(this.data.userInfo).length > 0) {
            this.reload()
        } else {
            getApp().callBack = (res) => {
                this.getLocUserInfo()
                this.reload()
            }
        }
    },
    getLocUserInfo() {
        this.storeBindings = createStoreBindings(this, {
            store,
            fields: {
                userInfo: 'userInfo'
            },
            actions: {
                setUser: 'setUser'
            }
        })
        this.storeBindings.updateStoreBindings()
    },
    reload() {
        this.getModelTexts()
        this.getReadRanking()
    },
    // 获取范文
    async getModelTexts() {
        let modelList = await getModelTexts({
            grade: this.data.userInfo.grade,
            activityId: this.data.activityId
        })
        this.setData({
            modelList
        })
    },
    async getReadRanking() {
        let {
            activityUserList,
            myActivityUser,
            activity
        } = await getReadRanking({
            activityId: this.data.activityId
        })
        console.log(activity);
        this.setData({
            activityUserList,
            myActivityUser,
            bannerList: activity.bannerList
        })
    },
    async getSelfReadRanking() {
        let list = await getSelfReadRanking({
            activityId: this.data.activityId
        })
        this.setData({
            list
        })
    },
    bannelEvent({
        target
    }) {
        wx.navigateTo({
            url: `/pages/reading/index?videoId=${target.dataset.id}&activityId=${this.data.activityId}&readingType=readMatch&autoPlay=true&free=${this.data.free}`
        })
    },
    jumpUserInfo({
        currentTarget
    }) {
        wx.navigateTo({
            url: `/pages/personal/index?uid=${currentTarget.dataset.uid}`,
        })
    },
    selectType({
        target
    }) {
        if (target.dataset.type) {
            let currentType = JSON.parse(target.dataset.type)
            if (!currentType) {
                this.getSelfReadRanking()
            }
            this.setData({
                currentType
            })
        }
    },
    jumpIntro({
        currentTarget
    }) {
        let iconDetail = currentTarget.dataset.icondetail
        if (iconDetail) {
            wx.navigateTo({
                url: `/pages/rankIntro/index?img=${iconDetail}`,
            })
        }
    },
})