// pages/saleOutStore/saleOutStore.js
const app = getApp();
var network = require("../../utils/net.js");
var timechage = require("../../utils/dateTimeUtil.js");
var UnCheckPageIndex = 0
var UnCheckPageSize = 10
var CheckedPageIndex = 0
var CheckedPageSize = 10
var outStoreMethod = '/MainSystem/B3MiniProgramRpc/Rpcs/CustomerRpc/SaleOutStoreRpc/GetSaleOutStore'
// 未审核订单列表
function getUnCheckOrderList(that, UnCheckPageIndex, UnCheckPageSize) {
  let getArr = [];
  let method = outStoreMethod;
  let params = [0, UnCheckPageIndex, UnCheckPageSize];
  network.transfer_request(method, params, function(res) {
    var array = res.result;
    if (array.length <= 0) {
      wx.showToast({
        title: '无数据更新',
      })
      return;
    }
    for (var i = 0; i < array.length; i++) {
      var dmo = {
        orderID: array[i].ID,
        money: array[i].Customer_Name,
        // money: array[i].Money,
        time: timechage.formatTimeTwo(array[i].LoadTime.substring(6, 19), 'Y/M/D')
      }
      getArr.push(dmo);
    }
    let arrLast = that.data.unCheckDataArr.concat(getArr);
    // if (arrLast.length == that.data.unCheckDataArr.length){
    //   wx.showToast({
    //     title: '已加载全部数据',
    //   })
    //   return;
    // } else {
      that.setData({
        unCheckDataArr: arrLast,
      })
    // }
  })
}

// 已审核订单列表
function getCheckedOrderList(that, CheckedPageIndex, CheckedPageSize) {
  let getArr = [];
  let method = outStoreMethod;
  let params = [20, CheckedPageIndex, CheckedPageSize];
  network.transfer_request(method, params, function(res) {
    var array = res.result;
    if (array.length <= 0) {
      wx.showToast({
        title: '无数据更新',
      })
      return;
    }
    for (var i = 0; i < array.length; i++) {
      var dmo = {
        orderID: array[i].ID,
        money: array[i].Customer_Name,
        // money: array[i].Money,
        time: timechage.formatTimeTwo(array[i].LoadTime.substring(6, 19), 'Y/M/D')
      }
      getArr.push(dmo);
    }
    let arrLast = that.data.checkedDataArr.concat(getArr);

    that.setData({
      checkedDataArr: arrLast,
    })
  })
}

Page({
  data: {
    checkedDataArr: [],
    unCheckDataArr: [],
    currentTab: 0,
    winHeight: 0,
    winWidth: 0,
  },

  /**
   * 滑动切换tab
   */
  bindChange: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });

  },
  /**
   * 点击tab切换
   */
  swichNav: function(e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  //点击进入详情页面
  transToOrderDetail: function(event) {
    var itemID = event.currentTarget.dataset.detailitemid;
    console.log("获取到的详情ID = " + itemID);
    wx.navigateTo({
      url: 'outStoreDetail/outStoreDetail?detailItemID=' + itemID,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    UnCheckPageIndex = 0;
    CheckedPageIndex = 0;
    getUnCheckOrderList(that, UnCheckPageIndex, UnCheckPageSize);
    getCheckedOrderList(that, CheckedPageIndex, CheckedPageSize);
  },

  //   getUnCheckOrderList(that, UnCheckPageIndex, UnCheckPageSize);
  //   getCheckedOrderList(that, CheckedPageIndex, CheckedPageSize);
  // }

  //未审核订单滑动
  // unCheckedScrollUpper : function (){
  //   this.data.unCheckDataArr = [];
  //   var that = this;
  //   UnCheckPageIndex = 0;
  //   getUnCheckOrderList(that, UnCheckPageIndex, UnCheckPageSize);
  // },

  //审核订单滑动
  // checkedScrollUpper: function () {
  //   var that = this;
  //   CheckedPageIndex = 0;
  //   getCheckedOrderList(that, CheckedPageIndex, CheckedPageSize);
  // },

  //未审核订单滑动到底部加载
  unCheckedScrollLower: function(event) {
    var that = this;
    UnCheckPageIndex = UnCheckPageIndex + 1;
    getUnCheckOrderList(that, UnCheckPageIndex, UnCheckPageSize);
  },

  //已审核订单滑动到底部加载
  CheckedScrollLower: function (event) {
    var that = this;
    CheckedPageIndex = CheckedPageIndex + 1;
    getCheckedOrderList(that, CheckedPageIndex, CheckedPageSize);
  },
  /**
   * scrollView不会调用此函数
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    if (this.data.currentTab === 0) {
      UnCheckPageIndex = UnCheckPageIndex + 1;
      getUnCheckOrderList(that, UnCheckPageIndex, UnCheckPageSize);
    } else {
      CheckedPageIndex = CheckedPageIndex + 1;
      getCheckedOrderList(that, CheckedPageIndex, CheckedPageSize);
    }
  },
})