// packageC/redPacket/daily_red_home/daily_red_home.js
const app = getApp();
var tapMore = false;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    show1: false,
    receive_logs_lastPage: 1,
    receive_logs_Page: 1,
    goods_list_lastPage: 1,
    goods_list_page: 1,
    info: [],
    color: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
    this.share();
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en,
    });
    if (options.mid) {
      app._setMid(options.mid);
    }
  },
  swipeToTop(e) {
    let ind = e.detail.current;
    if (this.data.receive_logs_Page < this.data.receive_logs_lastPage) {
      // 有分页 第一个开始就加载下一页
      if (this.data.info.receive_logs.data.length - 14 == ind) {
        this.getMore("log", this.data.receive_logs_Page + 1);
      }
    }
  },
  tapGo(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/packageA/detail_v2/detail_v2?id=" + id,
    });
  },
  closeBtn() {
    this.setData({
      show1: false,
    });
  },
  init() {},
  closeShow() {
    this.setData({
      show: false,
    });
    this.init();
    this.getData();
  },
  toPage(e) {
    let tag = e.currentTarget.dataset.tag;
    let urls;
    if (tag == 'record') {
      urls = '/packageC/redPacket/get_red_record/get_red_record?tag=new';
    } else if (tag == 'amount') {
      urls = '/packageC/redPacket/red_amount/red_amount?tag=new';
    } else if (tag == 'rank') {
      urls = '/packageC/redPacket/redRank/redRank';
    }
    wx.navigateTo({
      url: urls,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var value = wx.getStorageSync('yz_uid');
    var mid = '';
    if (value) {
      mid = value;
    }
    return {
      title: this.data.shareinfo.share_title,
      path: '/packageC/redPacket/daily_red_home/daily_red_home?mid=' + mid,
      imageUrl: this.data.shareinfo.share_pictures
    };
  },
  sureGet(e) {
    let value = e.currentTarget.dataset.value;
    if (value == 1) {
      if (tapMore) {
        console.log('等请求完才能重新点击');
        return;
      } else {
        tapMore = true;
        let that = this;
        let urlStr = app.getNetAddresss("plugin.redpack-tool.frontend.redpack.index");
        app._postNetWork({
          url: urlStr,
          success: function (resdata) {
            var res = resdata.data;
            console.log(res);
            if (res.result == 1) {
              that.setData({
                show: true,
                amount: res.data.amount
              });
            } else {
              if (res.data.alert) {
                that.setData({
                  show1: true,
                });
              } else {
                wx.showToast({
                  title: res.msg,
                });
              }
            }
            tapMore = false;
          },
          fail: function (res) {
            console.log(res);
            tapMore = false;
          },
        });
      }
    } else if (value == 0) {
      console.log("不可领取状态");
    } else if (value == -2) {
      wx.showToast({
        title: "当前红包已领完",
      });
    } else if (value == -1) {
      wx.showToast({
        title: "额度不足",
      });
    }

  },
  getData() {
    wx.showLoading({
      title: "加载中",
    });
    let that = this;
    let urlStr = app.getNetAddresss("plugin.redpack-tool.frontend.homePage.index");
    app._postNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          res.data.quota_info.quota_total = Number(res.data.quota_info.quota_total);
          res.data.quota_info.use_total = Number(res.data.quota_info.use_total);
          that.setData({
            info: res.data,
            goods_list_lastPage: res.data.goods_list.last_page,
            receive_logs_lastPage: res.data.receive_logs.last_page,
            bfbMum: (Number(res.data.quota_info.use_total) / Number(res.data.quota_info.quota_total)).toFixed(2) * 100 >= 100 ?
              '100%' 
              : (Number(res.data.quota_info.use_total) / Number(res.data.quota_info.quota_total)).toFixed(2) * 100 + "%",
          });
          wx.setNavigationBarTitle({
            title: res.data.plugin_name,
          });
        } else {
          wx.showToast({
            title: res.msg,
          });
        }
        wx.hideLoading({
          success: (res) => {},
        });
      },
      fail: function (res) {
        console.log(res);
        wx.hideLoading({
          success: (res) => {},
        });
      },
    });
  },
  tapMore() {
    this.getMore('goods', this.data.goods_list_lastPage);
  },
  getMore(key, page) {
    let that = this;
    let json = {};
    if (key == "log") {
      json.log_page = page;
    } else {
      if (page >= this.goods_list_lastPage) {
        return;
      } else {
        this.setData({
          goods_list_page: page + 1
        });
      }
      json.goods_page = this.data.goods_list_page;
    }
    let urlStr = app.getNetAddresss("plugin.redpack-tool.frontend.homePage.index");
    app._postNetWork({
      url: urlStr,
      data: JSON,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          let contenLis, conlist;
          if (key == "log") {
            contenLis = res.data.receive_logs.data;
            conlist = that.data.info.receive_logs.data.concat(contenLis);
            that.data.info.receive_logs.data = conlist;
            that.setData({
              info: that.data.info,
              receive_logs_lastPage: res.data.receive_logs.last_page
            });
          } else {
            contenLis = res.data.goods_list.data;
            conlist = that.data.info.goods_list.data.concat(contenLis);
            that.data.info.goods_list.data = conlist;
            that.setData({
              info: that.data.info,
              goods_list_lastPage: res.data.goods_list.last_page
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  share() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.redpack-tool.frontend.share.index");
    app._postNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          that.setData({
            shareinfo: res.data
          });
        } else {
          wx.showToast({
            title: res.msg,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  goPersonred() {
    wx.navigateTo({
      url: '/packageI/personRed/personRed/personRed',
    });
  }
});