// packageI/warehouseFarm/warehouseFarmGiftLog/warehouseFarmGiftLog.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeName: 0,
    dataList: [],
    titleArr: [{
      name: "我送出的",
      url: "plugin.warehouse.frontend.controllers.gift.records.give"
    },
    {
      name: "我领取的",
      url: "plugin.warehouse.frontend.controllers.gift.records.receive"
    }
    ],
    isLoadMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.tag == 1){
      this.setData({
        activeName: 1
      });
    }
    
  },
  init() {
    this.data.dataList = [{
      loading: false,
      finished: false,
      refreshing: false,
      page: 1,
      list: [],
      url: "plugin.warehouse.frontend.controllers.gift.records.give"
    },
    {
      loading: false,
      finished: false,
      refreshing: false,
      page: 1,
      list: [],
      url: "plugin.warehouse.frontend.controllers.gift.records.receive"
    }
    ];
  },
  handleClick(e) {
    this.setData({
      activeName: e.detail.name
    });
    if (this.data.dataList[this.data.activeName].list.length == 0) {
      this.getData();
    }
  },
  getData() {
    let urlStr = app.getNetAddresss(this.data.dataList[this.data.activeName].url);
    app._postNetWork({
      url: urlStr,
      data: {
        page: this.data.dataList[this.data.activeName].page
      },
      success: (resdata) => {
        let res = resdata.data;
        this.data.dataList[this.data.activeName].loading = false;
        if (res.result == 1) {
          this.data.dataList[this.data.activeName].isLoadMore = true;
          this.data.dataList[this.data.activeName].page = this.data.dataList[this.data.activeName].page + 1;

          let _list = this.data.dataList[this.data.activeName].list;
          this.setData({
            ['dataList[' + this.data.activeName + '].list']: _list.concat(res.data.data)
          });
          if (res.data.last_page == res.data.current_page) {
            this.data.dataList[this.data.activeName].finished = true;
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  toUrl(e) {
    let _data = e.currentTarget.dataset || {};
    this.wxRouterLink(_data);
  },
  wxRouterLink(_data) {
    let _Param = '';
    const {
      url,
      ...otherParam
    } = _data;
    Object.keys(otherParam).forEach(function (key) {
      if (otherParam[key] !== '') _Param += key + '=' + otherParam[key] + '&';
    });
    if (url === '') return;
    wx.navigateTo({
      url: url + '?' + _Param.slice(0, -1)
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(this.data.dataList[this.data.activeName].loading, this.data.dataList[this.data.activeName].finished);
    if (!this.data.dataList[this.data.activeName].loading && !this.data.dataList[this.data.activeName].finished) {
      this.getData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});