const app = getApp()
import request from '../../utils/request'
import { timestampToTime } from '../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum: 1,
    orderList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getPayOrderList(1)
  },

  goCheckDetail(e) {
    const item = JSON.stringify(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: `/pages/orderDetail/orderDetail?detail=${item}`,
    })
  },

  getPayOrderList(pageNum) {
    const that = this
    request.get({
      url: '/api/pay/payOrderList',
      data: {
        pageNum,
        pageSize: 10
      }
    }).then(res => {
      wx.stopPullDownRefresh()
      wx.hideLoading()
      console.log(res, 'getPayOrderList');
      res.data.list.forEach(item => {
        item.payTime = timestampToTime(item.payTime, 'YYYY-MM-DD hh:mm:ss')
        item.validStartTime = timestampToTime(item.validStartTime, 'YYYY-MM-DD hh:mm:ss')
        item.validEndTime = timestampToTime(item.validEndTime, 'YYYY-MM-DD hh:mm:ss')
        item.createTime = timestampToTime(item.createTime, 'YYYY-MM-DD hh:mm:ss')
        item.updateTime = timestampToTime(item.updateTime, 'YYYY-MM-DD hh:mm:ss')
      })
      if (res.data.list && res.data.list.length > 0) {
        if (pageNum == 1) {
          const tempArr = res.data.list
          that.setData({
            orderList: res.data.list
          })
        } else {
          const tempArr = that.data.orderList
          that.setData({
            orderList: tempArr
          })
        }
      }
    }).catch(err => {
      console.log(err, 'err');
    })
  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showLoading()
    this.getPayOrderList(1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    that.data.pageNum++;
    that.setData({
      pageNum: that.data.pageNum
    });
    that.getPayOrderList(that.data.pageNum);
  },

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

  }
})