// packageC/video_goods/myVideo/myVideo.js
const app = getApp();
var colOneHeight = 0;
var colTwoHeight = 0;
// var width = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isSelf: true,//是否是本人
    showTab: false,
    is_open: "",
    review: "",
    release: "",
    activeName:'1',
    videoList: [],
    bannerList: [],
    newArray1: [],
    newArray2: [],
    showLoading: false,
    member: {},
    //more
    isLoadMore: false,
    page: 1,
    total_page: 0,
    activitys1:[],
    activitys2:[],
    imgCount:0,
    noload:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData(this.data.activeName);
    if(this.options.uid) {
      this.setData({
        isSelf: false
      })
    }
  },
  handleClick(e){
    this.setData({
      activeName:e.detail.name,
      page:1
    });
    this.getData(this.data.activeName);
  },
  toVideo(e) {
    let item = e.currentTarget.dataset.item;
    let page = this.data.videoList.findIndex((value, index, arr) => {
      return value.id === item.id;
    });
    page = Math.ceil((page + 1) / 20);
    wx.navigateTo({
      url: '/packageC/video_goods/VideoDetail/VideoDetail?vid=' + item.id + '&page=' + page +'&status=' + this.data.activeName + '&uid=' + this.data.member.uid,
    });
  },
  
  onImageLoad (options) {
    let currentId = options.currentTarget.id;
    let imageWidth = options.detail.width; // 图片宽度
    let imageHeight = options.detail.height; // 图片高度
    let activitys = this.data.videoList; // 获取所有的数据资源
    let activityObj = null;
    let scle  = (imageWidth/imageHeight);
    for (let i = 0; i < activitys.length; i++) {
      if (activitys[i].id == currentId) {
        activityObj = activitys[i];
        break;
      }
    }
    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.videoList.length){
      this.setData({
        showLoading : true
      });
    }

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.current_page >= this.data.last_page){
      wx.showToast({
        title: '没有更多了',
        icon:'none',
        duration:1000
      });
    }else{
      let pages = this.data.page + 1;
      this.setData({
        page:pages,
      });
      console.log(this.data.activeName);
      console.log(this.data.page);
      this.getMoreData(this.data.activeName);
    }
  },

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

  },
  getData(sta){
    let that = this;
    let urlStr = app.getNetAddresss("plugin.video-share.frontend.video.my-video");
    app._getNetWork({
      url: urlStr,
      data: {
        v_uid: this.options.uid || '',
        page:that.data.page,
        status:sta
      },
      success: (resdata)=> {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            is_open: !this.options.uid ? res.data.is_open: false,
            member:res.data.member,
            review:res.data.review,
            release:res.data.release,
            current_page:res.data.video.current_page,
            last_page:res.data.video.last_page,
            showLoading:true,
          });
          that.setData({
            activitys1:[],
            activitys2:[],
          });
          colOneHeight = 0;
          colTwoHeight = 0;
          
          that.setData({
            videoList:res.data.video.data,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
        that.setData({
          showTab: true
        });
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  delTap(e){
    console.log(e.currentTarget.dataset.info.id);
    let vid = e.currentTarget.dataset.info.id;
    let that = this;
    let urlStr = app.getNetAddresss("plugin.video-share.frontend.video.del");
    app._getNetWork({
      url: urlStr,
      data: {
        id:vid
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          
          let pages = getCurrentPages();
          let videoList = pages[pages.length-2].data.videoList;
          console.log(videoList);
          videoList.forEach((item,key)=>{
            if(item.id == vid){
              videoList.splice(key,1);
            }
          });  
          pages[pages.length-2].setData({videoList:videoList,activitys1:[],activitys2:[]});

          that.setData({
            noload:false
          });
          wx.showToast({
            title: res.msg,
            icon:'none',
            duration:1000,
            success:function(){
              that.getData(that.data.activeName);
            }
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  getMoreData(sta){
    let that = this;
    let urlStr = app.getNetAddresss("plugin.video-share.frontend.video.my-video");
    app._getNetWork({
      url: urlStr,
      data: {
        v_uid: this.options.uid || '',
        page:that.data.page,
        status:sta
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            is_open:res.data.is_open,
            member:res.data.member,
            review:res.data.review,
            release:res.data.release,
            current_page:res.data.video.current_page,
            last_page:res.data.video.last_page,
            showLoading:true
          });
          
          
          that.setData({
            videoList:that.data.videoList.concat(res.data.video.data),
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
});