// packageI/warehouseFarm/warehouseFarmCreate/warehouseFarmCreate.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showInstructions: false,
    explain: null,
    stock: {},
    num: null,
    share_num: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.warehouse.frontend.controllers.gift.give.instructions");
    app._postNetWork({
      url: urlStr,
      data: {
        stock_id: this.options.id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            explain: res.data.explain,
            stock: res.data.stock
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            });
          }, 1500);
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  creatGift() {
    if (!this.data.num) {
      wx.showToast({
        icon: "none",
        title: "请输入数量",
        duration: 1000,
      });
      return;
    }
    if (!this.data.share_num) {
      wx.showToast({
        icon: "none",
        title: "请输入分享人数",
        duration: 1000,
      });
      return;
    }
    if (Number(this.data.num) > Number(this.data.stock.nums)) {
      wx.showToast({
        icon: "none",
        title: `已超出范围，当前库存为${this.data.stock.nums}`,
        duration: 1000,
      });
      this.setData({
        num: this.data.stock.nums
      });
      return;
    }
    if (Number(this.data.num) < Number(this.data.share_num)) {
      wx.showToast({
        icon: "none",
        title: `赠送数量需要大于分享人数`,
        duration: 1000,
      });
      return;
    }
    wx.navigateTo({
      url: '/packageI/warehouseFarm/warehouseFarmGiftDetail/warehouseFarmGiftDetail?mode=creat&gid=' + this.data.stock.id + '&nums=' + this.data.num + '&sNum=' + this.data.share_num
    });
  },
  toUrl(e) {
    let _data = e.currentTarget.dataset || {};
    this.wxRouterLink(_data);
  },
  wxRouterLink(_data) {
    let _Param = '';
    const {
      url,
      ...otherParam
    } = _data;
    Object.keys(otherParam).forEach(function (key) {
      if (otherParam[key] !== '') _Param += key + '=' + otherParam[key] + '&';
    });
    if (url === '') return;
    wx.navigateTo({
      url: url + '?' + _Param.slice(0, -1)
    });
  },
  openPopup(e) {
    let _params = e.currentTarget.dataset;
    if (_params.tag) {
      let _tag = _params.tag;
      this.setData({
        [_tag]: _params.set
      });
    }
  },
  bindinput(e) {
    this.setData({
      num: e.detail.value
    });
  },
  bindinputSnum(e) {
    this.setData({
      share_num: e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  }
});