// pages/allGoods/allGoods.js
var http = require('../../utils/http.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topFlag: false,
    shopInfo: {},
    shopProdList: [],
    shopId: 0,
    sort: 0,
    current: 1, // 当前页
    pages: 0, // 总页码
    isAll: false,
    currentTab: true
  },


  /**
   * tab栏切换
   */
  onStsTap: function (e) {
    var sort = e.currentTarget.dataset.sort;
    this.setData({
      sort: sort,
      currentTab: false,
      current: 1,
      isAll: false
    });
    this.getShopProds()
  },

  /**
   * 跳转搜索页
   */
  toShopSearchPage: function (e) {
    const shopId = e.currentTarget.dataset.shopid
    wx.navigateTo({
      url: '/pages/shopSearch/shopSearch?shopId=' + shopId,
    })
  },


  // 获取店铺商品
  getShopProds () {
    wx.showLoading()
    var params = {
      url: '/search/searchProdPage',
      method: 'GET',
      data: {
        shopId: this.data.shopInfo.shopId,
        sort: this.data.sort,
        current: this.data.current,
        isAllProdType: true,
      },
      callBack: (res) => {
        wx.hideLoading()
        var shopProdList = []
        if (this.data.current == 1) {
          this.setData({
            shopProdList: res.records,
            pages: res.pages,
            current: res.current
          })
        } else {
          shopProdList = this.data.shopProdList
          shopProdList.push(...res.records)
          this.setData({
            shopProdList
          })
        }
      }
    }
    http.request(params)
  },

  // 触底加载下一页
  getNextPage () {
    if (this.data.pages > this.data.current) {
      this.setData({
        current: this.data.current + 1
      })
      this.getShopProds()
    } else {
      this.setData({
        isAll: true
      })
    }
  },


  // 跳转商品详情
  toProdPage: function (e) {
    var prodid = e.currentTarget.dataset.prodid;
    if (prodid) {
      wx.navigateTo({
        url: '/pages/prod/prod?prodid=' + prodid,
      })
    }
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.shopId) {
      this.setData({
        shopId: options.shopId
      })
    } else {
      this.setData({
        shopId: this.data.shopInfo.shopId
      })
    }
    this.setData({
      shopInfo: wx.getStorageSync("shopInfo")
    })

    this.getShopProds()

  },

  /**
   * 跳转店铺详情
   */
  toShopInfo() {
    wx.navigateTo({
      url: '/pages/shopInfo/shopInfo?shopId=' + this.data.shopInfo.shopId,
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getNextPage()
  },

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

  },

  /**
   * 页面滚动事件
   */
  onPageScroll: function (e) {
    var _this = this
    if (e.scrollTop > 80) {
      _this.setData({
        topFlag: true
      })
    } else {
      _this.setData({
        topFlag: false
      })
    }
  }
})