// pages/shop/bespeak/bespeak.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: '',
    shopBean: null,
    active: 1,
    testGoods: {
      name: "商品"
    },
    goodsList: [],
    shopCart: [],
    tabList: [],
    TabCur: 0,
    scrollLeft: 0,
    swiperlist: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: options.shopId,
    })
    this.getShopDetail()
    this.getShopGoodsCatalog()
    this.getShopGoodsList()
  },
  getShopDetail() {
    let _self = this
    wx.showLoading({
      title: '加载中...',
    })
    app.restAjax.get(app.restAjax.path(app.apis.getShopDetail, [app.baseUrls.tradeUrl, _self.data.shopId]), {}, {
      headers: {
        token: app.globalData.token
      }
    }, function (code, data) {
      wx.hideLoading({})
      if (code == 200) {
        console.log(data)
        _self.setData({
          shopBean: data
        })
      }
    }, function (code, data) {
      wx.hideLoading({})
      if (data.msg) {
        wx.showToast({
          title: data.msg,
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '网络错误',
          icon: 'error'
        })
      }
    });
  },
  //获取店铺所有商品类目
  getShopGoodsList() {
    let _self = this
    wx.showLoading({
      title: '加载中...',
    })
    app.restAjax.get(
      app.restAjax.path(app.restAjax.apis.getShopGoodsList, [app.restAjax.baseUrl.tradeUrl, _self.data.shopId]), {}, {
        headers: {
          token: app.globalData.token
        }
      }, (code, data) => {
        wx.hideLoading({})
        if (code == 200) {
          if (data.rows.length > 0) {
            _self.buildData(data.rows)
          } else {
            //TODO 没有数据
          }
        }
      }, (code, error) => {
        wx.hideLoading({})
        if (error.msg) {
          wx.showToast({
            title: error.msg,
          })
        } else {
          wx.showToast({
            title: '网络错误',
            icon: 'error'
          })
        }
      })
  },
  //获取商店所有分类
  getShopGoodsCatalog() {
    let _self = this
    app.restAjax.get(app.restAjax.path(app.restAjax.apis.getShopCatalogList, [app.restAjax.baseUrl.tradeUrl, _self.data.shopId]), {}, {
      headers: {
        token: app.globalData.token
      }
    }, (code, data) => {
      if (code == 200) {
        _self.setData({
          tabList: data
        })
        _self.getGoodsListByCategory(_self.data.tabList[0].categoryId)
      }
    }, (code, error) => {
      if (error.msg) {
        wx.showToast({
          title: error.msg,
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '网络错误',
          icon: 'error'
        })
      }
    })
  },
  getGoodsListByCategory(categoryId) {
    wx.showLoading({
      title: '加载中',
    })
    let _self = this
    app.restAjax.get(
      app.restAjax.path(app.restAjax.apis.getGoodsByCatalogAndShop, [app.restAjax.baseUrl.tradeUrl, categoryId]), {
        page: 1,
        shopId: _self.data.shopId
      }, {
        headers: {
          token: app.globalData.token
        }
      }, (code, data) => {
        wx.hideLoading({})
        if (code == 200) {
          if (data.rows.length > 0) {
            data.rows.forEach(it => {
              var picId = it.goodsPhotos.substring(0, it.goodsPhotos.indexOf(',') != -1 ? it.goodsPhotos.indexOf(',') : it.goodsPhotos.length)
              it.hotPic = app.restAjax.baseUrl.tradeUrl + app.restAjax.baseUrl.baseImgUrl + picId
            })
            _self.data.goodsList = _self.data.goodsList.concat(data.rows)
            _self.setData({
              goodsList: _self.data.goodsList
            })
          }
        }
      }, (code, error) => {
        wx.hideLoading({})
        if (error.msg) {
          wx.showToast({
            title: error.msg,
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '网络错误',
            icon: 'error'
          })
        }
      })
  },
  //构建热梦商品
  buildData(datas) {
    let _self = this
    datas.forEach(it => {
      var picId = it.goodsPhotos.substring(0, it.goodsPhotos.indexOf(',') != -1 ? it.goodsPhotos.indexOf(',') : it.goodsPhotos.length)
      it.hotPic = app.restAjax.baseUrl.tradeUrl + app.restAjax.baseUrl.baseImgUrl + picId
      _self.data.swiperlist.push(it)
    })
    _self.setData({
      swiperlist: _self.data.swiperlist
    })
  },
  tabSelect(e) {
    this.setData({
      goodsList: [],
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
    var cItem = this.data.tabList[this.data.TabCur]
    this.getGoodsListByCategory(cItem.categoryId)
  },
  callPhone() {},
  //商品分类页面
  showCategoryGoods() {
    var item = this.data.tabList[this.data.TabCur]
    wx.navigateTo({
      url: '../catalog/bannerlist/goodscataloglist?shopId=' + this.data.shopId + '&catalogId=' + item.categoryId,
    })
  },
  //商品详情页面
  toGoodsDetail(e) {
    let _self = this
    wx.navigateTo({
      url: '../product/goodsdetail?shopId=' + _self.data.shopId + '&goodsId=' + e.currentTarget.dataset.item.goodsId,
    })
  },
})