// packageC/video_goods/addVideoGood/addVideoGood.js
var app = getApp();
const storeAlone = 'plugin.store-alone-temp.frontend.videoShare.goods.getGoodsByKeyword';
const getlistStoreAlone = "plugin.store-alone-temp.frontend.videoShare.goods.getRecommendGoods";
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() {

  },
  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 = '';
  },
  getData() {
    let that = this;
    that.data.isLoadMore = true;
    that.data.page = 1; 
    that.data.total_page = 0;
    that.data.loading = false;
    let urlStr = app.getNetAddresss(app.globalData.store_alone_temp == 1?storeAlone:"plugin.video-share.frontend.goods.getGoodsByKeyword");
    app._getNetWork({
      url: urlStr,
      data: {
        keyword: this.data.inputs
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            goodList: res.data.list.data
          });
          that.data.total_page = res.data.list.last_page;
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  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 {
      wx.redirectTo({
        url: '/packageC/video_goods/releaseVideo/releaseVideo?goods_id=' + this.data.chooseGoods
      });
    }
  },
  getRecommendGoods() {
    let that = this;
    let urlStr = app.getNetAddresss(app.globalData.store_alone_temp == 1?getlistStoreAlone:"plugin.video-share.frontend.goods.getRecommendGoods");
    app._getNetWork({
      url: urlStr,
      data: {
        page: 1
      },
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            goodList: res.data.list.data
          });
          that.data.total_page = res.data.list.last_page;
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  //获取更多数据
  getMoreData() {
    const that = this;
    // "plugin.video-share.frontend.goods.getRecommendGoods" : "plugin.video-share.frontend.goods.getGoodsByKeyword"
    console.log(this.data.inputs);
    let url = !this.data.inputs ? (app.globalData.store_alone_temp == 1?getlistStoreAlone:'plugin.video-share.frontend.goods.getRecommendGoods'):(app.globalData.store_alone_temp == 1?storeAlone:'plugin.video-share.frontend.goods.getGoodsByKeyword');
    let urlStr = app.getNetAddresss(url);
    let params = !this.data.inputs ? {
      page: that.data.page + 1
    } : {
      page: that.data.page + 1,
      keyword: this.data.inputs
    };
    that.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      that.setData({
        loading: true
      });
      return;
    } else {
      this.setData({
        page : this.data.page + 1,
      });
      console.log(this.data.page,params);
      app._getNetWork({
        url: urlStr,
        data: params,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            var myData = res.data.list.data;
            that.setData({
              goodList: that.data.goodList.concat(myData),
              total_page:res.data.list.last_page
            });
          } else {
            that.data.page = that.data.page - 1;
            that.data.loading = true;
            that.data.isLoadMore = false;
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  },
});
