import {
    getSelfRead,
    getFavoritesList
} from '~/api/user'
import {
    getFollowWorks
} from '~/api/works'
import {
    getreadInfo
} from '~/api/video'
import share from '~/mixins/share'
import event from '~/mixins/event'
import reachBottom from '~/mixins/reachBottom'
Page({
    behaviors: [reachBottom, share, event],
    data: {
        firstWork: '',
        type: 'my',
        emptyText: '您还没有作品哦，赶快去发表吧！'
    },
    onLoad(options) {
        if (options.type && options.type != 'my') {
            let type = options.type
            let emptyText = ''
            let title = ''
            if (type == 'follow') {
                emptyText = '您还没有关注用户哦~'
                title = '关注作品'
            } else if (type == 'like') {
                emptyText = '还没有点赞记录哦~'
                title = '点赞'
            } else if (type == 'collect') {
                emptyText = '快去收藏喜欢的作品吧！'
                title = '收藏作品'
            } else if (type == 'comment') {
                emptyText = '还没有收到评论哦~'
                title = '作品评论'
            }
            this.setData({
                type,
                emptyText
            })
            wx.setNavigationBarTitle({
                title
            })
        }
        if (options.id) {
            this.getreadInfo(options.id)
            wx.nextTick(() => {
                this.selectComponent('#worksList').openTypeComment({
                    target: {
                        dataset: {
                            type: options.type,
                            onceId: options.onceId,
                            id: options.id
                        }
                    }
                })
            })
        } else {
            this.loadMore()
        }
    },
    loadMore() {
        if (this.data.type == 'follow') {
            this.getData(getFollowWorks, {})
        } else if (this.data.type == 'collect') {
            this.getData(getFavoritesList, {})
        } else {
            console.log('zzz');
            this.getData(this.getSelfRead, {})
        }
    },
    getSelfRead(data) {
        return new Promise(async (reslove) => {
            let res = await getSelfRead(data)
            if (this.data.firstWork) {
                res.list = res.list.filter(item => {
                    return item.userRead.id != this.data.firstWork.userRead.id
                })
                res.list.unshift(this.data.firstWork)
            }
            reslove(res)
        })
    },
    async getreadInfo(videoId) {
        let firstWork = await getreadInfo(videoId)
        this.setData({
            firstWork
        })
        this.loadMore()
    },
    onReachBottom() {
        this.loadMore()
    },
})