// packageC/redPacket/personRed/personRed.js
const app = getApp();
Page({
  data: {
    customBalance: '', // 自定义余额字样
    showRejectPopup: false,
    showGiftPopup: false,
    showSuccessPopup: false,

    rid: '', // 红包id
    redpack_img: '', // 红包图标
    redpack_name: '', // 红包会员名称
    redpack_list: [],
    show_button: false,

    total: 0,
    page: 1,
    loading: false,
    finished: false,

    receivedSucceed: {}, // 领取红包成功
  },

  onLoad: function (options) {
    try {
      let yz_basic_info = wx.getStorageSync('yz_basic_info');
      this.setData({
        customBalance: yz_basic_info.balance.balance ? yz_basic_info.balance.balance : "余额"
      });
    } catch (err) {
      console.log(err);
    }
  },

  onShow: function () {
    this.init();
    this.getData();
  },

  init() {
    this.setData({
      showRejectPopup: false,
      showGiftPopup: false,
      showSuccessPopup: false,
      rid: '', // 红包id
      redpack_img: '', // 红包图标
      redpack_name: '', // 红包会员名称
      redpack_list: [],
      show_button: false,
      total: 0,
      page: 1,
      loading: false,
      finished: false,
    });
  },

  getData() {
    let urlStr = app.getNetAddresss("plugin.redpack-user.frontend.homePage.index");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result !== 1) {
          wx.showToast({
            title: res.data.msg,
          });
          return;
        }
        let data = res.data.data;
        let finished = false;
        if (this.data.page >= data.redpack_list.last_page || data.redpack_list.data.length < data.redpack_list.per_page) {
          finished = true;
        }
        this.setData({
          finished,
          page: (++this.data.page),
          show_button: data.show_button,
          redpack_list: data.redpack_list.data
        });
      }
    });
  },

  getMoreData() {
    if (this.data.finished) return;
    let urlStr = app.getNetAddresss("plugin.redpack-user.frontend.homePage.index");
    wx.showLoading({
      title: '加载中',
    });
    app._getNetWork({
      url: urlStr,
      data: {
        page: this.data.page
      },
      success: (res) => {
        wx.hideLoading();
        if (res.data.result !== 1) {
          wx.showToast({
            title: res.data.msg,
          });
          return;
        }
        let data = res.data.data;
        let finished = false;
        let redpack_list = this.data.redpack_list.concat(data.redpack_list.data);
        if (this.data.page >= data.redpack_list.last_page || data.redpack_list.data.length < data.redpack_list.per_page) {
          finished = true;
        }
        this.setData({
          redpack_list,
          finished,
          page: (++this.data.page)
        });
      },
      fail() {
        wx.hideLoading();
      }
    });
  },

  receiveGift(event) {
    let redpack = event.currentTarget.dataset.redpack;
    this.setData({
      rid: redpack.id,
      redpack_img: redpack.thumb,
      redpack_name: redpack.member.nickname,
      showGiftPopup: true,
    });
  },

  closePopup() {
    this.setData({
      showGiftPopup: false
    });
  },

  // 拆开红包
  openGift() {
    let urlStr = app.getNetAddresss("plugin.redpack-user.frontend.receive.index");
    app._getNetWork({
      url: urlStr,
      data: {
        rid: this.data.rid
      },
      success: (res) => {
        if (res.data.result !== 1) {
          this.setData({
            showRejectPopup: true,
            showGiftPopup: false,
            rejectMess: res.data.msg,
          });
          return;
        }
        this.setData({
          receivedSucceed: res.data.data,
          showGiftPopup: false,
          showSuccessPopup: true
        });
      }
    });
  },

  // 确认
  confirm() {
    this.setData({
      showSuccessPopup: false,
      page: 1,
    });
    this.getData();
  },


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

  goPersonRedIssue() {
    wx.navigateTo({
      url: '/packageI/personRed/personRedIssue/personRedIssue',
    });
  },
  goPersonList() {
    wx.navigateTo({
      url: '/packageI/personRed/personRedList/personRedList',
    });
  },
  goPersonRedRecord(event) {
    let rid = event.currentTarget.dataset.rid;
    wx.navigateTo({
      url: `/packageI/personRed/personRedRecord/personRedRecord?rid=${rid}`,
    });
  },
});