const wx2my = require('../../wx2my');
const Behavior = require('../../Behavior');
// pages/userCenter.js
const app = getApp();

const wxService = require('../../utils/wxService');

const utils = require('../../utils/util');

const envInfo = require('../../config/index').envInfo;

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    pageNo: 1,
    pageSize: 10,
    totalPages: '',
    orderList: [],
    noMore: false
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // const { orderNo } = this.options
    this.initOrderList(this.data.pageNo, this.data.pageSize);
  },

  goDetail(e) {
    const {
      orderNo
    } = e.currentTarget.dataset; // pages/consumptionDetails/consumptionDetails

    wxService.router(`/pages/consumptionDetails/consumptionDetails?orderNo=${orderNo}`);
  },

  //获取消费列表
  initOrderList(pageNo, pageSize) {
    wx2my.showLoading({
      title: '加载中'
    }); // 取会员id

    const {
      memberId
    } = wx2my.getStorageSync('_baseUserInfo');
    const params = {
      memberId
    };
    wxService.post(`/sale/order/list?pageNo=${pageNo}&pageSize=${pageSize}`, params).then(res => {
      if (res) {
        const {
          result,
          data
        } = res.data;

        if (result == 0) {
          wx2my.hideLoading(); // 数量
          this.setData({
            orderList: this.data.pageNo == 1 ? [...data.content] : [...this.data.orderList, ...data.content],
            totalPages: data.totalPages
          });
        }
      }
    }).finally(() => {
      wx2my.hideLoading();
    });
  },

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.pageNo < this.data.totalPages) {
      this.setData({
        pageNo: this.data.pageNo + 1,
        pageSize: 10,
        noMore: true
      }); // 

      this.initOrderList(this.data.pageNo, this.data.pageSize);
    } else {
      this.setData({
        noMore: true
      });
    }
  }
});