// 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: {
    // 订单状态(C:取消;N:新建,P:已支付;D:已发货;R:已收货)
    swiperItem: [
      {
        type: '1',
        status: '',
        name: '全部'
      },
      {
        type: '2',
        status: 'N',
        name: '待付款' // 新建
      },
      {
        type: '3',
        status: 'P',
        name: '待发货'
      },
      {
        type: '4',
        status: 'D',
        name: '待收货'
      },
      {
        type: '5',
        status: 'RF',
        name: '退款/售后'
      }
    ],
    currentIndex: 1,
    pageNo: 1,
    pageSize: 5,
    noMoreFlag: false,
    orderList: null,
    totalPages: 0,
    subImgs: [],
    trade: ''  // 订单状态(C:取消;N:新建,P:已支付;D:已发货;R:已收货)
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initOrderList(this.data.pageNo, this.data.pageSize, this.data.trade)
  },
  handelDetail(e) {
    // const {detail} = e.currentTarget.dataset
    const { id } = e.currentTarget.dataset
    wxService.router(`/pages/orderDetail/orderDetail?id=${id}`)
  },
  initOrderList(pageNo, pageSize,trade) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    const params = {trade}
    wxService.post(`/sale/trade/buyer/history?pageNum=${pageNo}&pageSize=${pageSize}`, params).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          this.setData({
            orderList: this.data.pageNo == 1 ? [...data] : [...this.data.orderList, ...data],
            noMoreFlag: data.length < pageSize ? true : false
          },()=> {
            wx.setStorageSync('orderList', this.data.orderList)
          })
        }
      }
    })
  },
  //点击切换tab
  switchTab(e) {
    const { type, status } = e.currentTarget.dataset
    const {currentIndex} = this.data
    if(currentIndex == type) return false
    this.setData({
      currentIndex: type,
      pageNo: 1,
      orderList: [],
      trade: ''
    },()=>{
      if(type == 5 && status == 'RF'){
        console.log("退款/售后")
      } else {
        this.initOrderList(this.data.pageNo, this.data.pageSize, status)
      }
    })
  },


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

  },

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

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

  }
})