// packageE/community_buying/buying_order/buying_order.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    scrollTop: 0,
    //当前的订单状态
    selected: 0,
    isLoadMore: true,
    //shop 商城，cashier 收银台 store 门店
    orderType: 'community_buying',
    detailUrl: 'orderdetail',
    total_page: 0,
    page: 1,
    action: '',
    //全部订单
    orderList: [],
    //待付款
    waitPayList: [],
    //待发货
    waitSendList: [],
    //全待收货
    waitReceiveList: [],
    //已完成
    waitCompleteList: [],
    stopRefresh: false,
    //待付款-多订单合并付款的订单id数组
    order_ids: [],
    //是否开启合并支付
    isMultiplePay: false,
    buyingid:0,

    keywords: "",
  },

  inpbtn(e) {
    let val = e.detail.value;
    this.setData({
      keywords: val
    });
  },
  inpbtnclear() {
    this.setData({
      keywords: ''
    });
  },
  search() {
    this._getOrderList(this.data.selected);
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
		
    if (options.status) {
      this.setData({
        selected: options.status
      });
    }
    if (options.buyingid) {
      this.setData({
        buyingid: options.buyingid
      });
    }

  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {


  },

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

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {
    if (this.data.stopRefresh) {
      wx.stopPullDownRefresh();
    } else {
      this._initData();
      this._getOrderList(this.data.selected);
    }

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (this.data.isLoadMore) {
      this._getMoreData(this.data.page, this.data.selected);
    } else {
      console.log('没有更多数据');
    }
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  _initData() {
    this.setData({
      isLoadMore: true
    });
    this.data.page = 1;
    this.data.total_page = 0;
  },
  _getOrderList(index) {
    if (index == 0) {
      this.data.action = 'plugin.package-deliver.frontend.order.list.index';
		
    } else if (index == 1) {
      this.data.action = 'plugin.package-deliver.frontend.order.list.waitPay';
			
    } else if (index == 2) {
      this.data.action = 'plugin.package-deliver.frontend.order.list.waitDeliver';
		
    } else if (index == 3) {
      this.data.action = 'plugin.package-deliver.frontend.order.list.completed';
			
    } else if (index == 4) {
      this.data.action = 'plugin.package-deliver.frontend.order.list.cancelled';
		
    }
    this._getNetData(this.data.action, index);

  },
  //获取订单数据
  _getNetData(api, index) {
    let that = this;
    this.data.page=1;
    let urlStr = app.getNetAddresss(api);
    urlStr += "&page=" + 1;
    if(this.data.buyingid && this.data.buyingid!=0){
      urlStr += "&group_id=" + this.data.buyingid;
    }
    app._getNetWork({
      url: urlStr,
      data:{keyword: this.data.keywords},
      success: function(resdata) {
        var res = resdata.data;
        console.log(index, '测试');

        if (res.result == 1) {
          let myData = res.data;
          that.data.total_page = myData.total;
          if (index == 0) {
            that.setData({
              orderList: myData.data
            });
          } else if (index == 1) {
            that.setData({
              waitPayList: myData.data
            });
          } else if (index == 2) {
            that.setData({
              waitSendList: myData.data
            });
          } else if (index == 3) {
            that.setData({
              waitReceiveList: myData.data
            });
          } else if (index == 4) {
            that.setData({
              waitCompleteList: that.data.waitCompleteList.concat(myData.data)
            });
          } else {
            that.setData({
              waitRefund: myData.data
            });
          }
        }
        wx.stopPullDownRefresh();
      },
      fail: function(res) {
        wx.stopPullDownRefresh();
        console.log(res);
      }
    });
		

  },
  _getMoreData(page, index) {
    let that = this;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let urlStr = app.getNetAddresss(this.data.action);
      urlStr += "&page=" + this.data.page;
      if(this.data.buyingid && this.data.buyingid!=0){
        urlStr += "&group_id=" + this.data.buyingid;
      }
      app._getNetWork({
        url: urlStr,
        data:{keyword: this.data.keywords},
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            let myData = res.data;
            if (index == 0) {
              that.setData({
                orderList: that.data.orderList.concat(myData.data)
              });

            } else if (index == 1) {
              that.setData({
                waitPayList: that.data.waitPayList.concat(myData.data)
              });

            } else if (index == 2) {
              that.setData({
                waitSendList: that.data.waitSendList.concat(myData.data)
              });

            } else if (index == 3) {
              that.setData({
                waitReceiveList: that.data.waitReceiveList.concat(myData.data)
              });
            } else if (index == 4) {
              that.setData({
                waitCompleteList: that.data.waitCompleteList.concat(myData.data)
              });
            } else {
              that.setData({
                waitRefund: that.waitRefund.concat(myData.data)
              });
            }
          }

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

        }
      });
    }
  },
  swichTabTItem(e) {
    let index = e.detail.index;
    this.setData({
      selected: index
    });
    this._initData();
    this._getOrderList(this.data.selected);
  },
  onPageScroll(event) {
    this.setData({
      scrollTop: event.scrollTop
    });
  },
  stopRefresh(e) {
    let bol = e.detail;
    this.setData({
      stopRefresh: bol
    });
  },
  //接收到合并多订单支付的通知方法
  multiplePayAction(e) {
    let order_ids = e.detail;
    this.setData({
      order_ids: order_ids
    });
    if (order_ids.length > 0) {
      this.setData({
        isMultiplePay: true
      });
    } else {
      this.setData({
        isMultiplePay: false
      });
    }
  },
  gotoPickUpCard(){
    wx.navigateTo({
      url: '/packageE/pickUpCard/pickUpCardIndex/pickUpCardIndex'
    });
  },
  //待付款状态下-多订单合并支付,order_ids-订单id数组
  toMultiplePay() {
    //type-1订单支付，type-2订单合并支付
    wx.navigateTo({
      url: '/packageD/buy/orderPay/orderPay?order_id=' + this.data.order_ids.join(',') + '&status=2'
    });
  },
  //确认收货订单
  cofirmOrderAction(e) {
    let order = e.detail;
    if (this.data.selected == 0) {
      //全部订单列表
      this._getOrderList(this.data.selected);
    } else if (order == 'pay') {
      this._getOrderList(this.data.selected);
    } else {
      let waitReceiveList = this.data.waitReceiveList;
      for (let i = 0; i < waitReceiveList.length; i++) {
        if (order == waitReceiveList[i].id) {
          waitReceiveList.splice(i, 1);
        }
      }
      this.setData({
        waitReceiveList: waitReceiveList
      });

    }
  },
});
