// pages/order/order.js
const app = getApp();
var network = require("../../utils/net.js");
var timechage = require("../../utils/dateTimeUtil.js");
var PageSize = 10
var UnCheckPageIndex = 0
var CheckedPageIndex = 0
var getListPath = '/MainSystem/B3MiniProgramRpc/XuRpcs/Customer/OrderRpc/GetList'
var deleteItem = "/MainSystem/B3MiniProgramRpc/XuRpcs/Employee/OrderRpc/Delete"

// 未审核订单列表
function getUnCheckOrderList(that, UnCheckPageIndex, PageSize, successaction) {
  let getArr = [];
  let method = getListPath;
  let params = [{
    "BillState": 0,
    "StartLoadTime": app.globalData.selectStartDate,
    "EndLoadTime": app.globalData.selectEndDate,
    "Customer_ID": null,//app.globalData.userID
    "PageIndex": UnCheckPageIndex,
    "PageSize": PageSize,
  }];

  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: true,
        workFlowName: array[i].DepartmentWorkFlow_Detail_Name,
        orderID: array[i].ID,
        Customer_Name: array[i].Customer_Name,
        time: timechage.formatTimeTwo(array[i].LoadTime.substring(6, 19), 'Y/M/D')
      }
      getArr.push(dmo);
    }
    let arrLast = that.data.unCheckDataArr.concat(getArr);
    that.setData({
      unCheckDataArr: arrLast,
    })
  })
}

// 已审核订单列表
function getCheckedOrderList(that, CheckedPageIndex, PageSize) {

  let getArr = [];
  let method = getListPath;
  let params = [{
    "BillState": 20,
    "StartLoadTime": app.globalData.selectStartDate,
    "EndLoadTime": app.globalData.selectEndDate,
    "Customer_ID":  null, // app.globalData.userID,
    "PageIndex": CheckedPageIndex,
    "PageSize": PageSize,
  }];
  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: false,
        workFlowName: array[i].DepartmentWorkFlow_Detail_Name,
        orderID: array[i].ID,
        Customer_Name: array[i].Customer_Name,
        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: [],
    winHeight: app.globalData.winHeight,
    currentTab: 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
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      checkedDataArr: [],
      unCheckDataArr: [],
    })
    var that = this;

    UnCheckPageIndex = 0;
    CheckedPageIndex = 0;
    getUnCheckOrderList(that, UnCheckPageIndex, PageSize, function (res) {
      getCheckedOrderList(that, CheckedPageIndex, PageSize);

    });
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    app.globalData.selectStartDate = null
    app.globalData.selectEndDate = null

  },

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

    });
  },

  //已审核订单滑动到底部加载
  checkedScrollLower: function (event) {
    var that = this;
    CheckedPageIndex = CheckedPageIndex + 1;
    getCheckedOrderList(that, CheckedPageIndex, PageSize);
  },

  // 进入订单详情
  itemclick: function(e) {
    wx.navigateTo({
      url: 'detail/detail?ID=' + e.currentTarget.dataset.orderid,
    })
  },

  // 长按删除一条单据
  longPressDelete: function (event) {
    var that = this;
    var itemID = event.currentTarget.dataset.orderid;
    wx.showModal({
      title: '提示',
      content: '确定要删除' + itemID + '号单据吗？',
      success: function (res) {
        if (res.confirm) {
          var params = [itemID];
          network.transfer_request(deleteItem, params, function (res) {
            wx.showToast({
              title: '操作成功',
            })
            that.onShow();
          })
        } else if (res.cancel) {
          console.log('点击取消了');
          return false;
        }
      }
    })
  },

  createNew: function(e) {
    wx.navigateTo({
      url: 'newBill/newBill',
    })
  },
  chose: function(e) {
    wx.navigateTo({
      url: 'query/query',
    })
  },
  //   该方法绑定了页面滚动时的事件
  scroll: function(event) {
    this.setData({
      scrollTop: event.detail.scrollTop
    });
  },
})