// pages/shop/product/goodsdetail.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shopId: '', //商店ID
        goodsId: '', //商品ID
        picList: [], //商品
        goodsDetail: null, //商品详情Bean
        goodsPrices: 10000,
        _options: null,
        isShowCart: false,
        shopCart: [],
        baseImg: app.baseUrls.tradeUrl + app.baseUrls.baseImgUrl
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            _options: options,
            shopId: options.shopId,
            goodsId: options.goodsId,
            shopCart: app.globalData.shopCart
        })
        console.log(this.data.baseImg)
        this.getGoodsDetail()
    },
    //获取商品详情
    getGoodsDetail() {
        wx.showLoading({
            title: '加载中...',
        })
        let _self = this
        app.restAjax.get(app.restAjax.path(app.restAjax.apis.getGoodsDetail, [app.restAjax.baseUrl.tradeUrl, _self.data.goodsId]), {}, {
            headers: {
                token: app.globalData.token
            }
        }, (code, data) => {
            wx.hideLoading({})
            if (code == 200) {
                _self.buildPicList(data)
                _self.setData({
                    goodsDetail: data
                })
            }
        }, (code, err) => {
            wx.hideLoading({})
            if (err.msg) {
                app.dialog.msg(err.msg)
            } else {
                wx.showToast({
                    title: '网络错误',
                    icon: 'error'
                })
            }
        })
    },
    //构建图片数据
    buildPicList(data) {
        let _self = this
        let picStrs = data.goodsPhotos.split(',')
        picStrs.forEach(it => {
            let url = _self.data.baseImg + it
            _self.data.picList = _self.data.picList.concat(url)
        })
        _self.setData({
            picList: _self.data.picList
        })
    },

    onShow() {
        this.setData({
            shopCart: app.globalData.shopCart
        })
        this.selectComponent('#sCart').refreshCart()
    },
    //显示购物车弹框
    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
        })
    },
    doRefreshShopcart() {
        this.setData({
            shopCart: app.globalData.shopCart
        })
    }
})