// packageC/video_goods/VideoReward/VideoReward.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    active: 0,
    allData: {},
    member: {},
    rewards: {},
    name: {},
    rewards_type: 1,
    recordsList: [],
    amount_id: "",
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkrouter();
    this.getStatistic();
    this.getData();
  },
  checkrouter() {
    try {
      // 推广中心是否开启该功能，没开启跳转到指定路径
      let basic_info = wx.getStorageSync("yz_basic_info");
      let stop_info = basic_info.popularize_page.mini.vue_route;
      for (let i = 0; i < stop_info.length; i++) {
        if (stop_info[i] == "VideoReward") {
          // console.log(basic_info.popularize_page.mini.mini_url + "跳转的路径");
          // let suburl = basic_info.popularize_page.mini.mini_url.substr(1);

          wx.showToast({
            title: "未开启推广权限",
            duration: 1000,
            icon: "none",
            success: function () {
              setTimeout(() => {
                wx.redirectTo({
                  url: basic_info.popularize_page.mini.mini_url,
                });
              }, 1000);
            },
          });
          return;
        }
      }
    } catch (e) {
      console.log(e);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({ language: language.en });
  },

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

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

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getStatistic() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.video-share.frontend.bonus.statistic"
    );
    app._getNetWork({
      url: urlStr,
      data: {},
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            rewards: res.data,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.video-share.frontend.bonus.getList"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        page: 1,
      },
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          that.data.isLoadMore = true;
          that.data.total_page = res.data.list.last_page;
          if (!that.data.total_page) {
            that.data.total_page = 0;
          }
          that.setData({
            recordsList: res.data.list.data,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res.msg);
      },
    });
  },
  //获取更多数据
  getMoreData() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.video-share.frontend.bonus.getList"
    );
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      app._getNetWork({
        url: urlStr,
        data: {
          page: that.data.page,
        },
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            var myData = res.data.list.data;
            that.setData({
              recordsList: that.data.recordsList.concat(myData),
            });
          } else {
            that.data.page = that.data.page - 1;
            that.data.isLoadMore = false;
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    }
  },
});
