// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
var currentTabIndex = "0";
// 标记 防止重复加载
var is_first_content = false;
var is_second_content = false;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    is_first_content: false,
    is_second_content: false,
    mailLanguage: "",
    mailFullReturnTitle: "",
    mailFullReturnName: "",
    activeName: "first",
    activeName1:"0",
    first_content: [],
    second_content: [],
    alreadyReturn: 0.0,
    each_return_rate: 0,
    order_return_rate: 0,
    returnAmountTotal: 0.0,
    totay:"0",
    valid:"0",
    task:false,
    notReturn: 0.0,
    fullReturnName: "",
    isShow: false,
    return_list_name: "",
    httpUrl:app.getNetAddresss("plugin.single-return.api.single-return.get-single-return-queue"),
    loading:false,
    logPage:1,
    isLoadMore:false,
    noMore:false,
    logPage:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkrouter();
    this.initMailLanguage();
    this.getData(); // 初始化数据
    // this.setDataByTabIndex();
    this.getListData();
  },
  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] == "single_return_center") {
          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 () {
    if(this.data.isLoadMore) this.getMoreData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  initMailLanguage() {
    let mailLanguage;
    let urlStr = app.getNetAddresss("setting.get-lang-setting");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          mailLanguage = resdata.data;
          let mailSingleReturnTitle = app.setMailLanguage(
            "消费赠送",
            mailLanguage.single_return.single_return
          );
          let mailSingleReturnName = app.setMailLanguage(
            "赠送",
            mailLanguage.single_return.return_name
          );
          let mailReturnQueue = app.setMailLanguage(
            "消费队列",
            mailLanguage.single_return.return_queue
          );
          let mailReturnLog = app.setMailLanguage(
            "消费记录",
            mailLanguage.single_return.return_log
          );
          this.setData({
            mailSingleReturnTitle,
            mailSingleReturnName,
            mailReturnQueue,
            mailReturnLog,
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  // 初始化数据
  getData() {
    let urlStr = app.getNetAddresss(
      "plugin.single-return.api.single-return.get-single-return-statistic"
    );
    app._getNetWork({
      url: urlStr,
      success: ({data:{data,result,msg}}) => {
        if (result === 1) {
          this.setData({
            alreadyReturn: data.alreadyReturn,
            each_return_rate: data.each_return_rate,
            order_return_rate: data.order_return_rate,
            returnAmountTotal: data.returnAmountTotal,
            notReturn: data.notReturn,
            totay:data.totay,
            valid:data.valid,
            task:data.task
          });
        } else app.tips(msg);
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  // 返现队列详情
  toReturnInfo(val) {
    var id = val.currentTarget.dataset.id || val.target.dataset.id;
    wx.navigateTo({
      url:
        "/packageB/member/income/single_return_list_info/single_return_list_info" +
        "?id=" +
        id,
    });
  },
  getReturnList() {
    let urlStr = app.getNetAddresss(
      "plugin.single-return.api.single-return.get-single-return-queue"
    );
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            first_content: resdata.data,
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  // 获取返现
  getReturnListInfo() {
    let urlStr = app.getNetAddresss(
      "plugin.single-return.api.single-return.get-single-return-log"
    );
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resdata = res.data;
        if (resdata.result === 1) {
          this.setData({
            second_content: resdata.data,
          });
        } else {
          wx.showToast({
            title: resdata.msg,
            icon: "none",
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },

  // 设置选择后的数据
  setDataByTabIndex() {
    if (currentTabIndex == "0" && !is_first_content) {
      is_first_content = true;
      this.getReturnList(); // 获取返现队列
    } else if (currentTabIndex == "1" && !is_second_content) {
      is_second_content = true;
      this.getReturnListInfo(); // 获取返现详情
    }
  },
  handleClick1({detail:{name}}){
    if(name == this.data.activeName1)return false; //控制点击当前选择的选项
    this.setData({
      activeName1:name,
      listData:[],
      logPage:1,
    })
    this.getListData();
  },
  // tab 点击
  handleClick({detail:{name}}) {
    if(name == this.data.avtiveName) return false;//控制点击当前选择的选项
    let httpUrl = app.getNetAddresss("plugin.single-return.api.single-return.get-single-return-log");
    if(name == "first"){
      httpUrl = app.getNetAddresss("plugin.single-return.api.single-return.get-single-return-queue");
    }
    this.setData({
      logPage:1,
      listData:[],
      httpUrl,
      activeName:name
    })
    this.getListData();
    return false
    if (currentTabIndex == tab.detail.index) {
      return;
    } else {
      currentTabIndex = tab.detail.index;
    }
    this.setDataByTabIndex();
  },
  getJson(){
    if(this.data.activeName == "first"){
      return {}
    }else{
      return{
        award_status:this.data.activeName1
      }
    }
  },
  setnoMore(bool){
    this.setData({
      noMore:bool,
      isLoadMore:!bool
    })
  },
  getListData(){
    this.setData({loading:true});
    app._postNetWork({
      url: this.data.httpUrl,
      data:{page:1,...this.getJson()},
      success: ({data:{result,msg,data}}) => {
        this.setData({loading:false});
        if(result == 1){
          this.setData({listData:data.data});
          this.setnoMore(this.data.logPage == data.last_page);
        }else app.tips(msg);
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getMoreData(){
    this.setData({loading:true});
    app._postNetWork({
      url: this.data.httpUrl,
      data:{page:1,...this.getJson()},
      success: ({data:{result,msg,data}}) => {
        this.setData({loading:false});
        if(result == 1){
          this.setData({listData:this.data.listData.concat(data.data)});
          this.setnoMore(this.data.logPage == data.last_page);
        }else app.tips(msg);
      }
    });
  }
});
