// pages/companyOrder/companyOrder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    state: 'index', //全部index  待付款unpaid 待发货 paid 待收货delive 退款/售后refund
    lists: [], //列表
    page: 1,
    has_more: true,
  },

  onLoad() {
    this.orderList();
  },

  /**
   * 点击按钮触发事件
   */
  menuSelect: function (e) {
    if (this.data.state != e.currentTarget.dataset.state) {
      this.setData({
        state: e.currentTarget.dataset.state,
        page: 1,
        lists: [],
        has_more: true
      })
      this.orderList()
    }
  },
  // 全部index  待付款  unpaid 待发货 paid 待收货 delive 退款/售后 refund
  orderList() {
    var url = 'crowdorders'
    if (this.data.state == 'index') {
      url = 'crowdorders'
    } else if (this.data.state == 'unpaid') {
      url = 'crowdorders/unpaid'
    } else if (this.data.state == 'paid') {
      url = 'crowdorders/paid'
    } else if (this.data.state == 'delive') {
      url = 'crowdorders/delivered	'
    } else if (this.data.state == 'refund') {
      url = 'refunds/crowdfunds'
    }
    wx.$api.companyModule.orderList(url, this.data.page).then(res => {
      console.log(res)
      setTimeout(() => {
        wx.hideLoading({})
      }, 1000);
      var lists = this.data.lists.concat(res.data)
      if (res.page.has_more) {
        this.setData({
          has_more: res.page.has_more,
          page: this.data.page + 1,
          lists: lists
        })
      } else {
        this.setData({
          has_more: res.page.has_more,
          lists: lists
        })
      }
    })
  },
  // 触底加载更多
  onReachBottom() {
    if (this.data.has_more) {
      this.orderList();
    } else {
      wx.showToast({
        icon: 'none',
        title: '没有更多',
      })
    }
  },
  /**
   * 订单支付
   */
  orderPay(e) {
    let orderId = e.currentTarget.dataset.orderid
    wx.navigateTo({
      url: './projectPay/projectPay?orderid=' + orderId,
    })
  },

  /**
   * 取消订单
   */
  orderDelete(e) {
    wx.showModal({
      title: '提示',
      content: '是否确认取消订单',
      success: res => {
        if (res.confirm) {
          wx.showLoading({
            title: '取消中',
          })
          let orderId = e.currentTarget.dataset.orderid
          wx.$api.order.cancel(orderId).then(res => {
            // 获取列表
            this.setData({
              lists: [],
              has_more: true,
              page: 1
            })
            this.orderList()
          })
        }
      }
    })
  },

  /**
   * 签收订单
   */
  orderSign(e) {
    let orderId = e.currentTarget.dataset.orderid
    wx.$api.order.sign(orderId).then(res => {
      // 获取列表
      this.setData({
        lists: [],
        has_more: true,
        page: 1
      })
      this.orderList()
    })
  },
})