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/Employee/SaleForecastRpc/GetList'

var deleteItem = "/MainSystem/B3MiniProgramRpc/XuRpcs/Employee/SaleForecastRpc/Delete"

// 未审核订单列表
function getUnCheckOrderList(that, UnCheckPageIndex, UnCheckPageSize, successaction) {
  let getArr = [];
  let method = getList;
  let params = [{
    "BillState": 0,
    "Customer_ID": that.data.Customer_ID,
    "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 = {
        // BillState: "未审核"
        // Customer_Name: "陈旭辉"
        // Date: "/Date(1540453380000+0800)/"
        // DepartmentWorkFlow_Detail_Name: "初始"
        // DepartmentWorkFlow_ID: 2
        // ID: 4
        // Money: 143
        // Number: 1
        // billType: array[i].BillType_Name,
        //添加单据状态的传递，在详情页面判断此值，进行是否允许提交的操作 true : 可提交
        orderState: true,
        orderID: array[i].ID,
        Customer_Name: array[i].Customer_Name,
        time: timechage.formatTimeTwo(array[i].Date.substring(6, 19), 'Y/M/D'),
        workFlowName: array[i].DepartmentWorkFlow_Detail_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 = [{
    "BillState": 20,
    "Customer_ID": that.data.Customer_ID,
    "PageIndex": CheckedPageIndex,
    "PageSize": CheckedPageSize,
  }];
  // 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: false,
        orderID: array[i].ID,
        Customer_Name: array[i].Customer_Name,
        time: timechage.formatTimeTwo(array[i].Date.substring(6, 19), 'Y/M/D'),
        workFlowName: array[i].DepartmentWorkFlow_Detail_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,
    Customer_ID: null,
  },

  createNew: function(e) {
    wx.navigateTo({
      url: 'newBill/newBill',
    })
  },

  /**
   * 滑动切换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: 'Detail/Detail?ID=' + ID + '&State=' + State,
    })
  },

  // 长按删除一条单据
  longPressDelete: function(event) {
    var that =this;
    var itemID = event.currentTarget.dataset.detailitemid;
    var state = event.currentTarget.dataset.detailitemstate;
    if (!state) {
      wx.showModal({
        title: '提示',
        content: '不能操作已审核单据',
      })
      return;
    }
    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;
        }

      }
    })

  },

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

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

    });
  },

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

    app.globalData.selectCustomer_ID = null
    app.globalData.selectStartDate = null
    app.globalData.selectEndDate = null
    app.globalData.selectBillState = 0
  },
  //未审核订单滑动到底部加载
  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);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  // 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);
  //   }

  // },
})