// order.js
const app = getApp(),
  { hostname, business_id } = app.globalData,
  users_id = wx.getStorageSync('users_id')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    empty_box: {
      tips_img: 'https://ypx.shenmikj.com/canyin/Uploads/canyin/box_red.png',
      tips_word: '暂无订单',
      tips_btn: '返回首页'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.globalData.business_id=options.business_id;
  },

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

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

  },

  /**
     * 用户点击右上角分享
     */
  onShareAppMessage: function (res) {
    var userid = wx.getStorageSync('openId')
    // var userid = 1

    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: app.globalData.share_title,
      path: '/pages/index/index?u_openid=' + userid,
      success: function (res) {
        var pages = getCurrentPages()    //获取加载的页面
        var currentPage = pages[pages.length - 1]    //获取当前页面的对象
        //点击分享传进userid 截取userid 从onload拿到
        var url = currentPage.route + '?u_openid=' + userid;  //当前页面url
        console.log(url, '/pages/index/index?u_openid=' + userid);
      },
      fail: function (res) {
        // 转发失败
      },
    }
  },

  // 获取订单列表
  getOrderList() {
    let that = this
    var business_id = app.globalData.business_id
    wx.request({
      url: hostname + 'Order/get_order_list',
      data: {
        business_id,
        users_id,
        page: this.data.page
      },
      header: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' },
      method: 'POST',
      dataType: 'JSON',
      success: (res) => {
        let orderList = JSON.parse(res.data).data
        that.setData({ orderList: orderList ? orderList:[] })
      },
      fail: (res) => { },
      complete: (res) => { },
    })
  },

  __emptyAction(){
    wx.switchTab({
      url: '../index/index',
    })
  }
})