// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    level_name: "",
    rate: 0,
    ratioDatas: [],
    activeName: "first",
    first_content: [],
    second_content: [],
    third_content: [],
    fourth_content: [],
    dividend: {},
    mailLanguage: "", // 全局语言包设置
    area_dividend_title: '区域代理',
    dividend_amount: "佣金", // 佣金名称
    area_dividend: "", // 区域分红名字
    is_first_content: false,
    is_second_content: false,
    is_third_content: false,
    is_fourth_content: false,
    page: 1,
    showNone: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // if (basic_info.popularize_page.mini.vue_route)

    this.cheackRouter();
    this.getData(); // 初始化数据
    this.initInfo(); // 初始化info数据
    this.getAreaDividend();
    this.getDividendList("");
    try {
      let mailLanguage = JSON.parse(wx.getStorageSync('mailLanguage'));
      if (mailLanguage && mailLanguage.area_dividend) {
        let _title = mailLanguage.area_dividend.title || '区域代理';
        wx.setNavigationBarTitle({
          title: _title + '中心'
        });
        this.setData({
          area_dividend_title: _title
        });
      }
    } catch (e) {
      // console.log(e)
    }
  },
  cheackRouter() {
    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] == "AgentBonus") {
          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: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getAreaDividend() {
    let urlStr = app.getNetAddresss(
      "plugin.area-dividend.api.area-dividend.getAreaDividend"
    );
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            dividend: resdata.data,
          });
          console.log(this.data.dividend, "数组");
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  initInfo() {
    let urlStr = app.getNetAddresss(
      "plugin.area-dividend.api.area-dividend.get-area-agent"
    );
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            level_name: resdata.data.level_name,
            rate: resdata.data.rate,
            agent_many: resdata.data.agent_many
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  // 初始化数据
  getData() {
    let urlStr = app.getNetAddresss(
      "plugin.area-dividend.api.area-dividend.get-dividend-statistic"
    );
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          // 组装数据
          let itemDay = {};
          itemDay.money = resdata.data.today;
          itemDay.data = "今日";
          itemDay.name = "data";
          let data = [];
          data.push(itemDay);
          this.setData({
            ratioDatas: data,
          });
          let itemYesterday = {};
          itemYesterday.money = resdata.data.yesterday;
          itemYesterday.data = "昨日";
          itemYesterday.name = "data";
          data.push(itemYesterday);
          this.setData({
            ratioDatas: data,
          });

          let itemWeek = {};
          itemWeek.money = resdata.data.this_week;
          itemWeek.data = "本周";
          itemWeek.name = "data";
          data.push(itemWeek);
          this.setData({
            ratioDatas: data,
          });
          let itemMonth = {};
          itemMonth.money = resdata.data.this_month;
          itemMonth.data = "本月累计分红";
          itemMonth.name = "mounth";
          data.push(itemMonth);
          this.setData({
            ratioDatas: data,
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  // 获取佣金列表
  getDividendList(status) {
    console.log(status);
    let urlStr = app.getNetAddresss(
      "plugin.area-dividend.api.area-dividend.get-dividend-list"
    );
    urlStr += "&status=" + status;
    urlStr += "&page=" + this.data.page;
    let that = this;
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        console.log(resdata.data.data.data);
        if (resdata.result === 1) {
          that.setData({
            allContent: resdata.data.data.data
          });
          let datas = [];
          let time_list = resdata.data.time;
          time_list.forEach((item, index) => {
            datas.push({
              create_month: item.create_month,
              data: [],
              total: 0,
              current_page: 1,
              last_page: 1,
              is_loading: 0,
              is_show: 0
            });
          });
          console.log(datas[0], 'sssss')
          if (datas[0]) {
            datas[0].data = resdata.data.data.data;
            datas[0].total = resdata.data.data.total;
            datas[0].last_page = resdata.data.data.last_page;
            datas[0].current_page = resdata.data.data.current_page;
            datas[0].is_loading = 1;
            datas[0].is_show = 1;
          }

          console.log(datas);
          that.setData({
            datas: datas
          });
          if (resdata.data.data.current_page >= resdata.data.data.last_page) {
            that.setData({
              showNone: false,
            });
          } else {
            that.setData({
              showNone: true,
            });
          }
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getOtherList(e) {
    let that = this;
    let index = e.currentTarget.dataset.ind;
    let date = e.currentTarget.dataset.item;
    if (that.data.datas[index].is_show == 1) {
      that.data.datas[index].is_show = 0;
      console.log('隐藏数据', that.data.datas);
      that.setData({
        datas: that.data.datas
      });
      return;
    } else {
      that.data.datas[index].is_show = 1;
      that.setData({
        datas: that.data.datas
      });
      //已经加载首页数据
      if (that.data.datas[index].is_loading == 1) {
        console.log('已经加载首页数据');
        return;
      }
    }
    let json = {
      status: that.data.nextStatus ? that.data.nextStatus : '',
      page: 1,
      create_month: date
    };
    let urlStr = app.getNetAddresss(
      "plugin.area-dividend.api.area-dividend.get-dividend-list"
    );
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (res) => {
        let resdata = res.data;
        console.log(resdata.data.data.data);
        if (resdata.result === 1) {
          that.setData({
            ['datas[' + index + '].data']: resdata.data.data.data,
            ['datas[' + index + '].total']: resdata.data.data.total,
            ['datas[' + index + '].last_page']: resdata.data.data.last_page,
            ['datas[' + index + '].current_page']: resdata.data.data.current_page,
            ['datas[' + index + '].is_loading']: 1,
            ['datas[' + index + '].is_show']: 1,
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  // 佣金列表分页
  getNextDividendList(e) {
    var that = this;
    let date = e.currentTarget.dataset.item.create_month;
    let index = e.currentTarget.dataset.ind;
    let pages = that.data.datas[index].current_page + 1;
    console.log(that.data.datas);
    let json = {
      status: that.data.nextStatus ? that.data.nextStatus : '',
      page: pages,
      create_month: date
    };
    // let pages = this.data.page + 1;
    // this.setData({
    //   page: pages,
    // });
    // let status = this.data.nextStatus ? this.data.nextStatus : "";
    let urlStr = app.getNetAddresss(
      "plugin.area-dividend.api.area-dividend.get-dividend-list"
    );
    // let one = [];
    // urlStr += "&status=" + status;
    // urlStr += "&page=" + that.data.page;
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (res) => {
        let resdata = res.data;
        console.log(resdata.data.data.data);
        if (resdata.result === 1) {
          that.setData({
            ['datas[' + index + '].data']: that.data.datas[index].data.concat(resdata.data.data.data),
            ['datas[' + index + '].current_page']: resdata.data.data.current_page
          });
          console.log(that.data.datas[index]);
          // if (status == "") {
          //   that.setData({
          //     first_content: that.data.first_content.concat(
          //       resdata.data.data.data
          //     ),
          //   });
          // } else if (status == "1") {
          //   this.setData({
          //     second_content: that.data.second_content.concat(
          //       resdata.data.data.data
          //     ),
          //   });
          // } else if (status == "0") {
          //   this.setData({
          //     third_content: that.data.third_content.concat(
          //       resdata.data.data.data
          //     ),
          //   });
          // } else if (status == "-1") {
          //   this.setData({
          //     fourth_content: that.data.fourth_content.concat(
          //       resdata.data.data.data
          //     ),
          //   });
          // }
          // if (resdata.data.data.current_page >= resdata.data.data.last_page) {
          //   that.setData({
          //     showNone: false,
          //   });
          // } else {
          //   that.setData({
          //     showNone: true,
          //   });
          // }
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  // 输入框被改变的时候
  onChange(val) {
    console.log(val.detail.index, "555");
    let status = "";
    if (val.detail.index == 1) {
      status = "1";
    } else if (val.detail.index == 2) {
      status = "0";
    } else if (val.detail.index == 3) {
      status = "-1";
    }
    this.setData({
      nextStatus: status,
    });
    this.getDividendList(status);
  },
  hideShowPopup() {
    this.setData({
      showPopup: false
    });
  },
  toSelectionarea() {
    wx.navigateTo({
      url: '/packageB/member/income/applyRegionalAgency/applyRegionalAgency',
    });
  },
  showPopupTap() {
    wx.showLoading({
      title: '加载中',
    });
    let urlStr = app.getNetAddresss("plugin.area-dividend.api.area-dividend.get-all-area-agent");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (resdata.result === 1) {
          this.setData({
            showPopup: true,
            moreList: resdata.data
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        wx.hideLoading({
          success: (res) => {},
        });
        console.log(res);
      },
    });
  },
});