// pages/shopPage/shopPage.js

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

  /**
   * 页面的初始数据
   */
  data: {
    shopId: 0,
    shopInfo: {},
    indicatorDots: true,
    indicatorColor: '#d1e5fb',
    indicatorActiveColor: '#1b7dec',
    autoplay: true,
    interval: 2000,
    duration: 1000,
    indexImgs: [],
    topFlag: false,
    isCollection: false,
    shopProdList: [],
  },

  //加载轮播图
  getIndexImgs() {
    var shopId = this.data.shopId
    //加载轮播图
    var params = {
      url: `/indexImgs/${shopId}`,
      method: "GET",
      data: {},
      callBack: (res) => {
        this.setData({
          indexImgs: res,
          // seq: res
        });
        wx.hideLoading();
      }
    };
    http.request(params);
  },

  // 跳转店铺详情页

  toShopInfo(e) {
    const shopId = e.currentTarget.dataset.shopid
    wx.navigateTo({
      url: '/pages/shopInfo/shopInfo?shopId=' + shopId,
    })
  },

  // 跳转店内搜索页

  toShopSearchPage: function(e) {
    wx.navigateTo({
      url: '/pages/shopSearch/shopSearch?shopId=' + e.currentTarget.dataset.shopid,
    })
  },


  // 跳转所有商品页
  toShopProds(e) {
    wx.navigateTo({
      url: '/pages/shopProds/shopProds?shopId=' + e.currentTarget.dataset.shopid,
    })
  },


  // 收藏/取消收藏
  addOrCannelCollection() {
    this.setData({
      isCollection: !this.data.isCollection
    })
  },


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

  // 获取店铺信息
  getShopInfo: function() {
    var ths = this;
    //热门搜索
    var params = {
      url: "/shop/headInfo",
      method: "GET",
      data: {
        shopId: ths.data.shopId
      },
      callBack: function(res) {
        wx.hideLoading()
        ths.setData({
          shopInfo: res,
        })
        wx.setStorageSync("shopInfo", res)
        ths.setNavTitle()
        ths.getIndexImgs()
        ths.getShopProds()
      },
    };
    http.request(params);
  },

  // 获取店铺商品
  getShopProds() {
    var params = {
      url: '/search/searchProdPage',
      method: 'GET',
      data: {
        shopId: this.data.shopId,
        channelId:wx.getStorageSync('channelId')||3,
        isAllProdType: true
      },
      callBack: (res) => {
        wx.hideLoading()
          this.setData({
            shopProdList: res.records
          })
      }
    }
    http.request(params)
  },

  // 设置页面标题
  setNavTitle() {
    wx.setNavigationBarTitle({
      title: this.data.shopInfo.shopName
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '',
      mask: true,
    })
    if (options.shopId) {
      this.setData({
        shopId: options.shopId
      })
      wx.setStorageSync("currShopId", options.shopId);
    } else {
      this.setData({
        shopId: wx.getStorageSync("currShopId")
      })
    }
    this.getShopInfo()
  },

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

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },


  // 页面滚动事件

  onPageScroll: function(e) {
    var _this = this
    if (e.scrollTop > 80) {
      _this.setData({
        topFlag: true
      })
    } else {
      _this.setData({
        topFlag: false
      })
    }
  }
})