// subpages/goodsdetail/goodsdetail.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsId: "",
        isHidePageloading: false,
        detailBean: {},
        currentStandard: {},
        standardList: [],
        curStandardIndex: 0,
        imgUrl: app.shopImgUrl,
        carList: [],
        carNum: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            goodsId: options.id
        })
        this.getGoodsDetail();

    },
    onShow() {
        try {
            var value = wx.getStorageSync('carlist');
            if (value) {
                this.setData({
                    carList: value
                })
                this.parseCarList();
            }
        } catch (error) {

        }
    },
    parseCarList() {
        var _self = this;
        var count = 0;
        for (let i = 0; i < _self.data.carList.length; i++) {
            const item = _self.data.carList[i];
            count = item.carAmount + count;
        }
        console.log(count)
        if (count > 0) {
            _self.setData({
                carNum: count
            })
        }
    },
    //获取商品详情
    getGoodsDetail() {
        var _self = this
        app.restAjax.get(app.restAjax.path('{shopUrl}/app/shopgoods/getrelease/{goodsId}',
                [app.shopUrl, _self.data.goodsId]),
            null,
            null,
            function (code, data) {
                if (data) {
                    if (data.goodsPhotos.length > 0) {
                        var photos = data.goodsPhotos.split(',');
                        data.photoList = photos;
                    } else {
                        data.photoList = []
                    }
                    data.goodsPrice = 0;
                    data.goodsStandarName = "";
                    data.goodsStandarId = "";
                    data.isCheck = false;
                    data.carAmount = 0;
                    _self.setData({
                        detailBean: data,
                    })
                }
            },
            function (code, data) {
                app.dialog.msg(data.msg);
            },
            function () {
                _self.getGoodsStandardList();
            })
    },
    //  carAmount;
    //  goodsPrice;
    // goodsStandarId;
    // goodsStandarName;
    // isCheck;
    //获取商品附加信息
    getGoodsStandardList() {
        var _self = this
        _self.data.detailBean.goodsPrice = 0;
        _self.data.detailBean.goodsStandarName = "";
        _self.data.detailBean.goodsStandarId = "";
        _self.data.detailBean.isCheck = false;
        _self.data.detailBean.carAmount = 0;

        app.restAjax.get(app.restAjax.path('{shopUrl}/app/shopgoodsitem/listrelease/{goodsId}',
                [app.shopUrl, _self.data.goodsId]),
            null,
            null,
            function (code, data) {
                if (data && data.length > 0) {
                    _self.data.detailBean.goodsPrice = data[0].goodsItemUnitPrice;
                    _self.data.detailBean.goodsStandarName = data[0].goodsItemName;
                    _self.data.detailBean.goodsStandarId = data[0].goodsItemId;
                    _self.setData({
                        standardList: data,
                        currentStandard: data[0],
                        isHidePageloading: true,
                        detailBean: _self.data.detailBean
                    })
                } else {
                    _self.setData({
                        isHidePageloading: true
                    })
                }
            },
            function (code, data) {
                app.dialog.msg(data.msg);
                _self.setData({
                    isHidePageloading: true
                })
            },
            function () {
                _self.setData({
                    isHidePageloading: true
                })
            })
    },
    doSelStandard(e) {
        var item = e.currentTarget.dataset.item;
        var index = e.currentTarget.dataset.index;
        this.data.detailBean.goodsPrice = item.goodsItemUnitPrice;
        this.data.detailBean.goodsStandarName = item.goodsItemName;
        this.data.detailBean.goodsStandarId = item.goodsItemId;
        this.setData({
            currentStandard: item,
            curStandardIndex: index,
            detailBean: this.data.detailBean
        })
    },
    // 添加购物车
    addToCar() {
        //判断购物车中是否存在该商品
        var _self = this;
        var pos = -1;
        for (let i = 0; i < _self.data.carList.length; i++) {
            var item = _self.data.carList[i];
            if (_self.data.detailBean.goodsId == item.goodsId && _self.data.detailBean.goodsStandarId == item.goodsStandarId) {
                pos = i;
                break;
            }
        }
        if (pos != -1) {
            _self.data.carList[pos].carAmount = _self.data.carList[pos].carAmount + 1;
        } else {
            _self.data.detailBean.carAmount = 1;
            _self.data.carList.push(_self.data.detailBean);
        }
        _self.setData({
            carList: _self.data.carList
        })
        _self.parseCarList();
        wx.setStorageSync('carlist', _self.data.carList)
    },
    openCar() {
        if (this.data.carNum > 0) {
            wx.navigateTo({
                url: '/subpages/goodscar/goodscar',
            })
        } else {
            app.dialog.msg('购物车无数据')
        }
    }
})