// pages/goodaNameChooseTemplate/goodaNameChooseTemplate.js
var app = getApp()
var network = require("../../utils/net.js")
var dateTimePicker = require('../../utils/dateTimePicker.js');
var utilll = require('../../utils/util.js');

var unitID = null;
var customerID = null;

var getGoods = "/MainSystem/B3MiniProgramRpc/Rpcs/BaseInfoRpc/GetGoodsWithUnitPrice";

Page({

  data: {
    winHeight: app.globalData.winHeight,
    currentTab: 0,
    collectionArray: [],
    goodsArray: [],
    checkBoxArray: [],
    storageCheckBoxArray: [],
  },

  getGoodsNameBySearchString: function (event){
    var searchString = event.detail.value;
    this.GetGoodsWithUnitPrice(unitID, customerID, searchString);
     
  },

  GetGoodsWithUnitPrice: function(unitID, customerID, searchString) {
    var that = this;
    let timestamp = Date.parse(new Date());
    let date = "/Date(" + timestamp + "+0800)/";
    let method = getGoods;
    let params = [{
      "Input": searchString,
      "PageIndex": 0,
      "PageSize": 100,
      "Customer_ID": parseInt(customerID),
      "AccountingUnit_ID": parseInt(unitID),
      "Date": date
    }];
    network.transfer_request(method, params, function(res) {
      that.setData({
        goodsArray: res.result,
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // 根据客户id和会计单位获取存货及价格
    var that = this;
    unitID = options.unitID;
    customerID = options.customerID;
    this.GetGoodsWithUnitPrice(unitID, customerID, "");
    // 获取收藏的存货信息
    wx.getStorage({
      key: 'storageGoodsKey',
      success(res) {
        console.log(res.data);
        if (res.data.length > 0) {
          that.setData({
            collectionArray: res.data,
          })
        }
      }
    })
  },

  storegeBtnClick: function() {
    var that = this;

    // 获取收藏的存货信息
    var oldCollectionArr = this.data.collectionArray;
    var waitStorageArr = this.data.checkBoxArray;
    var goodsNameArrayAll = this.data.goodsArray;

    if (waitStorageArr.length <= 0) {
      wx.showToast({
        title: '请选择存货',
      })
      return;
    } else {

      // 对比所选存货是否已经存在于收藏中 有：不重复添加 返回未添加过的数组
      for (var i = 0; i < waitStorageArr.length; i++) {
        for (var j = 0; j < oldCollectionArr.length; j++) {
          if (Number(waitStorageArr[i]) == Number(oldCollectionArr[j].SaleGoods_ID)) {
            waitStorageArr.splice(i, 1);
          }
        }
      }

      // 对比原有数组和选中的数组 存在，将数组包含的其他值也取出加载
      let choseCheck = [];
      for (var i = 0; i < that.data.goodsArray.length; i++) {
        for (var j = 0; j < waitStorageArr.length; j++) {
          if (Number(goodsNameArrayAll[i].SaleGoods_ID) == waitStorageArr[j]) {
            choseCheck.push(goodsNameArrayAll[i])
          }
        }
      }
      oldCollectionArr = oldCollectionArr.concat(choseCheck);
      that.setData({
        collectionArray: oldCollectionArr,
        currentTab: 1,
      });
      wx.setStorage({
        key: "storageGoodsKey",
        data: oldCollectionArr,
        success: function() {
          wx.showToast({
            title: '收藏成功',
          });
        },
      })
    }
  },


  // 多选框数值变化
  checkboxValueChange: function(e) {
    this.setData({
      checkBoxArray: e.detail.value,
    })
  },

  // 收藏夹页面多选框值变化
  storageCheckboxValueChange:function (e){
    this.setData({
      storageCheckBoxArray: e.detail.value,
    })
  },

  // 收藏夹点击添加存货明细按钮 存数据进入global
  storageAddGoodsNameArrayBack: function () {
    let choseCheck = []
    // 对比原有数组和选中的数组 存在，将数组包含的其他值也取出加载
    for (var i = 0; i < this.data.collectionArray.length; i++) {
      for (var j = 0; j < this.data.storageCheckBoxArray.length; j++) {
        if (Number(this.data.collectionArray[i].SaleGoods_ID) == this.data.storageCheckBoxArray[j]) {
          choseCheck.push(this.data.collectionArray[i])
        }
      }
    }
    app.globalData.goodsNameArray = choseCheck;

    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];  //当前页面
    var prevPage = pages[pages.length - 2];  //上一个页面
    //直接调用上一个页面对象的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      backPage: "goodsNameChoosePage",
    });
    
    wx.navigateBack({
      delta: 1,
    })
  },

  // 全部页面点击添加存货明细按钮 存数据进入global
  addGoodsName: function() {
    let choseCheck = []
    // 对比原有数组和选中的数组 存在，将数组包含的其他值也取出加载
    for (var i = 0; i < this.data.goodsArray.length; i++) {
      for (var j = 0; j < this.data.checkBoxArray.length; j++) {
        if (Number(this.data.goodsArray[i].SaleGoods_ID) == this.data.checkBoxArray[j]) {
          choseCheck.push(this.data.goodsArray[i])
        }
      }
    }
    app.globalData.goodsNameArray = choseCheck;

    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];  //当前页面
    var prevPage = pages[pages.length - 2];  //上一个页面
    //直接调用上一个页面对象的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      backPage: "goodsNameChoosePage",
    });

    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 滑动切换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
      })
    }
  },

})