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

Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    brand_id: '',
    zoneId: '',
    inputs: '',
    datas: [],
    classifyBol: true,
    show: false,
    order_by: '',
    order_field: '',
    loading: false,
    page:1,
    allLoaded: false
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    try {
      const value = wx.getStorageSync('zoneId');
      if (value) {
        this.setData({
          zoneId: value
        });
        // Do something with return value
      }
    } catch (e) {
      // Do something when catch error
    }
    if (options.id) {
      this.setData({
        brand_id: options.id
      });
    }
    this.gotoSearch();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  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() {

  },
  gotoSearch(page = 1) {
    let that = this;
    let urlStr = app.getNetAddresss("goods.goods.get-goods-brand-list");
    urlStr += "&keywords=" + this.data.inputs;
    urlStr += "&page=" + page;
    urlStr += "&brand_id=" + this.data.brand_id;
    urlStr += "&as_id=" + this.data.zoneId;
    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({
            loading: false,
            allLoaded: false
          });
          // 搜索结果为空
          if (res.data.goods.data.length <= 0) {
            that.setData({
              loading: true,
              allLoaded: true
            });
            return;
          }
          // 如果当前页数大于总页数 就不加载了
          if (res.data.goods.current_page > res.data.goods.last_page) {
            that.setData({
              loading: true,
              allLoaded: true
            });
            return;
          }
          let datas = that.data.datas;
          datas.push(...res.data.goods.data);
          that.setData({
            datas: datas
          });
          try{
            for(let i = 0 ; i<datas.length;i++){
              if(Number(datas[i].market_price) == 0){
                datas[i].notMarket_price = 0;
              }else{
                datas[i].notMarket_price = 1;
              }
              that.setData({
                datas: datas
              });
            }
          }catch(err){
            console.log(err);
          }
          try{
            that.setData({
              goods_template:res.data.goods.goods_template
            });
            console.log(that.data.goods_template);
          }catch(err){
            console.log(e);
          }
          if (that.data.goods_template.names == '02') {
            that.setData({
              showcccc: true,
              classifyBol: false
            });
          }
          if (res.data.goods.data.length < 20) {
            that.setData({
              loading: true,
              allLoaded: true
            });
            return;
          }
        }
      },
      fail: function(res) {

      }
    });
  },
  inpbtn(e) {
    let val = e.detail.value;
    this.setData({
      inputs: val
    });
  },
  inpbtnclear() {
    this.setData({
      inputs: ''
    });
  },
  search() {
    this.data.page = 1;
    this.setData({
      datas: []
    });
    this.gotoSearch(this.data.page, false);
  },
  classifyBolbtn() {
    if (this.data.show) {
      this.setData({
        show: false,
        classifyBol: true
      });
    } else {
      this.setData({
        show: true,
        classifyBol: false
      });
    }
  },
  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);
  },
});
