// index.js
//获取应用实例
var app = getApp();
let host = app.globalData.servsers; // 请求的url
Page({
  data: {
    host:host,
    classifyNavList:[],
    classifyNavActiveId: 2981,
    goodsList:[],
    commodity_levelTwo:null,
    requestState:true, //默认为true,表示还有数据
    page:0,
    filterArray: [
      { name: '1', value: '0~499元' },
      { name: '2', value: '500~999元'},
      { name: '3', value: '1000~1999元' },
      { name: '4', value: '2000元以上' },
    ],
    filterStatic:false,
    isNew:true,
    isMin:true,
  },
  onLoad(){
    this.getNavList(this);
    this.getGoodsListAllfor(this)
  },
  onShow(){

  },
  onReachBottom: function () {
    if (this.data.requestState) {
      this.setData({
        page: this.data.page + 1
      })
      this.getGoodsList(this)
    }
    console.log('到底了')
  },
  getNavList(that){
    wx.request({
      url: host + '/classifyapi/findAllforhome',
      method:'get',
      success(res){
        console.log(res,"成功后的")
        that.setData({
          classifyNavList:[...res.data.rows]
        })
      }
    })
  },
  classifyTap(e){
    console.log(e,"classifyTap")
    let item = e.currentTarget.dataset.item,that = this;
    that.setData({
      classifyNavActiveId:item.id,
      requestState: true, //数据初始化
      page: 0,//数据初始化
      goodsList: []//数据初始化
    })
    that.getGoodsListAllfor(that)
    
  },
  getGoodsListAllfor(that){
    wx.request({
      url: host +'/classifyapi/findAllforviewSce',
      method:'get',
      data:{
        id:that.data.classifyNavActiveId
      },
      success(res){
        that.setData({
          commodity_levelTwo:res.data.rows[0].id,
        })
        that.getGoodsList(that)
      }
    })
  },
  getGoodsList(that){
    let goodsList = that.data.goodsList, requestState = true
    wx.request({
      url: host + '/commodityapi/commoditviewfoeeverybody?commodity_levelTwo=2983&offset=0&limit=10',
      method:'get',
      data:{
        commodity_levelTwo: that.data.commodity_levelTwo,
        offset:that.data.page,
        limit:10,
      },
      success(res){
        console.log(res)
        res.data.rows.length < 10 ? requestState = false : requestState = true
        goodsList.push(...res.data.rows)
        that.setData({
          goodsList: goodsList,
          requestState: requestState
        })
      }
    })
  },
  filterTap(){
    let that = this;
    that.setData({
      filterStatic: !that.data.filterStatic
    })
  },
  newTap(){
    this.setData({
      isNew:!this.data.isNew
    })
  },
  priceTap() {
    this.setData({
      isMin: !this.data.isMin
    })
  },
  close(){
    this.setData({
      filterStatic:false
    })
  }

})