import behavior from '~/mixins/video'
import event from '~/mixins/event'
import share from '~/mixins/share'
import {
    getreadInfo
} from '~/api/video'
import {
    getPkRecord
} from '~/api/works'
Page({
    behaviors: [behavior, share,event],
    data: {
        videoInfo: '',
        videoId: '',
        isShare: false,
        recordList: [],
        // 播放的音频
        audioPath: ''
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let videoId = null
        let params = decodeURIComponent(options.scene).split('&')
        videoId = !options.scene ? options.videoId : params[0]
        this.setData({
            videoId,
            isShare: !options.scene ? options.isShare : true
        })
        this.getreadInfo(videoId)
        this.getPkRecord()
    },
    async getreadInfo(videoId) {
        let videoInfo = await getreadInfo(videoId)
        wx.setNavigationBarTitle({
            title: !this.data.isShare ? videoInfo.userRead.title : '推荐'
        })
        if (videoInfo.userReadExtend.resourcesType == 1) {
            this.playAudio({
                currentTarget: {
                    dataset: {
                        id: videoId,
                        audio: videoInfo.userRead.audioPath
                    }
                }
            })
        }
        this.setData({
            videoInfo,
            audioPath: videoInfo.userRead.audioPath,
            currentId: videoId
        })
    },
    // pkPage页面示范朗读
    pkPageAudio({
        detail
    }) {
        this.setData({
            audioPath: detail.currentTarget.dataset.audio,
        })
        this.playAudio(detail)
    },
    async getPkRecord() {
        let recordList = await getPkRecord({
            userReadId: this.data.videoId
        })
        this.setData({
            recordList
        })
    },
    jumpUserInfo({
        currentTarget
    }) {
        wx.navigateTo({
            url: `/pages/personal/index?uid=${currentTarget.dataset.uid}&type=user`,
        })
    },
    jumpIndex() {
        wx.switchTab({
            url: '/pages/index/index',
        })
    },
    addCommentNum() {
        this.setData({
            ['videoInfo.userRead.commentAmount']: ++this.data.videoInfo.userRead.commentAmount
        })
    },
    /*   onShareTimeline: function () {
        return {
          title: '终于找到适合孩子的朗读神器了!动画配音,边玩边学!',
          query: `uid=${wx.getStorageSync('uid')}&videoId=1676021823219008`,
          imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/yuwen.jpg'
        }
      }, */
    onUnload() {
        this.resetAudio()
        this.setData({
            currentId: ''
        })
    }
})