// pages/store/store.js
const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 21,
    storeList: [],
    hasData: true,
    loading: false,
    fetcherData: {
      type: 21,
      address: '',
      page: 1,
      size: 20,
      name: ''
    }
  },
  gotoWV: function (event) {
    let {id,index} = event.detail
    wx.navigateTo({
      url: `/pages/webview/index?id=${id}&type=${this.data.fetcherData.type}`,
    })
    util.request(api.increaseViewCount, {
      brandId: id,
      type:this.data.fetcherData.type
    }, 'GET').then((res) => {
        var num =  'storeList['+index+'].pvTotalNum'
        this.setData({
          [num]:res.data
        })
    });

  },
  toSearch(e) {
    var type = e.currentTarget.dataset.type
    console.log(type)
    wx.navigateTo({
      url: `/pages/searchRoom/searchRoom?type=` + type
    })
  },
  getStoreList() {
    if (this.data.hasData) {
      if (!this.data.loading) {
        this.setData({
          loading: true
        })
        wx.showLoading({
          title: '加载中...',
        })
        util.request(api.BrandList, this.data.fetcherData).then((res) => {
          this.setData({
            loading: false
          })
          if (res.errno === 0) {
            if (res.data.data.length != 0) {
              this.setData({
                storeList: this.data.storeList.concat(res.data.data),
                'fetcherData.page': this.data.fetcherData.page + 1
              })

            } else {
              this.setData({
                hasData: false
              })

            }
          }
        }).catch(err => {
          this.setData({
            loading: false
          })
        });
      }

    } else {
      console.log('没有更多数据')
    }

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

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      storeList: [],
      'fetcherData.page': 1,
      hasData: true
    });
    this.getStoreList()
    app.onPullDownRefresh()
  },
  onReachBottom: function () {
    this.getStoreList()
    console.log('onReachBottom')
  },

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

  }
})