// packageC/member/searchAll/searchAll.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    title: "搜索",
    inputs: "",
    amout: false,
    loading: false,
    goods: [],
    order_by: "",
    order_field: "",
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    classifyBol: true,
    show: false,
  },

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

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

  },

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

  },

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

  },

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

  },

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

  },

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

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

  },
  search() {
    this.setData({
      goods: []
    });
    this.getData();
  },
  getData() {
    let that = this;
    let urlStr = app.getNetAddresss("goods.goods.search-goods");
    urlStr += "&search[keyword]=" + this.data.inputs;
    urlStr += "&page=" + 1;
    urlStr += "&order_field=" + this.data.order_field;
    urlStr += "&order_by=" + this.data.order_by;
    app._getNetWork({
      showToastIn: false,
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            isLoadMore: true,
            total_page: res.data.last_page,
          });
          if (!that.data.total_page) {
            that.setData({
              total_page: 0
            });
          }
          that.setData({
            goods: res.data.data
          });
          try{
            that.setData({
              goods_template:res.data.goods_template
            });
            if (that.data.goods_template.names == '02' || that.data.goods_template.names == '03') {
              that.setData({
                show: true,
                classifyBol: false
              });
            }else{
              that.setData({
                show:false,
                classifyBol: true
              });
            }
          }catch(err){
            console.log(e);
          }
        }
      },
      fail: function(res) {}
    });
  },
  sortOut(e) {
    let order_by = e.detail.order_by;
    let order_field = e.detail.order_field;
    if (this.data.goods.length == 0) {
      return;
    }
    this.setData({
      goods: [],
      order_field: order_field,
      order_by: order_by
    });
    this.getData();
  },
  classifyBolbtn() {
    if (this.data.show) {
      this.setData({
        show: false,
        classifyBol: true
      });
    } else {
      this.setData({
        show: true,
        classifyBol: false
      });
    }
  },
  getMoreData() {
    let that = this;
    that.isLoadMore = false; // 防止多次请求分页数据
    let urlStr = app.getNetAddresss("goods.goods.search-goods");
    urlStr += "&search[keyword]=" + this.data.inputs;
    urlStr += "&order_field=" + this.data.order_field;
    urlStr += "&order_by=" + this.data.order_by;
    if (this.data.page >= this.data.total_page) {
      that.setData({
        loading: true
      });
      return;
    } else {
      that.setData({
        page: this.data.page + 1
      });
      urlStr += "&page=" + this.data.page;
      app._getNetWork({
        showToastIn: false,
        url: urlStr,
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            var myData = res.data.data;
            that.setData({
              goods: that.data.goods.concat(myData)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              loading: true,
              isLoadMore: false
            });
          }
        },
        fail: function(res) {

        }
      });
    }
  },
  inpbtn(e) {
    let val = e.detail.value;
    this.setData({
      inputs: val
    });
  },
  inpbtnclear() {
    this.setData({
      inputs: ''
    });
  },
});
