// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    sType: "",
    level: "",
    return_time: "",
    ratioDatas: [],
    activeName: "first",
    first_content: [],
    second_content: [],
    third_content: [],
    fourth_content: [],
    loading: false,
    allLoaded: false,
    goload: true,
    isLoadMore: true,
    page: 1,
    total_page: 0,
    currentTabIndex: "0",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkrouter();
    this.initInfo(); // 初始化info数据
    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] == "level_return") {
          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 () {
    if (this.data.isLoadMore) {
      this.getMoreData(this.data.page, this.data.sType);
    } else {
      wx.showToast({
        title: "没有更多了",
        icon: "none",
      });
    }
  },

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  // 初始化info数据
  initInfo() {
    let urlStr = app.getNetAddresss(
      "plugin.level-return.api.level-return.get-level-return-statistic"
    );
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            level: resdata.data.level,
            return_time: resdata.data.return_time,
          });
          // 组装数据
          let arr = [];
          let item0 = {};
          item0.money = resdata.data.amountTotal;
          item0.data = "总返还金额";
          item0.name = "data";
          arr.push(item0);
          let item1 = {};
          item1.money = resdata.data.notReturn;
          item1.data = "未返还金额";
          item1.name = "data";
          arr.push(item1);
          let item2 = {};
          item2.money = resdata.data.return_amount;
          item2.data = "已返还金额";
          item2.name = "data";
          arr.push(item2);
          let item3 = {};
          item3.money = resdata.data.invalid;
          item3.data = "无效金额";
          item3.name = "data";
          arr.push(item3);
          this.setData({
            ratioDatas: arr,
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  // 获取数据 空：全部 -1：无效 0：未返现 1：已返现
  getData(type) {
    let urlStr = app.getNetAddresss(
      "plugin.level-return.api.level-return.get-return-log"
    );
    urlStr += "&status=" + type + "&page=1";
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            total_page: resdata.data.last_page,
          });
          if (type == "") {
            this.setData({
              first_content: resdata.data.data,
            });
          } else if (type == "0") {
            this.setData({
              second_content: resdata.data.data,
            });
          } else if (type == "1") {
            this.setData({
              third_content: resdata.data.data,
            });
          } else if (type == "-1") {
            this.setData({
              fourth_content: resdata.data.data,
            });
          }
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  // 获取更多
  getMoreData(page, type) {
    if (this.data.page == this.data.total_page) {
      return;
    }
    if (this.data.page >= this.data.total_page) {
      this.setData({
        loading: true,
        allLoaded: true,
      });
      return;
    } else {
      page = page + 1;
      let urlStr = app.getNetAddresss(
        "plugin.level-return.api.level-return.get-return-log"
      );
      urlStr += "&status=" + type + "&page=" + page;
      app._getNetWork({
        url: urlStr,
        success: (res) => {
          let resdata = res.data;
          if (resdata.result === 1) {
            this.setData({
              loading: false,
              allLoaded: false,
            });
            switch (type) {
            case "":
              this.setData({
                first_content: this.data.first_content.concat(
                  resdata.data.data
                ),
              });
              break;
            case "0":
              this.setData({
                second_content: this.data.second_content.concat(
                  resdata.data.data
                ),
              });
              break;
            case "1":
              this.setData({
                third_content: this.data.third_content.concat(
                  resdata.data.data
                ),
              });
              break;
            case "-1":
              this.setData({
                fourth_content: this.data.fourth_content.concat(
                  resdata.data.data
                ),
              });
              break;

            default:
              break;
            }
          } else {
            let pages = this.data.page - 1;
            this.setData({
              page: pages,
              loading: true,
              allLoaded: true,
              isLoadMore: false,
            });
          }
        },
        fail: function (res) {
          console.log(res);
        },
      });
    }
  },
  // tab 点击
  onChange(val) {
    if (val.detail.index == 0) {
      this.getData("");
    } else if (val.detail.index == 1) {
      this.getData("0");
    } else if (val.detail.index == 2) {
      this.getData("1");
    } else if (val.detail.index == 3) {
      this.getData("-1");
    }
  },
});
