import {
  getSelfRead
} from '~/api/user'
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: ''
  },
  onLoad(options) {
    console.log(options);
    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() {
    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()
  },
})