import behavior from '~/mixins/video'
import share from '~/mixins/share'
import {
  getreadInfo
} from '~/api/video'
import {
  getPkRecord
} from '~/api/works'
Page({
  behaviors: [behavior, share],
  data: {
    videoInfo: '',
    videoId: '',
    isShare: false,
    recordList: [],
    // 播放的音频
    audioPath: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options, 'options');
    let videoId = options.videoId
    this.setData({
      videoId,
      isShare: options.isShare
    })
    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
    })
  },
  onUnload() {
    this.resetAudio()
    this.setData({
      currentId: ''
    })
  }
})