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 getList = '/MainSystem/B3MiniProgramRpc/XuRpcs/Customer/DebitNoteRpc/GetDebitNoteList'

// 未确认订单列表
function getUnCheckOrderList(that, UnCheckPageIndex, UnCheckPageSize, successaction) {
  let getArr = [];
  let method = getList;
  let params = [{
    // Confirmed	bool	false
    // StartDate	datetime	null
    // EndDate	datetime	null
    // PageIndex	int	0
    // PageSize	int	0
    "Confirmed": false,
    "StartDate": that.data.startDate,
    "EndDate": that.data.endDate,
    "PageIndex": UnCheckPageIndex,
    "PageSize": UnCheckPageSize,
  }];

  network.transfer_request(method, params, function (res) {
    successaction();
    var array = res.result;
    if (array.length <= 0) {
      wx.showToast({
        title: '无数据更新',
      })
      UnCheckPageIndex = UnCheckPageIndex - 1;
      return;
    }
    for (var i = 0; i < array.length; i++) {
      var dmo = {
  
        orderState: "未确认",
        ID: array[i].ID,
        Money: array[i].Money,
        Date: timechage.formatTimeTwo(array[i].Date.substring(6, 19), 'Y/M/D'),
        Employee_Name: array[i].Employee_Name,
      }
      getArr.push(dmo);
    }
    let arrLast = that.data.unCheckDataArr.concat(getArr);
    that.setData({
      unCheckDataArr: arrLast,
    })
  })
}

// 已确认订单列表
function getCheckedOrderList(that, CheckedPageIndex, CheckedPageSize) {
  let getArr = [];
  let method = getList;
  let params = [{
    "Confirmed": true,
    "StartDate": that.data.startDate,
    "EndDate": that.data.endDate,
    "PageIndex": UnCheckPageIndex,
    "PageSize": UnCheckPageSize,
  }];
  // let params = [true, CheckedPageIndex, CheckedPageSize];
  network.transfer_request(method, params, function (res) {
    var array = res.result;
    if (array.length <= 0) {
      wx.showToast({
        title: '无数据更新',
      })
      CheckedPageIndex = CheckedPageIndex - 1;
      return;
    }
    for (var i = 0; i < array.length; i++) {
      var dmo = {
        //添加单据状态的传递，在详情页面判断此值，进行是否允许提交的操作
        orderState: "已确认",
        ID: array[i].ID,
        Money: array[i].Money,
        Date: timechage.formatTimeTwo(array[i].Date.substring(6, 19), 'Y/M/D'),
        Employee_Name: array[i].Employee_Name,

      }
      getArr.push(dmo);
    }
    let arrLast = that.data.checkedDataArr.concat(getArr);
    that.setData({
      checkedDataArr: arrLast,
    })
  })
}

Page({
  data: {
    checkedDataArr: [],
    unCheckDataArr: [],
    currentTab: 0,
    winHeight: app.globalData.winHeight,
    startDate: null,
    endDate:null,
  },

  /**
   * 滑动切换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 ID = event.currentTarget.dataset.detailitemid;
    var State = event.currentTarget.dataset.detailitemstate;
    wx.navigateTo({
      url: 'debitNoteDetail/debitNoteDetail?ID=' + ID + '&State=' + State,
    })
  },

  //点击进入筛选页面
  chose: function (e) {
    wx.navigateTo({
      url: 'debitNoteQuery/debitNoteQuery',
    })
  },

  onShow: function (options) {
    this.setData({
      checkedDataArr: [],
      unCheckDataArr: [],
    })
    var that = this;
    UnCheckPageIndex = 0;
    CheckedPageIndex = 0;
    getUnCheckOrderList(that, UnCheckPageIndex, UnCheckPageSize, function (res) {
      getCheckedOrderList(that, CheckedPageIndex, CheckedPageSize);

    });
  },


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

    });
  },

  //已确认订单滑动到底部加载
  CheckedScrollLower: function (event) {
    var that = this;
    CheckedPageIndex = CheckedPageIndex + 1;
    getCheckedOrderList(that, CheckedPageIndex, CheckedPageSize);
  },
  
})
  