let App = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    IMG_NAME: App.IMG_NAME,
    order_id: null,
    order: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.order_id = options.order_id
  },

  onShow() {
    this.getOrderDetail(this.data.order_id)
  },

  refund(e) {
    wx.navigateTo({
      url: '../applyRefund/applyRefund?order_id=' + this.data.order_id,
    })
  },

  /**
   * 获取订单详情
   */
  getOrderDetail(order_id) {
    let _this = this,
      params = {
        orderId: order_id
      }
    App.AjaxRequest('get', {
      'Authorization': 'Bearer ' + App.Getopenid()
      }, '/orders/orderDetail', params,
      function (res) {
        if (res.code == 0) {
          _this.setData({
            order: res.data
          })
        }
      })
  },

  /**
   * 跳转到商品详情
   */
  goodsDetail: function (e) {
    let goods_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../../shop/goods/goods?goods_id=' + goods_id
    });
  },

  /**
   * 取消订单
   */
  cancelOrder: function (e) {
    let _this = this;
    let order_id = _this.data.order_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) {
                wx.navigateBack()
              }
            })
        }
      }
    });
  },

  /**
   * 发起付款
   */
  payOrder: function (e) {
    let _this = this
    _this.pay(wx.getStorageSync('OPENID'))
    // 显示loading
    wx.showLoading({
      title: '正在处理...',
    })
  },

  pay(e) {
    let _this = this
    App.AjaxRequest('post', {
      'Authorization': 'Bearer ' + App.Getopenid()
      }, '/shop/pay/wxPay?openId=' + e + '&orderId=' + _this.data.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=' + _this.data.order_id, {},
                function (res) {
                  // 跳转到已付款订单
                  _this.getOrderDetail(_this.data.order_id)
                })
            },
            fail: function (res) {
              App.showError('订单未支付')
            },
          })
        }
      })
  },

  /**
   * 确认收货
   */
  receipt: function (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);
            })
        }
      }
    })
  },


});