// packageI/warehouseFarm/warehouseFarmExtract/warehouseFarmExtract.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: "",
    list: [],
    result: [],
    checked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  initData() {
    this.data.list = [];
    this.data.result = [];
    this.data.checked = false;
    this.data.keyword = '';
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.warehouse.frontend.controllers.takeDelivery.list");
    app._postNetWork({
      url: urlStr,
      data: {
        keyword: this.data.keyword
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let list = res.data;
          for (let j = 0; j < list.length; j++) {
            list[j].total = 1;
          }
          this.setData({
            list: list
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  toOrder() {
    if (this.data.result.length == 0) {
      wx.showToast({
        icon: "none",
        title: '请选择礼物！',
        duration: 1000,
      });
      return;
    }
    let _json = [];
    for (let j = 0; j < this.data.result.length; j++) {
      let obj = {};
      obj["goods_id"] = this.data.list[Number(this.data.result[j])].id;
      obj["total"] = this.data.list[Number(this.data.result[j])].total;
      obj["option_id"] = 0;
      _json.push(obj);
    }
    let json_data = {
      tag: "warehouse"
    };
    json_data.goods = JSON.stringify(_json);
    let url = "/packageD/buy/myOrder_v2/myOrder_v2?tag=warehouse";
    wx.navigateTo({
      url: url + "&goodCollection=" + json_data.goods
    });
  },
  onChangeSearch(e) {
    this.setData({
      keyword: e.detail
    });
  },
  onSearch() {
    this.setData({
      list: []
    });
    this.getData();
  },
  checkChange(event) {
    if (this.data.list.length == 0) {
      return;
    }
    this.setData({
      checked: event.detail,
    });
    let _result = [];
    if (event.detail) {
      // 全选中
      this.data.list.forEach((item, index) => {
        _result.push(index.toString());
      });
    }
    this.setData({
      result: _result
    });
  },
  changeGroup(event) {
    this.setData({
      result: event.detail,
      checked: event.detail.length == this.data.list.length ? true : false
    });
  },
  onChangeStepper(e) {
    this.data.list[e.currentTarget.dataset.index].total = e.detail;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initData();
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});