// pages/catelist/catelist.js
var app = getApp();
var n = 1;
Page({

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

    type: null
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  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) {
      try {
        this.setData({
          id: JSON.parse(options.id)
        });
      } catch (e) {
        this.setData({
          id: options.id
        });
      }
    }
    if (options.coupon) {
      this.setData({
        coupon: options.coupon
      });
    }
    if (options.type) {
      this.setData({
        type: options.type
      });
    }
    if (options.keyword){
      this.setData({
        inputs : options.keyword
      });
    }
    if (options.order_field){
      this.setData({
        order_field : options.order_field
      });
    }
    if (options.order_by){
      this.setData({
        order_by : options.order_by
      });
    }
    this.gotoSearch(this.data.page,true);
  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {
    console.log(this.data.allLoaded);
    if (this.data.allLoaded) {
      console.log('没有更多数据');
      wx.showToast({
        title: '没有更多数据',
        icon:'none',
        duration:1000
      });
    } else {
      let pagein = this.data.page+1;
      this.setData({
        page: pagein
      });
      this.gotoSearch(pagein, true);
    }
  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {
    let urlStr = "";
    urlStr += "?keyword=" + this.data.inputs;
    urlStr += "&id=" + this.data.id;
    urlStr += "&order_field=" + this.data.order_field;
    urlStr += "&order_by=" + this.data.order_by;
    urlStr += "&coupon=" + this.data.coupon;
    urlStr += "&type=" + this.data.type;
    return {
      path:"/packageB/member/category/catelist/catelist"+urlStr
    };
  },
  toggle(e) {
    let idx = e.currentTarget.dataset.idx;
    let order_field = '';
    switch (idx) {
    case 1:
      this.setData({
        hascolor: 1,
        page: 1
      });
      if (this.data.colorshow == '1_2') {
        this.setData({
          colorshow: '1_1'
        });
        order_field = 'comment_num';
        this.setData({
          order_field: order_field,
          order_by: 'asc'
        });
      } else {
        this.setData({
          colorshow: '1_2'
        });
        order_field = 'comment_num';
        this.setData({
          order_field: order_field,
          order_by: 'desc'
        });
      }
      break;
    case 2:
      this.setData({
        hascolor: 2,
        page: 1
      });
      if (this.data.colorshow == '2_2') {
        this.setData({
          colorshow: '2_1',
        });
        order_field = 'show_sales';
        this.setData({
          order_field: order_field,
          order_by: 'asc'
        });
      } else {
        this.setData({
          colorshow: '2_2',
        });
        order_field = 'show_sales';
        this.setData({
          order_field: order_field,
          order_by: 'desc'
        });
      }
      break;
    case 3:
      this.setData({
        hascolor: 3,
        page: 1
      });
      if (this.data.colorshow == '3_2') {
        this.setData({
          colorshow: '3_1'
        });
        order_field = 'price';
        this.setData({
          order_field: order_field,
          order_by: 'asc'
        });
      } else {
        this.setData({
          colorshow: '3_2'
        });
        order_field = 'price';
        this.setData({
          order_field: order_field,
          order_by: 'desc'
        });
      }
      break;
    }
    if (this.data.datas.length == 0) {
      return;
    }
    this.setData({
      datas: []
    });
    console.log(this.data.page);
    this.gotoSearch(this.data.page);
  },
  gotoSearch(page, flag) {
    let that = this;
    let urlStr = null;
    if(this.data.type == 'snatchRegiment'){
      urlStr = app.getNetAddresss("plugin.snatch-regiment.api.index.getSnatchData");
      urlStr += "&art=catGoods";
    }else{
      urlStr = app.getNetAddresss("goods.goods.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 += "&search[as_id]=" + this.data.zoneId;
    app._getNetWork({
      showToastIn: false,
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.data.loading = false;
          that.data.allLoaded = false;
          // 搜索结果为空
          let dataLength = that.data.type == 'snatchRegiment'?res.data.catGoods.goods.data:res.data.data;
          if (dataLength.length <= 0) {
            that.data.loading = true;
            that.data.allLoaded = true;
            return;
          }
          console.log(res.data);
          // 如果当前页数大于总页数 就不加载了
          if(that.data.type == 'snatchRegiment'){
            if (res.data.catGoods.goods.current_page > res.data.catGoods.goods.last_page) {
              that.data.loading = true;
              that.data.allLoaded = true;
              return;
            }
          }
          
          let datas = [];
          if(flag){
            datas = that.data.datas;
            if(that.data.type == 'snatchRegiment'){
              datas.push(...res.data.catGoods.goods.data);
            }else{
              datas.push(...res.data.data);
            }
            that.setData({
              show:res.data.goods_template=='02'?false:true,
            });
          }else{
            datas = that.data.type == 'snatchRegiment'?res.data.catGoods.goods.data:res.data.data;
            try{
              that.setData({
                goods_template:res.data.goods_template,
                show:res.data.goods_template=='02'?false:true
              });
            }catch(err){
              console.log(err);
            }
          }
          for(let i = 0;i<datas.length;i++){
            console.log(parseInt(Number(datas[i].market_price == 0)));
            if (parseInt(Number(datas[i].market_price == 0))){
              datas[i].notMarket_price = 0;
            }else{
              datas[i].notMarket_price = 1;
            }
          }
          that.setData({
            datas: datas
          });
          if(res.data.current_page >= res.data.last_page){
            that.data.loading = true;
            that.data.allLoaded = true;
            return;
          }
          if (dataLength.length < 10) {
            that.data.loading = true;
            that.data.allLoaded = true;
            return;
          }
        } else {
          that.setData({
            datas:[]
          });
          that.data.loading = true;
          that.data.allLoaded = true;
        }
      },
      fail: function(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
    });
  },
  inpbtnclear() {
    this.setData({
      inputs: ''
    });
  },
  search() {
    n = 1;
    this.setData({
      datas: []
    });
    this.gotoSearch(n, false);
  },
});
