// pages/welfareMall/historical/historical.js
import WelfareMall from '../../../api/welfareMall'
import { parseTime } from '../../../utils/util'
import {getMobileCache, getPhoneNumber as getPhoneNumberSync, getColor} from '../../../utils/user'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    pageSize: 10,
    lock: false,
    noResult: false,
    noMore: false,
    mobileTop: 'TONY WU',
    orderList: [],
    config: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.getOrderList();
    let info =  await getColor();
    this.setData({
      config:info.config
    })
    console.log(info)
  },

  // 订单数据
  getOrderList: function() {
    WelfareMall.getOrderList({
      page: this.data.page,
      pageSize: this.data.pageSize,
      mobile: getMobileCache(),
    }).then(res => {
        if (res.code == 200) {
            this.userOrderListView(res.data)
        }
        this.data.lock = false
    }).catch(_ => {
        console.log(_)
        this.data.lock = false
    })
},

  userOrderListView: function(data) {
    if (!Array.isArray(data) || data.length == 0) {
        console.log("订单列表数据为空");
        if (this.data.page == 1) {
            this.setData({
                mobileTop:getMobileCache(),
                noResult: true
            })
        } else {
            this.setData({
                mobileTop:getMobileCache(),
                noMore: true
            })
        }
        return
    }
    // data.forEach(v => {
    //     let payeTime = v.payedAt
    //     v.payedAt = parseTime(payeTime, "{y}.{m}.{d} {h}:{i}:{s}")
    // })

    this.data.orderList = this.data.orderList.concat(...data)
    this.setData({
        mobileTop:getMobileCache(),
        orderList: this.data.orderList
    })
  }, 

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.lock || this.data.noMore) {
      return
    }
    this.data.lock = true
    this.data.page++
    this.getOrderList();

  },

  getCompletion(e) {
    const url = "../order/orderCompletion?ordersn=" + e.currentTarget.dataset.ordersn
    wx.navigateTo({
        url
    })
  },

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

  }
})