// pages/order/order.js
const app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const envInfo = require('../../config/index').envInfo

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperItem: [
      {
        type: '1',
        name: '全部'
      },
      {
        type: '2',
        name: '待付款'
      },
      {
        type: '3',
        name: '待发货'
      },
      {
        type: '4',
        name: '待收货'
      },
      {
        type: '5',
        name: '退款/售后'
      }
    ],
    currentIndex: 1,
    pageNo: 1,
    pageSize: 5,
    noMoreFlag: false,
    orderList: null,
    totalPages: 0,
    subImgs: [{
      listUrl: "https://hwimagecdn.ihotwind.cn//544/201906/1b41a9a3247d42c89e0c461e2faaf6c1_512_512.jpg"
    }, {
      listUrl: "https://hwimagecdn.ihotwind.cn//544/201906/1b41a9a3247d42c89e0c461e2faaf6c1_512_512.jpg"
    }, {
      listUrl: "https://hwimagecdn.ihotwind.cn//544/201906/1b41a9a3247d42c89e0c461e2faaf6c1_512_512.jpg"
    }, {
      listUrl: "https://hwimagecdn.ihotwind.cn//544/201906/1b41a9a3247d42c89e0c461e2faaf6c1_512_512.jpg"
    }, {
      listUrl: "https://hwimagecdn.ihotwind.cn//544/201906/1b41a9a3247d42c89e0c461e2faaf6c1_512_512.jpg"
    }, {
      listUrl: "https://hwimagecdn.ihotwind.cn//544/201906/1b41a9a3247d42c89e0c461e2faaf6c1_512_512.jpg"
    }]
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initOrderList(this.data.pageNo, this.data.pageSize)
  },
  initOrderList(pageNo, pageSize) {
    if (this.data.noMoreFlag) { // 没有更多数据了，不调用接口
      return false
    }

    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wxService.post(`/sale/order/list?pageNo=${pageNo}&pageSize=${pageSize}`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          this.setData({
            orderList: this.data.pageNo == 1 ? [...data.content] : [...this.data.orderList, ...data.content],
            totalPages: data.totalPages
          })
        }
      }
    })
  },
  //点击切换tab
  switchTab(e) {
    this.setData({
      currentIndex: e.currentTarget.dataset.type
    })
  },


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

  },

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

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

  }
})