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

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

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            shopId: options.shopId,
            catalogId: options.catalogId,
            shopCart: app.globalData.shopCart
        })
        this.getShopGoodsList(this.data.catalogId)
    },
    //根据类目Id获取商店商品
    getShopGoodsList(catalogID) {
        let _self = this
        app.restAjax.get(
            app.restAjax.path(app.restAjax.apis.getGoodsByCatalogAndShop, [app.restAjax.baseUrl.tradeUrl, catalogID]), {
                page: _self.data.currentPage,
                shopId: _self.data.shopId
            }, {
                headers: {
                    token: app.globalData.token
                }
            }, (code, data) => {
                _self.setData({
                    isLoadMore: false
                })
                if (code == 200) {
                    if (data.rows.length > 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
                                    } else {
                                        it.buyNum = 0
                                    }
                                })
                            } else {
                                it.buyNum = 0
                            }
                        })
                        _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.getShopGoodsList(_self.data.catalogId)
        } 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,
        })
    },
    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
            })
        }
        _self.setData({
            goodsList: _self.data.goodsList
        })
        if (_self.selectComponent('#sCart')) {
            _self.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'
                    })
                }
            }
        })
        _self.setData({
            goodsList: _self.data.goodsList
        })
        _self.selectComponent('#sCart').listAddToCart(goods)
    },
    removeToCart(e) {
        let goods = e.currentTarget.dataset.item
        let _self = this
        _self.data.goodsList.forEach(it => {
            if (goods.goodsId == it.goodsId) {
                it.buyNum -= 1
            }
        })
        _self.setData({
            goodsList: _self.data.goodsList
        })
        this.selectComponent('#sCart').listRemoveCart(goods)
    },
    //显示购物车弹框
    doShowCart() {
        if (this.data.shopCart.length > 0) {
            this.setData({
                isShowCart: true
            })
        } 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
        })
    }
})