// pages/broadcastList/broadcastList.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    liveUrl: app.liveUrl,
    liveRecordUrl: app.liveRecordUrl,
    liveRecordSrc: null,
    livePlanId: '',
    liveRecoreList: [],
    videoContext: null,
    currentPage: 1,
    rows: 20,
    isPlay: false
  },
  getLivePlanRecordList: function(page) {
    var self = this;
    app.dialog.loading('正在加载');
    app.restAjax.get(app.restAjax.path('{liveUrl}/app/liverecord/listpageliverecordrelease/{planId}', [self.data.liveUrl, self.data.livePlanId]), {
      page: page,
      rows: self.data.rows
    }, null, function(code, data) {
      if(data.rows.length == 0) {
        app.dialog.msg('暂无数据');
        return;
      }
      var liveRecoreArray;
      if(page <= 1) {
        liveRecoreArray = data.rows;
      } else {
        liveRecoreArray = self.data.liveRecoreList;
        liveRecoreArray = liveRecoreArray.concat(data.rows);
      }
      self.setData({
        currentPage: page,
        'liveRecoreList': liveRecoreArray
      })
    }, function(code, data) {
      app.dialog.msg(data.msg);
    }, function() {
      wx.stopPullDownRefresh();
      wx.hideLoading();
    })
  },
  doChangeSrc: function(event) {
    var self = this;
    self.setData({
      liveRecordSrc: event.currentTarget.dataset.liveRecordSrc
    })
    self.videoContext.play({
      success: function() {
        self.setData({
          isPlay: true
        })
      }
    });
  },
  onPlayStateChange: function(event) {
    console.log(event.detail.code)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.videoContext = wx.createLivePlayerContext('liveRecordVideo');
    this.setData({
      livePlanId: options.livePlanId
    })
    this.getLivePlanRecordList(1);
  },
  doPlay: function() {
    var self = this;
    if(!self.data.liveRecordSrc) {
      if(self.data.liveRecoreList.length == 0) {
        app.dialog.msg('无播放内容');
        return;
      }
      self.setData({
        liveRecordSrc : self.data.liveRecoreList[0].recordFilePath
      });
      self.videoContext.play({
        success: function() {
          self.setData({
            isPlay: true
          })
        }
      });
    } else {
      self.videoContext.play({
        success: function() {
          self.setData({
            isPlay: true
          })
        }
      });
    }
  },
  doStop: function() {
    var self = this;
    self.videoContext.pause({
      success: function() {
        self.setData({
          isPlay: false
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getLivePlanRecordList(1);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getLivePlanRecordList(this.data.currentPage + 1);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})