// pages/shop/catalog/bannerlist/bannerlist.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: '',
    active: 1,
    swiperlist: [],
    typeMenu: [],
    goodsList: [],
    currentPage: 1,
    contentHeight: app.globalData.windowHeight,
    isLoadMore: false, //是否在加载中
    hasMore: true, //是否还有更多数据
    currentPage: 1,
    totalSize: 0,
    baseImg: app.urls.baseImgUrl,
    isShowCart: false,
    shopCart: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: options.shopId,
      shopCart: app.globalData.shopCart
    })
    //获取热门商品
    this.getShopGoodsList(this.data.shopId)
    //获取店铺类别
    this.getShopGoodsCatalog(this.data.shopId)
    //获取全部商品
    this.getShopGoodsListByPage(this.data.shopId)
    this.selectComponent('#sCart').refreshCart()
  },
  //获取店铺所有商品类目
  getShopGoodsList(shopId) {
    let _self = this
    wx.showLoading({
      title: '加载中...',
    })
    app.restAjax.get(
      app.restAjax.path(app.restAjax.apis.getShopGoodsList, [app.restAjax.baseUrl.tradeUrl, shopId]), {}, {
        headers: {
          token: app.globalData.token
        }
      }, (code, data) => {
        wx.hideLoading({})
        _self.setData({
          isRefreshing: false
        })
        if (code == 200) {
          if (data.rows.length > 0) {
            _self.buildData(data.rows)
          } else {
            //TODO 没有数据
          }
        }
      }, (code, error) => {
        wx.hideLoading({})
        if (error.msg) {
          app.dialog.msg(error.msg)
        } 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
    })
  },
  //显示热门商品详情
  showHot(e) {
    let _self = this
    wx.navigateTo({
      url: '../../product/goodsdetail?shopId=' + _self.data.shopId + '&goodsId=' + e.currentTarget.dataset.item.goodsId,
    })
  },
  //获取商店所有分类
  getShopGoodsCatalog(shopId) {
    let _self = this
    app.restAjax.get(app.restAjax.path(app.restAjax.apis.getShopCatalogList, [app.restAjax.baseUrl.tradeUrl, shopId]), {}, {
      headers: {
        token: app.globalData.token
      }
    }, (code, data) => {
      if (code == 200) {
        _self.setData({
          typeMenu: data
        })
      }
    }, (code, error) => {
      if (error.msg) {
        app.dialog.msg(error.msg)
      } else {
        wx.showToast({
          title: '网络错误',
          icon: 'error'
        })
      }
    })
  },
  //分页获取商品
  getShopGoodsListByPage(id) {
    let _self = this
    app.restAjax.get(
      app.restAjax.path(app.restAjax.apis.getShopGoodsList, [app.restAjax.baseUrl.tradeUrl, id]), {
        page: _self.data.currentPage
      }, {
        headers: {
          token: app.globalData.token
        }
      }, (code, data) => {
        _self.setData({
          isLoadMore: false
        })
        if (code == 200) {
          if (data.rows.length > 0) {
            data.rows.forEach(it => {
              it.buyNum = 0
            })
            data.rows.forEach(it => {
              //判断购物车是否存在该商品
              if (app.globalData.shopCart.length > 0) {
                app.globalData.shopCart.forEach(i => {
                  if (i.goodsId == it.goodsId) {
                    it.buyNum = i.buyNum
                  }
                })
              }
            })
            _self.data.goodsList = _self.data.goodsList.concat(data.rows)
            _self.setData({
              goodsList: _self.data.goodsList
            })
          } else {
            _self.setData({
              hasMore: false
            })
          }
        }
      }, (code, error) => {
        wx.hideLoading({})
        if (error.msg) {
          app.dialog.msg(error.msg)
        } else {
          wx.showToast({
            title: '网络错误',
            icon: 'error'
          })
        }
      })
  },
  doLoadMore(e) {
    let _self = this
    if (_self.data.hasMore) {
      if (_self.data.isLoadMore) {
        return
      }
      _self.setData({
        isLoadMore: true,
        currentPage: ++_self.data.currentPage
      })
      _self.getShopGoodsListByPage(_self.data.shopId)
    } else {
      wx.showToast({
        title: '暂无更多数据',
        icon: 'none',
        duration: 500
      })
    }
  },
  //商品详情页面
  toGoodsDetail(e) {
    let _self = this
    wx.navigateTo({
      url: '../../product/goodsdetail?shopId=' + _self.data.shopId + '&goodsId=' + e.currentTarget.dataset.item.goodsId,
    })
  },
  //商品分类页面
  toGoodsCatalogPage(e) {
    wx.navigateTo({
      url: './goodscataloglist?shopId=' + this.data.shopId + '&catalogId=' + e.currentTarget.dataset.item.categoryId,
    })
  },
  onShow() {
    let _self = this
    if (app.globalData.shopCart.length > 0) {
      _self.data.goodsList.forEach(it => {
        var isExist = false
        app.globalData.shopCart.forEach(i => {
          if (i.goodsId == it.goodsId) {
            isExist = true
            it.buyNum = i.buyNum
          }
        })
        if (!isExist) {
          it.buyNum = 0
        }
      })
    } else {
      _self.data.goodsList.forEach(it => {
        it.buyNum = 0
      })
    }
    this.setData({
      goodsList: _self.data.goodsList,
      shopCart: app.globalData.shopCart
    })
    this.selectComponent('#sCart').refreshCart()
  },
  //列表添加购物车
  addToCart(e) {
    let _self = this
    let goods = e.currentTarget.dataset.item
    _self.data.goodsList.forEach(it => {
      if (goods.goodsId == it.goodsId) {
        it.buyNum += 1
        if (it.buyNum > goods.goodsTotal) {
          it.buyNum = goods.goodsTotal
          wx.showToast({
            title: '超出库存',
            icon: 'error'
          })
        }
      }
    })
    this.selectComponent('#sCart').listAddToCart(goods)
    _self.setData({
      goodsList: _self.data.goodsList,
      shopCart: app.globalData.shopCart
    })
  },
  removeToCart(e) {
    let goods = e.currentTarget.dataset.item
    let _self = this
    _self.data.goodsList.forEach(it => {
      if (goods.goodsId == it.goodsId) {
        it.buyNum -= 1
      }
    })
    this.selectComponent('#sCart').listRemoveCart(goods)
    _self.setData({
      goodsList: _self.data.goodsList,
      shopCart: app.globalData.shopCart
    })
  },
  //显示购物车弹框
  doShowCart() {
    var _self = this
    if (this.data.shopCart.length > 0) {
      //判断当前购物车是否存在该商店的商品
      var isExist = false
      this.data.shopCart.forEach(it => {
        if (it.shopId == _self.data.shopId) {
          isExist = true
        }
      })
      if (isExist) {
        if (this.data.isShowCart) {
          this.setData({
            isShowCart: false
          })
        } else {
          this.setData({
            isShowCart: true
          })
        }
      } else {
        wx.showToast({
          title: '购物车空空如也',
          icon: 'error'
        })
      }
    } else {
      wx.showToast({
        title: '购物车空空如也',
        icon: 'error'
      })
    }
  },
  //隐藏购物车弹框
  onHideCart() {
    this.setData({
      isShowCart: false
    })
  },
  //删除购物车中的商品
  delGoods(e) {
    let _self = this
    var index = e.currentTarget.dataset.index
    var goods = e.currentTarget.dataset.goods
    app.globalData.shopCart.splice(index, 1)
    //删除购物车中数据
    _self.selectComponent('#sCart').refreshCart()
    //将列表中的数据购买数量重置
    _self.data.goodsList.forEach(it => {
      if (it.goodsId == goods.goodsId) {
        it.buyNum = 0
      }
    })
    _self.setData({
      goodsList: _self.data.goodsList,
      shopCart: app.globalData.shopCart
    })
    if (_self.data.shopCart.length <= 0) {
      _self.setData({
        isShowCart: false
      })
    }
  },
  //购物车弹框删除或添加商品
  onChange(e) {
    let buyNum = e.detail
    let goods = e.currentTarget.dataset.goods
    let _self = this
    //遍历列表
    _self.data.goodsList.forEach(it => {
      if (it.goodsId == goods.goodsId) {
        it.buyNum = buyNum
      }
    })
    //遍历购物车
    _self.data.shopCart.forEach(it => {
      if (it.goodsId == goods.goodsId) {
        it.buyNum = buyNum
      }
    })
    //刷新视图
    _self.setData({
      goodsList: _self.data.goodsList,
      shopCart: _self.data.shopCart
    })
    //刷新购物车
    _self.selectComponent('#sCart').refreshCart()
  },
  onHide() {
    this.setData({
      isShowCart: false
    })
  }
})