// order.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex: 0,
    hasOnload: false,
    orderziti: '',
  },
  /**
 * 生命周期函数--监听页面显示
 */
  //未自提
  set_order: function (e) {
    var id = e.currentTarget.dataset.id;
    if (e.currentTarget.dataset.type == 2) {
      return false;
    }
    console.log(app.globalData.openid)
    wx.request({
      url: app.globalData.serverUrl + '/Api/Integral/orderList',
      data: {
        admin_id: app.globalData.admin_id,
        openid: app.globalData.openid,
        status: typekind
      },
      success: function (res) {
        console.log(res.data.status);
        if (res.data.status == 1) {
          wx.showModal({
            title: '提示',
            content: '确认成功！',
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '确认失败！',
          })
        }
      }
    })
  },
  onShow: function () {
    var that = this;
    if (that.data.hasOnload) {
      typekind = that.data.tabIndex;
      that.getOrderList(that, typekind);
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getOrderList(that, that.data.tabIndex);
      
  },
  onUnload: function () {
    console.log("+++");
  },
  onHide: function () {
    this.setData({
      hasOnload: true,
    })
  },

  //获取订单列表
  getOrderList: function (that, typekind) {
    wx.showLoading({
      title: '正在加载中',
    })
    // type: 0全部 1待付款 2待发货 3待收货
    wx.request({
      url: app.globalData.serverUrl + '/Api/Int/exchange_log',
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        business_id: app.globalData.business_id,
        page: 1,
        openid:app.globalData.openid,
      },
      success: function (res) {
        if(res.data.status==1){
         
        }
        
        // that.setData({
        //   orderList: res.data.data
        // })
        console.log(res, "订单列表");
        if (res.data.status == 1) {
          for (var i = 0; i < res.data.data.length; i++) {
            res.data.data[i].newprice = returnFloat(res.data.data[i].goodsPrice)
          }
          function returnFloat(value) {
            var value = Math.round(parseFloat(value) * 100) / 100;
            var xsd = value.toString().split(".");
            if (xsd.length == 1) {
              value = value.toString() + ".00";
              return value;
            }
            if (xsd.length > 1) {
              if (xsd[1].length < 2) {
                value = value.toString() + "0";
              }
              return value;
            }
          }
          that.setData({
            orderList: res.data.data
          })
          console.log(res.data.data[0]);
        } else {
          that.setData({
            orderList: []
          })
        }
      },
      fail: function (res) { },
      complete: function (res) {
        wx.hideLoading()
      },
    })
  },
  // 选项卡
  changeTabbar: function (e) {
    var that = this;
    var tabIndex = e.currentTarget.dataset.tabindex;
    that.setData({
      tabIndex: tabIndex
    })
    that.getOrderList(that, tabIndex);
  },
  onReady: function () {

  },
  //取消订单
  cancleOrder: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.ordersn);
    var order_sn = e.currentTarget.dataset.ordersn;
    app.cancleOrder(that, order_sn, "order");

  },
  //去付款
  goPay: function (e) {
    var that = this;
    var order_sn = e.currentTarget.dataset.ordersn;
    wx.showModal({
      title: '温馨提示',
      content: '未绑定商户号，暂不支持微信支付，点击确定可进行模拟支付',
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.hostname + 'test_pay',
            data: { order_sn: order_sn },
            header: {},
            method: 'GET',
            dataType: '',
            success: function (resPay) {
              var openid = wx.getStorageSync("openid");
              var page = 1, size = 9999, typekind = that.data.tabIndex;
              wx.showToast({
                title: '模拟支付成功',
                duration: 1000,
                success: function () {
                  setTimeout(function () {
                    that.getOrderList(that, openid, page, size, typekind)
                  }, 1000)
                }
              })
            },
            fail: function (resPay) { },
            complete: function (resPay) { },
          })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
    // wx.request({
    //   url: app.globalData.hostname + 'payMoney',
    //   data: { order_num: order_sn },
    //   header: {},
    //   method: 'GET',
    //   dataType: '',
    //   success: function (res) {
    //     console.log(res, '支付');
    //     wx.requestPayment({
    //       'timeStamp': res.data.timeStamp,
    //       'nonceStr': res.data.nonceStr,
    //       'package': res.data.package,
    //       'signType': 'MD5',
    //       'paySign': res.data.paySign,
    //       'success': function (res) {
    //         console.log(res);
    //         var openid = wx.getStorageSync("openid");
    //         var page = 1, size = 9999, typekind = that.data.tabIndex;
    //         wx.showToast({
    //           title: '支付成功',
    //           icon: 'success',
    //           image: '',
    //           duration: 1000,
    //           mask: true,
    //           success: function (res) {
    //               setTimeout(function () {
    //                 that.getOrderList(that, openid, page, size, typekind)
    //               }, 1000)
    //           },
    //           fail: function (res) { },
    //           complete: function (res) { },
    //         })
    //         ;

    //       },
    //       'fail': function (res) {
    //         console.log(res);
    //       },
    //       'complete': function (res) {
    //         console.log(res);
    //       }
    //     })

    //   },
    //   fail: function (res) { },
    //   complete: function (res) { },
    // })
  },
  //退货、退货退款
  refoundMoney: function (e) {
    var order_sn = e.currentTarget.dataset.ordersn;
    var status = e.currentTarget.dataset.status;
    wx.navigateTo({
      url: '../refund/refund?order_sn=' + order_sn + "&status=" + status,
    })
  },
  //删除订单
  deleteOrder: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.ordersn);
    var order_sn = e.currentTarget.dataset.ordersn;
    app.deleteOrder(that, order_sn, "order");
  },
  //确认收货
  sureReceiveOrder: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.ordersn);
    var order_sn = e.currentTarget.dataset.ordersn;
    app.sureReceiveOrder(that, order_sn, "order");
  },
  //填写物流信息
  uploadLogistics: function (e) {
    let order_sn = e.currentTarget.dataset.ordersn;
    wx.navigateTo({
      url: '../logistics/logistics?order_sn=' + order_sn,
    })
  },

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

  },
  onShareAppMessage: function (res) {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})