// packageH/newRetail/newRetailInventory/newRetailInventory.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue: "",
    goodsList: [],
    temArr: [],
    value: 1,
    selectGoods: [],
    showPrice: false,

    //more
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0 //总页数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getGood();
  },
  gotoUrl(e) {
    let good = e.currentTarget.dataset.info;
    console.log(good);
    wx.navigateTo({
      url: '/packageI/newRetailInventoryDetails/newRetailInventoryDetails?gid=' + good.goods_id + '&optionId=' + good.goods_option,
    });
  },
  getGood() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.new-retail.frontend.index.getStock');
    app._postNetWork({
      url: urlStr,
      data: {
        goods_name: this.data.searchValue
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let goodsList = res.data.data;
          goodsList.forEach(tem => {
            tem.goods_num = 1;
            tem.goods_price = null;
          });
          that.setData({
            goodsList: goodsList,
            isLoadMore: true,
            total_page: res.data.last_page
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.msg);
      }
    });
  },
  _getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      // that.loading = true;
      return;
    } else {
      this.data.page += 1;
      let urlStr = app.getNetAddresss('plugin.new-retail.frontend.index.getStock');
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page,
          goods_name: this.data.searchValue
        },
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            var nextPageData = res.data.data;
            nextPageData.forEach(tem => {
              tem.goods_num = 1;
              tem.goods_price = null;
            });
            let goodsList = this.data.goodsList.concat(nextPageData);
            this.setData({
              goodsList
            });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        }
      });
    }
  },
  onChangeStepper(e) {
    let index = e.currentTarget.dataset.i;
    this.setData({
      ['goodsList[' + index + '].goods_num']: e.detail
    });
  },
  onSearchChange(e) {
    this.setData({
      searchValue: e.detail,
    });
  },
  selectGoodsCheckbox(e) {
    this.setData({
      selectGoods: e.detail
    });

  },
  agentSalePopup() {
    if (this.data.selectGoods.length < 1) {
      wx.showToast({
        icon: 'none',
        title: "请选择商品",
        duration: 1500
      });
      return;
    }
    let temArr = [];
    this.data.goodsList.forEach((tem, index) => {
      let i = index + '';
      if (this.data.selectGoods.indexOf(i) > -1) {
        temArr.push(tem);
      }
    });

    this.setData({
      showPrice: true,
      temArr
    });
    console.log(this.data.temArr);
  },
  blurPrice(e) {
    let index = e.currentTarget.dataset.i;
    this.setData({
      ['temArr[' + index + '].goods_price']: e.detail.value
    });
  },
  delSelectGood(e) {
    let index = e.currentTarget.dataset.i;
    this.data.selectGoods.splice(index, 1);
    this.data.temArr.splice(index, 1);
    this.setData({
      selectGoods: this.data.selectGoods,
      temArr: this.data.temArr
    });
    if (this.data.selectGoods.length < 1) {
      wx.showToast({
        icon: 'none',
        title: "请选择商品",
        duration: 1500
      });
      this.setData({
        showPrice: false
      });
    }
  },
  onClose() {
    this.setData({
      showPrice: false
    });
  },
  confirmAgentSale() {
    if (this.data.selectGoods.length < 1) {
      wx.showToast({
        icon: 'none',
        title: "请选择商品",
        duration: 1500
      });
      return;
    }
    for (let i = 0; i < this.data.temArr.length; i++) {
      if (!this.data.temArr[i].goods_price || this.data.temArr[i].goods_price == "") {
        wx.showToast({
          icon: 'none',
          title: "有商品没有填写单价！",
          duration: 2000
        });
        return;
      }
    }

    let urlStr = app.getNetAddresss('plugin.new-retail.frontend.index.onlineRetail');
    app._postNetWork({
      url: urlStr,
      data: {
        goods: this.data.temArr
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          wx.navigateTo({
            url: '/packageH/newRetail/newRetailRetailLink/newRetailRetailLink?id=' + res.data.id
          });
        } else {
          wx.showToast({
            title: res.msg,
            duration: 1000,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.msg);
      }
    });
  },
  freeSend() {
    if (this.data.selectGoods.length < 1) {
      wx.showToast({
        icon: 'none',
        title: "请选择商品",
        duration: 1500
      });
      return;
    }
    let _goods = [];
    let temArr = [];
    this.data.goodsList.forEach((tem, index) => {
      let i = index + '';
      if (this.data.selectGoods.indexOf(i) > -1) {
        temArr.push(tem);
      }
    });
    temArr.forEach(item => {
      _goods.push({
        goods_id: item.goods_id,
        goods_option: item.goods_option,
        goods_num: item.goods_num
      });
    });
    let url = "/packageD/buy/myOrder_v2/myOrder_v2?tag=newRetail";
    console.log("xpc", JSON.stringify(_goods));
    wx.navigateTo({
      url: url + "&goodsId=0&optionsId=0&total=0&retailState=2" + "&goods=" + JSON.stringify(_goods)
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
});