// pages/micro_shop_share_carelist/micro_shop_share_carelist.js
var app = getApp();

Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    coupon: '',
    id: '',
    inputs: '',
    order_by: '',
    order_field: '',
    datas: [],
    classifyBol: true,
    show: false,
    //当前分页
    page:1
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.coupon) {
      this.setData({
        coupon: options.coupon
      });
    }
    if (options.id) {
      this.setData({
        id: options.id
      });
    }
    if (options.shop_id) {
      this.setData({
        shop_id: options.shop_id
      });
    }
    this.gotoSearch(this.data.page);
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

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

  },

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

  },

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

  },

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

  },

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

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

  },
  sortOut(e) {
    let order_by = e.detail.order_by;
    let order_field = e.detail.order_field;
    if (this.data.datas.length == 0) {
      return;
    }
    this.data.page = 1;
    this.setData({
      datas: [],
      order_field: order_field,
      order_by: order_by
    });
    this.gotoSearch(this.data.page);
  },
  gotoSearch(page = 1, j = true) {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.micro.frontend.controllers.MicroShop.search-goods');
    urlStr += "&search[keyword]=" + this.data.inputs;
    urlStr += "&page=" + page;
    urlStr += "&search[category]=" + this.data.id;
    urlStr += "&order_field=" + this.data.order_field;
    urlStr += "&order_by=" + this.data.order_by;
    urlStr += "&search[couponid]=" + this.data.coupon;
    urlStr += "&shop_id=" + this.data.shop_id;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            loading: false,
            allLoaded: false
          });
          // 搜索结果为空
          if (res.data.data.length <= 0) {
            that.setData({
              loading: true,
              allLoaded: true
            });
            return;
          }
          // 如果当前页数大于总页数 就不加载了
          if (res.data.current_page > res.data.last_page) {
            that.setData({
              loading: true,
              allLoaded: true
            });
            return;
          }
          let datas = that.data.datas;
          datas.push(...res.data.data);
          that.setData({
            datas: datas
          });
          if (res.data.data.length < 20) {
            that.setData({
              loading: true,
              allLoaded: true
            });
            return;
          }
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  classifyBolbtn() {
    if (this.data.show) {
      this.setData({
        show: false,
        classifyBol: true
      });
    } else {
      this.setData({
        show: true,
        classifyBol: false
      });
    }
  },
  inpbtn(e) {
    let val = e.detail.value;
    this.setData({
      inputs: val
    });
  },
  search() {
    this.data.page = 1;
    this.setData({
      datas: []
    });
    this.gotoSearch(this.data.page, false);
  },
});
