// packageD/livePlayer/livePlayer.js
var app = getApp();
var colOneHeight = 0;
var colTwoHeight = 0;
// var width = 0;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    recordsList: [],
    activitys1: [],
    activitys2: [],
    showLoading: false,
    imgCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLive();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

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

  onImageError(options) {
    let index = options.target.dataset.index;
    let seltype = options.target.dataset.seltype;
    // let currentId = options.currentTarget.id;
    console.log("图片加载发生错误");
    this.onImageLoad({
      target: {
        dataset: {
          index: index,
          seltype: seltype,
        },
      },
      detail: {
        width: 2340,
        height: 1080,
      },
    });
  },

  gopage(e) {
    let roomId = e.currentTarget.dataset.id;
    let value = wx.getStorageSync("yz_uid");
    let customParams = { mid: value };
    wx.navigateTo({
      url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomId}&open_share_ticket=1&custom_params=${encodeURIComponent(JSON.stringify(customParams))}`,
    });
  },

  getLive() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.appletslive.frontend.controllers.live.getRoom");
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            recordsList: res.data != null ? res.data : [],
          });
        } else {
          console.log(res.msg);
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  /**
   * 图片加载函数
   * 图片加载完成后，根据图片的高度，依次往第一列和第二列中推入数据
   */
  onImageLoad(options) {
    let index = options.target.dataset.index;
    let imageWidth = options.detail.width; // 图片宽度
    let imageHeight = options.detail.height; // 图片高度

    let activitys = this.data.recordsList; // 获取所有的数据资源
    let activityObj = null;
    let scle = imageWidth / imageHeight;

    activityObj = activitys[index];

    this.data.imgCount += 1;
    let activitys1 = this.data.activitys1;
    let activitys2 = this.data.activitys2;

    if (colOneHeight <= colTwoHeight) {
      colOneHeight += 176 / scle + 60;
      activitys1.push(activityObj);
    } else {
      colTwoHeight += 176 / scle + 60;
      activitys2.push(activityObj);
    }

    let data = {
      activitys1: activitys1,
      activitys2: activitys2,
    };

    if (this.data.imgCount >= this.data.recordsList.length) {
      this.setData({
        showLoading: true,
      });
    }

    this.setData(data);
  },

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareTimeline: function () {
    var value = wx.getStorageSync("yz_uid");
    // var path = "";
    if (value) {
      mid = value;
    }
    return {
      title: "直播间列表",
      query: "mid=" + mid,
    };
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var value = wx.getStorageSync("yz_uid");
    var path = "";
    if (value) {
      path = value;
    }

    let _title = "直播间列表";

    let _link = "/packageD/livePlayer/livePlayer?mid=" + path;

    return {
      title: _title,
      path: _link,
      success: function (shareTickets) {
        console.info(shareTickets + "成功");
        // 转发成功
      },
      fail: function (res) {
        console.log(res + "失败");
        // 转发失败
      },
    };
  },
});
