const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    love_name: "",
    active: "speed_pool_record",
    amount: 0,
    member_surplus: 0,
    released_rocord: {}, //释放记录
    speed_pool_record: {}, //加速池记录,
    account_balance:{},//plus池
    speed_pool:'speed_pool',
    income:0,
    amount_total:0,//累计收入
    last_active:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    this.getData(); //首页数据
    this.getReleased() //获取释放爱心值(数量) 
    this.getPlusPool();//plus池
  },
  //plus池
  getPlusPool(){
    let urlStr = app.getNetAddresss('plugin.love-speed-pool.frontend.plus.index');
    urlStr += '&page=1'
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result == 1) {
          this.setData({
            amount_total:parseFloat(res.data.data.amount_total),
            account_balance: res.data.data.list
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  //领取释放爱心值
  confirm() {
    let url = app.getNetAddresss('plugin.love-speed-pool.frontend.receive.handle');
    app._getNetWork({
      url,
      success: (res) => {
        wx.showToast({
          icon: "none",
          title: res.data.msg,
          duration: 1500,
        });
      },
      fail: (err) => {
        console.log(err)
      },

    })
  },
  //显示遮罩层
  onClickShow() {
    if (this.data.member_surplus == 0) {
      wx.showToast({
        icon: "none",
        title: `暂无释放的${this.data.love_name}可领取`,
        duration: 1500,
      });
    } else {
      this.setData({
        show: true
      })
    }
  },
  //关闭遮罩层
  onClickHide() {
    this.setData({
      show: false
    })
  },
  onChangeSpeed(event){
    this.setData({
      speed_pool: event.detail.name,
    })
    if(this.data.speed_pool== 'speed_plus_pool'){
      this.setData({
        active:'account_balance',
        last_active:this.data.active,
      })
    }else{
      this.setData({
        active:this.data.last_active,
      })
    }
   
  },
  //tab切换
  onChange(event) {
    this.setData({
      active: event.detail.name
    })
  },
  //跳转商品权益页
  PayHandler() {
    wx.navigateTo({
      url: '/packageB/member/category/search_v2/search_v2?CommodityRights=CommodityRights',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  getReleased() {
    let urlStr = app.getNetAddresss('plugin.love-speed-pool.frontend.receive.index');
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result == 1) {
          this.setData({
            love_name: res.data.data.love_name,
            amount: parseFloat(res.data.data.amount)
          })
          wx.setNavigationBarTitle({
            title: res.data.data.love_name + '加速池释放'
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getData() {
    let urlStr = app.getNetAddresss('plugin.love-speed-pool.frontend.home.index');
    urlStr += '&page=1'
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result == 1) {
          this.setData({
            released_rocord: res.data.data.released_rocord,
            speed_pool_record: res.data.data.speed_pool_record,
            member_surplus: parseFloat(res.data.data.member_surplus)
          })
          this.data.released_rocord.data.forEach(item => item.love_name = res.data.data.love_name)
          this.setData({
            ['released_rocord.data']: this.data.released_rocord.data,
          })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data[this.data.active].current_page >= this.data[this.data.active].last_page) {
      return
    } else {
      this.getMoreData()
    }
  },
  //获取更多数据
  getMoreData() {
    let urlPath = '';
    if(this.data.speed_pool == 'speed_pool'){
      urlPath = 'plugin.love-speed-pool.frontend.home.index'
    }else{
      urlPath = 'plugin.love-speed-pool.frontend.plus.index'
    }
    let urlStr = app.getNetAddresss(urlPath);
    urlStr += `&page=${this.data[this.data.active].current_page+1}`
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result == 1) {
          let datas = '';
          let current_page = 0;
          if(this.data.active == 'account_balance'){
             datas = this.data.account_balance.data.concat(res.data.data.list.data)
             current_page = res.data.data.list.current_page
          }else{
             datas = this.data[this.data.active].data.concat(res.data.data[this.data.active].data)
             current_page = res.data.data[this.data.active].current_page
             this.data.active == "released_rocord" && datas.forEach(item => item.love_name = res.data.data.love_name)
          }
          let record_type_data = `${this.data.active}.data.`,
          record_type_current_page = `${this.data.active}.current_page`;
            this.setData({
              [record_type_data]: datas,
              [record_type_current_page]: current_page
            })
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
})