// packageH/circleCommunity/circleVideogood/circleVideogood.js

var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    language: '',
    inputsData: '',
    show1: false,
    goodList: [],
    chooseGoods: null,
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    loading: false
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this.initData();
    this.getRecommendGoods();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

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

  },

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

  },

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

  },

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

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    if (this.data.loading) {
      console.log('没有更多数据');
    } else {
      this.getMoreData();
    }
  },

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

  },
  onShareTimeline(){},
  initData() {
    this.setData({
      inputsData: '',
      show1: false,
      goodList: [],
      chooseGoods: null
    });
    this.data.inputs = '';
  },
  inpbtn(e) {
    let val = e.detail;
    this.data.inputs = val;
  },
  inpbtnclear() {
    this.setData({
      inputsData: ''
    });
    this.data.inputs = '';
  },
  chooseGood(e) {
    let _id = e.currentTarget.dataset.id;
    this.setData({
      chooseGoods: _id
    });
  },
  gotoRelease() {
    console.log(app._isTextEmpty(this.data.chooseGoods));
    if (app._isTextEmpty(this.data.chooseGoods)) {
      wx.showToast({
        title: '选择的商品为空',
        icon: 'none',
        duration: 1500
      });
      return;
    } else {
      let pages = getCurrentPages();
      let page = pages[pages.length-2];
      page.setGoodsId(this.data.chooseGoods);
      wx.navigateBack();
      // wx.navigateTo({
      // 	url: '/packageC/video_goods/releaseVideo/releaseVideo?goods_id=' + this.data.chooseGoods
      // })
    }
  },
  getRecommendGoods() {
    this.data.isLoadMore = true;
    this.data.page = 1;
    this.data.total_page = 0;
    this.data.loading = false;

    let urlStr = app.getNetAddresss("plugin.circle.frontend.circle-invitation.search-goods");
    app._getNetWork({
      url: urlStr,
      data: {
        page: 1,
        goods_name:this.data.inputs
      },
      success: (resdata)=> {
        var res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          this.setData({
            goodList: res.data.data
          });
          this.data.total_page = res.data.last_page;
        } else {
          app.tips(res.msg);
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    let url =  "plugin.circle.frontend.circle-invitation.search-goods";
    let urlStr = app.getNetAddresss(url);
    let params = {goods_name:this.data.inputs,page: this.data.page};
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      this.setData({
        loading: true
      });
      return;
    } else {
      this.data.page = this.data.page + 1;
      app._getNetWork({
        url: urlStr,
        data: params,
        success: (resdata)=> {
          var res = resdata.data;
          if (res.result == 1) {
            var myData = res.data.data;
            this.setData({
              goodList: this.data.goodList.concat(myData)
            });
          } else {
            this.data.page = this.data.page - 1;
            this.data.loading = true;
            this.data.isLoadMore = false;
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },
});





