cart-goods-win.js 13.1 KB
/**
 * Created by yoho on 2017-01-05.
 */

var $ = require('yoho-jquery'),
    Alert = require('../common/dialog').Alert,
    capi = require('./cart-api'),
    rDialog = require('./rdialog'),
    RAlert = rDialog.RAlert,
    giftsWinTpl = require('hbs/cart/cart-gifts-win-tpl.hbs'),
    productInfoTpl = require('hbs/cart/cart-product-info-tpl.hbs');

var $cartListWrap = $('#Y_CartListWrap'),
    $goodsSelWin = $('#Y_goodsSelectWin'),
    GoodsWinAction,

    // 显示赠品
    giftBtn = ['[data-role=gift-view-btn]',
        '[data-role=gift-resel-btn]',
        '[data-role=gift-sel-btn]',
        '[data-role=pg-sel-btn]',
        '[data-role=pg-resel-btn]'];

function renderAndShowGiftWin(plist) {

    if(!plist.goodsList || !plist.goodsList.length) {
        new RAlert('<div class="alert-main">已抢光</div>');
        return;
    }
    $goodsSelWin.find('.detail-body').empty().html(giftsWinTpl(plist));
    capi.showMDialog('#Y_goodsSelectWinDialog');

    $goodsSelWin.find('.side-img-dd').slider2({
        shownum: 7,
        isCircle: true
    });
}

/** 赠品加价购弹窗 **/
GoodsWinAction = {
    showGiftWin: function() {

        var $this = $(this);
        var $wrap = $this.closest('[data-role="promotion-wrap"]');
        var promotionid = $wrap.data('promotionid');
        var $promotionPool = $this.closest('.promotion-pool');
        var promotionInfo = $wrap.data('_promotionInfo');
        var role = $this.data('role');
        var isSwap = role === 'pg-resel-btn' || role === 'gift-resel-btn';
        var isView = role === 'gift-view-btn';
        var $selectedItem;
        var selectedSkn;

        if (role === 'pg-resel-btn') {
            $selectedItem = $promotionPool.find('[data-role=pitem]')
                .filter('[data-ispricegift]')
                .filter('[data-promotionid=' + promotionid + ']');
        } else if (role === 'gift-resel-btn') {
            $selectedItem = $promotionPool.find('[data-role=pitem]')
                .filter('[data-isgift]')
                .filter('[data-promotionid=' + promotionid + ']');
        }

        if ($selectedItem && $selectedItem.length > 0) {
            $selectedItem = $selectedItem.eq(0);
            selectedSkn = $selectedItem.data('skn');
        }

        $promotionPool.find('[data-role=pitem][data-ispricegift], [data-role=pitem][data-isgift]');
        if (!promotionInfo) {
            capi.getProductByPromotionId(promotionid).done(function(pinfo) {


                if (!pinfo && pinfo.code !== 200) {
                    return new Alert('获取商品失败,请稍后再试!').show();
                }

                promotionInfo = pinfo.data;
                promotionInfo.isSwap = isSwap;
                promotionInfo.isView = isView;
                if (selectedSkn) {
                    promotionInfo.selectedSkn = selectedSkn;
                }

                if (promotionInfo.productInfo) {
                    promotionInfo.productInfo.isView = isView;
                }

                $wrap.data('_promotionInfo', promotionInfo);
                renderAndShowGiftWin(promotionInfo);
            }).fail(function() {
                new Alert('获取商品失败,请稍后再试!').show();
            });
            return;
        }

        renderAndShowGiftWin(promotionInfo);
    },

    /* closeWin: function() {
        $ModalOverflow.hide();
    },*/
    changeGoods: function() {

        var $this = $(this);
        var id = $this.data('id');
        var skn = $this.data('skn');

        $this.siblings('li').removeClass('active');
        $this.addClass('active');

        capi.getProductInfo(id, skn).then(function(res) {

            // 赠品价格为 0, 加价购价格为 活动价
            if ($this.data('isgift')) {
                res.salePrice = '¥0.00';
            } else if ($this.data('ispricegift')) {
                if ($this.data('subjoinprice')) {
                    res.salePrice = '¥' + $this.data('subjoinprice');
                }
            }

            res.isView = $this.closest('.detail-body').find('.product-detail-info').data('isview');

            $goodsSelWin.find('.product-detail-info').empty().append(productInfoTpl(res));
        });

        $('[data-role="fav-count"]').attr('data-id', id);

    },
    selThumb: function() {
        var $this = $(this);
        var idx = $(this).index();

        $goodsSelWin.find('.detail-bigpic:not(.none) .piclist li').removeClass('active');
        $this.addClass('active');
        $goodsSelWin.find('.detail-bigpic:not(.none) .bigpic').hide().eq(idx).show();
    },
    selThumbPrevNext: function() {
        var $this = $(this);
        var $detailBigpic = $this.closest('.detail-bigpic');
        var curIndex = Number($detailBigpic.data('_index') || 0);
        var $lis = $this.siblings('.con').find('li');

        if ($this.hasClass('next')) {
            if (curIndex >= $lis.length - 1) {
                return false;
            }
            curIndex++;
        } else {
            if (curIndex < 1) {
                return false;
            }
            curIndex--;
        }

        $detailBigpic.data('_index', curIndex);

        $lis.removeClass('active').eq(curIndex).addClass('active');
        $detailBigpic.find('.bigpic').hide().eq(curIndex).show();
    },
    selColor: function() {

        var $this = $(this);
        var idx = $this.index();
        var $detail = $this.closest('.detail-goods');
        var $sizes = $detail.find('.size-row');
        var $detailBigpic = $detail.find('.detail-bigpic');
        var $curSize = $sizes.eq(idx);
        var $curDetailBig = $detailBigpic.eq(idx);
        var bigPicIndex = 0;    // 默认显示大图中的第一个图

        $curDetailBig.data('_index', bigPicIndex);

        $this.siblings('.color').find('p').removeClass('active');
        $this.find('p').addClass('active');

        $sizes.addClass('none');
        $curSize.removeClass('none');

        if ($curSize.find('span').length < 2) {
            $curSize.find('span:first').addClass('active');
        }

        $detailBigpic.addClass('none');
        $curDetailBig.removeClass('none');

        $curDetailBig.find('.bigpic').hide();
        $curDetailBig.find('.bigpic').eq(bigPicIndex).show();

        $curDetailBig.find('.con li').removeClass('active');
        $curDetailBig.find('.con li').eq(bigPicIndex).addClass('active');

        $sizes.eq(idx).each(function() {
            if ($(this).find('.active').data('num') === 0) {
                $goodsSelWin.find('.addcart').addClass('none');
                $goodsSelWin.find('.btn_sellOut').removeClass('none');
                $goodsSelWin.find('.colorBox .active').addClass('disabled');

            } else {
                $goodsSelWin.find('.addcart').removeClass('none');
                $goodsSelWin.find('.btn_sellOut').addClass('none');
                $goodsSelWin.find('.colorBox .active').removeClass('disabled');

                return false;
            }
        });
    },
    selSize: function() {

        var $this = $(this);

        // var idx = $this.index();
        var shopNumAll = $this.data('num');

        $this.closest('.detail-info').find('.submit').css('marginTop', '12px');
        $this.siblings('span').removeClass('active');
        $this.addClass('active');
        $this.closest('[data-role=sizes]').find('.size-p-tip').hide();

        if (shopNumAll > 0) {
            $goodsSelWin.find('.addcart').removeClass('none');
            $goodsSelWin.find('.btn-sellOut').addClass('none');
            $goodsSelWin.find('.colorBox .active').removeClass('disabled');
        } else {
            $goodsSelWin.find('.addcart').addClass('none');
            $goodsSelWin.find('.btn-sellOut').removeClass('none');
            $goodsSelWin.find('.colorBox .active').addClass('disabled');
        }
    },
    changeNum: function() {

        var $this = $(this);
        var $pinfo = $this.closest('.product-detail-info');

        // var count = $this.hasClass('minus') ? 'decreaseNum' : 'increaseNum';
        var promotionId = $pinfo.data('promotionid');
        var $num = $goodsSelWin.find('#num');
        var shopNum = Number($num.val() || 1);

        if (promotionId) {
            if ($this.hasClass('cut')) {
                new Alert('-_-,已经是最后一件,不能再减了!').show();
            } else {
                new Alert('最多只能购买一件,您好像购买的太多了!').show();
            }
        } else {
            if ($this.hasClass('add')) {
                shopNum++;
            } else {
                shopNum--;
            }
            if (shopNum < 1) {
                new Alert('-_-,已经是最后一件,不能在减了!').show();
                shopNum = 1;
                return false;
            }

            $num.val(shopNum);
        }

    },

    // 收藏
    btnFav: function() {

        var favBtn = $('[data-role="fav-count"]');
        var $this = $(this);
        var $goodsDetail = $this.closest('.detail-goods');
        var pid = $goodsDetail.data('productid');
        var colTxt = {
            normal: '收藏商品',
            coled: '已收藏',
            hover: '取消收藏'
        };

        function tipFav(parent) {

            var $tip = $('<span class="tip-fav">收藏失败</span>').appendTo(parent);

            $tip.fadeIn();

            setTimeout(function() {
                $tip.fadeOut();
            }, 2000);
        }

        if (favBtn.find('.favCount').hasClass('coled')) {

            capi.productCollect(pid, true).done(function(obj) {

                if (!obj && obj.code !== 200) {
                    tipFav(pid);
                    return false;
                }
                favBtn.find('.favCount').removeClass('coled');
                favBtn.find('em').text(colTxt.normal);
                favBtn.mouseenter(function() {
                    favBtn.find('em').text(colTxt.normal);
                }).mouseleave(function() {
                    favBtn.find('em').text(colTxt.normal);
                });

            }).fail(function() {

                tipFav(pid);
            });

            return false;

        } else {
            capi.productCollect(pid).done(function(obj) {

                if (!obj && obj.code !== 200) {
                    tipFav(pid);
                    return false;
                }

                favBtn.find('.favCount').addClass('coled');
                favBtn.find('em').text(colTxt.coled);
                favBtn.mouseenter(function() {
                    favBtn.find('em').text(colTxt.hover);
                }).mouseleave(function() {
                    favBtn.find('em').text(colTxt.coled);
                });
                obj.stopPropagation();

            }).fail(function() {

                tipFav(pid);
            });

            return false;
        }

    },
    add2Cart: function() {

        var $this = $(this);
        var $curSize = $goodsSelWin.find('[data-role=sizes] .size-row:not(.none) .active');
        var $num = $goodsSelWin.find('#num');
        var allNum = $curSize.data('num');
        var sku = $curSize.data('sku');
        var skn = $this.closest('.detail-goods').data('skn');
        var promotionId = $this.closest('.product-detail-info').data('promotionid') || 0;
        var isSwap = $this.closest('.product-detail-info').data('isswap');

        if ($curSize.length <= 0) {
            $this.closest('.detail-info').find('.size-p-tip').show();
            $this.closest('.detail-info').find('.submit').css('marginTop', '0');

            // new Alert('请选择尺码').show();
            return false;
        }

        if ($num.val() > allNum) {
            new Alert('库存不足,目前还有' + allNum + '个库存').show();
        } else {
            /* if (Number($('#addToCart').val()) === 1) {
             addcart(dataJSON);
             } else {
             new Alert('该商品无法加入购物车').show();
             }*/

            // 替换促销商品
            if (isSwap) {
                capi.updateCartGiftItem(promotionId, skn, sku).then(function() {
                    capi.hideMDialog('#Y_goodsSelectWinDialog');
                });
            } else {
                capi.addcart({
                    productSku: sku,
                    buyNumber: $num.val(),
                    promotionId: promotionId
                }).then(function() {
                    capi.hideMDialog('#Y_goodsSelectWinDialog');
                });
            }
        }
    }
};

/** 弹窗事件绑定 **/

$cartListWrap./* find('li[data-role="pitem"]').*/on('click', giftBtn.join(','), GoodsWinAction.showGiftWin);

// $goodsSelWin.on('click', '.close', GoodsWinAction.closeWin);
$goodsSelWin.on('click', '.slide-img .img-list .img-item', GoodsWinAction.changeGoods);
$goodsSelWin.on('click', '.piclist li', GoodsWinAction.selThumb);
$goodsSelWin.on('click', '.pre, .next', GoodsWinAction.selThumbPrevNext);
$goodsSelWin.on('click', '[data-role=colors] .color', GoodsWinAction.selColor);
$goodsSelWin.on('click', '[data-role=sizes] .size-row span', GoodsWinAction.selSize);
$goodsSelWin.on('click', '.cut, .add', GoodsWinAction.changeNum);
$goodsSelWin.on('click', '.btn-favCount', GoodsWinAction.btnFav);
$goodsSelWin.on('click', '.addcart', GoodsWinAction.add2Cart);


module.exports = GoodsWinAction;