var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    activeName: "first",
    recordsList: [],
    dataInfo: {},
    love_name: "",
    play_with: [],
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    current_page: 0,
    last_page: 0, //总页数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkrouter();
    this.getData(); // 初始化数据
  },
  onChange(event) {
    this.setData({
      activeName: event.detail.name
    })
    this.data.activeName == 'first'?this.getData():this.getPlayWhit()
  },
  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] == "lovePeriod") {
          console.log(basic_info.popularize_page.mini.mini_url + "跳转的路径");

          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() {
    if (this.data.current_page < this.data.last_page) {
      wx.showLoading({
        title: '加载中',
        mask: true,
      });
      this.setData({
        current_page: this.data.current_page + 1
      });
      this.getMoreData();
    } else {
      wx.showToast({
        title: '没有更多记录',
        icon: 'none'
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  //奖励记录
  getData() {
    let urlStr = app.getNetAddresss("plugin.period-return.frontend.log.index");
    urlStr += "&page=1";
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          this.setData({
            dataInfo: response.data,
            love_name: response.data.love_name,
            isLoadMore: true,
            total_page: response.data.list.last_page,
            recordsList: response.data.list.data,
          });
          wx.setNavigationBarTitle({
            title: this.data.love_name + "周期奖励记录",
          });
        } else {
          wx.showToast({
            title: response.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  }, //待打款记录
  getPlayWhit() {
    let urlStr = app.getNetAddresss("plugin.period-return.frontend.log.payment-log");
    urlStr += "&page=1";
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let data = res.data.data;
        if (res.data.result == 1) {
          this.setData({
            play_with: data.list.data,
            isLoadMore: true,
            current_page:data.list.current_page,
            last_page: data.list.last_page,
          });
          wx.setNavigationBarTitle({
            title:res.data.data.love_name + "周期奖励记录"
          });
        } else {
          app.tips(res.data.msg)
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  getMoreData() {
    this.setData({
      isLoadMore: false,
    });
    let url = '';
    if (this.data.activeName == 'first') {
      url = "plugin.period-return.frontend.log.index"
    } else {
      url = "plugin.period-return.frontend.log.payment-log"
    }
      let urlStr = app.getNetAddresss(url);
      urlStr += "&page=" + this.data.current_page;
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          var response = resdata.data;
          if (response.result == 1) {
            wx.hideLoading();
            this.setData({
              isLoadMore: true,
            })
            if (this.data.activeName == "first") {
              this.setData({
                recordsList: this.data.recordsList.concat(
                  response.data.list.data
                ),
              })
            } else {
              this.setData({
                play_with: this.data.play_with.concat(
                  response.data.list.data
                ),
              });
            }
          } else {
            wx.hideLoading();
            wx.showToast({
              title: response.msg,
              icon: "none",
            });
            this.setData({
              current_page: this.data.current_page - 1,
              isLoadMore: false,
            });
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
  
  },
});