// packageI/donation_project/donationProjectVideo/donationProjectVideo.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    listData: [],
    networkLoading: false,

    video_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id && options.id>0){
      wx.setNavigationBarTitle({
        title: '个人捐赠项目视频详情',
      });
    }
    this.getListData();
  },

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

  },

  
  initData() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      networkLoading: false,
    });
  },
  getListData() {

    this.initData();
    let urlStr = app.getNetAddresss("plugin.present-project.frontend.project.getProjectAll");
    let json={};
    if (this.options.id && this.options.id != 0) {
      urlStr = app.getNetAddresss("plugin.present-project.frontend.project.getPersonAll");
      json.id = this.options.id;
    }
    if (this.options.time && this.options.time != "") {
      json.time = this.options.time;
    }
    app._postNetWork({
      url: urlStr,
      data:json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result != 1) return app.tips(res.msg);
        this.data.total_page = res.data.list.last_page;
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }
        let video_list = [];
        res.data.list.data.forEach(element => {
          element.change_video.forEach(item => {
            let o = { name: element.title, url: item, id: element.id };
            video_list.push(o);
          });
        });
        this.setData({
          video_list,
          networkLoading: true,
        });
      },
    });
  },

  //加载更多数据
  _getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      // that.loading = true;
      return;
    } else {
      this.data.page += 1;
      let urlStr = app.getNetAddresss("plugin.present-project.frontend.project.getProjectAll");
      let json = {
        page: this.data.page
      };
      if (this.options.id && this.options.id != 0) {
        urlStr = app.getNetAddresss("plugin.present-project.frontend.project.getPersonAll");
        json.id = this.options.id;
      }
      if (this.options.time && this.options.time != "") {
        json.time = this.options.time;
      }

      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            let nextPageData = res.data.list.data;
            let video_list = this.data.video_list;
            nextPageData.forEach(element => {
              element.change_video.forEach(item => {
                let o = { name: element.title, url: item, id: element.id };
                video_list.push(o);
              });
            });
            this.setData({
              video_list
            });
            
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        },
      });
    }
  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log("没有更多数据");
    }
  },

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

  }
});