let App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    IMG_NAME: App.IMG_NAME,
    dataType: '',
    list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.dataType = options.type || '';
    this.setData({
      dataType: this.data.dataType
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 获取订单列表
    this.getOrderList(this.data.dataType);
  },

  /**
   * 获取订单列表
   */
  getOrderList(dataType) {
    let _this = this,
      params = {}

    if (dataType == 'receiveStatus') {
      params.receiveStatus = 'NO',
        params.deliveryStatus = 'YES',
        params.payStatus = 'YES',
        params.status = 'CREATED'
    }
    if (dataType == 'payStatus') {
      params.payStatus = 'YES'
    }
    if (dataType == 'deliveryStatus') {
      params.payStatus = 'YES',
        params.deliveryStatus = 'NO',
        params.status = 'CREATED'
    }
    App.AjaxRequest('get', {
        'Authorization': 'Bearer ' + App.Getopenid()
      }, '/orders/myList', params,
      function (res) {
        if (res.code == 0) {
          _this.setData({
            list: res.rows
          })
          res.rows.length && wx.pageScrollTo({
            scrollTop: 0
          })
        }
      })
    // App._get('user.order/lists', {
    //   dataType
    // }, function (result) {
    //   _this.setData(result.data)
    //   result.data.list.length && wx.pageScrollTo({
    //     scrollTop: 0
    //   })
    // })
  },

  /**
   * 切换标签
   */
  bindHeaderTap: function (e) {
    this.setData({
      dataType: e.target.dataset.type
    });
    // 获取订单列表
    this.getOrderList(e.target.dataset.type);
  },

  /**
   * 取消订单
   */
  cancelOrder: function (e) {
    let _this = this;
    let order_id = e.currentTarget.dataset.id;
    let params = {}
    wx.showModal({
      title: "提示",
      content: "确认取消订单？",
      success: function (o) {
        if (o.confirm) {
          App.AjaxRequest('post', {
              'Authorization': 'Bearer ' + App.Getopenid()
            }, '/orders/cancelOrder?orderId=' + order_id, params,
            function (res) {
              if (res.code == 0) {
                _this.getOrderList('')
              }
            })
        }
      }
    });
  },

  /**
   * 确认收货
   */
  receipt(e) {
    let _this = this;
    let order_id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "提示",
      content: "确认收到商品？",
      success: function (o) {
        if (o.confirm) {
          App.AjaxRequest('post', {
            'Authorization': 'Bearer ' + App.Getopenid()
            }, '/orders/confirm?orderId=' + order_id, {},
            function (res) {
              _this.getOrderList(_this.data.dataType);
            })
        }
      }
    })
  },

  refund(e) {
    wx.navigateTo({
      url: '../../my/order/applyRefund/applyRefund?order_id=' + e.currentTarget.dataset.id,
    })
  },

  /**
   * 发起付款
   */
  payOrder: function (e) {
    let _this = this;
    let order_id = e.currentTarget.dataset.id;

    // 显示loading
    wx.showLoading({
      title: '正在处理...',
    })
    App.AjaxRequest('post', {
      'Authorization': 'Bearer ' + App.Getopenid()
      }, '/shop/pay/wxPay?openId=' + wx.getStorageSync('OPENID') + '&orderId=' + order_id, {},
      function (res) {
        if (res.code == 0) {
          // 发起微信支付
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: 'MD5',
            paySign: res.data.paySign,
            success: function (res) {
              App.AjaxRequest('post', {
                'Authorization': 'Bearer ' + App.Getopenid()
                }, '/shop/pay/wxPayQuery?orderId=' + order_id, {},
                function (res) {
                  // 跳转到订单详情
                  wx.navigateTo({
                    url: '../order/detail/detail?order_id=' + order_id,
                  })
                })
            },
            fail: function () {
              App.showError('订单未支付', function () {})
            }
          })
        }
      })
  },

  /**
   * 跳转订单详情页
   */
  detail: function (e) {
    let order_id = e.currentTarget.dataset.id
    console.log(order_id)
    wx.navigateTo({
      url: '../order/detail/detail?order_id=' + order_id
    });
  },

  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  }


});