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

var $ = require('yoho-jquery'),
    Alert = require('../common/dialog').Alert,
    $payWapper = $('.pay-wapper'),
    giftsWinTpl = require('hbs/cart/cart-gifts-win-tpl.hbs'),
    productInfoTpl = require('hbs/cart/cart-product-info-tpl.hbs'),
    $goodsSelWin = $('#Y_goodsSelectWin'),
    capi = require('./cart-api'),
    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) {
    $goodsSelWin.find('.detail-body').empty().html(giftsWinTpl(plist));
    capi.showMDialog('#Y_goodsSelectWinDialog');
}

/** 赠品加价购弹窗 **/
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 $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;
                if (selectedSkn) {
                    promotionInfo.selectedSkn = selectedSkn;
                }

                $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(res => {

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

            $goodsSelWin.find('.product-detail-info').empty().append(productInfoTpl(res));
        });
    },
    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('[data-role=sizes] .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).find('span').each(function() {
            if ($(this).hasClass('null-atcivec')) {
                $goodsSelWin.find('.addcart').addClass('none');
                $goodsSelWin.find('.btn_sellout').removeClass('none');
            } else {
                $goodsSelWin.find('.addcart').removeClass('none');
                $goodsSelWin.find('.btn_sellout').addClass('none');
                return false;
            }
        });
    },
    selSize: function() {

        var $this = $(this);

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

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

        if (shopNumAll > 0) {
            $goodsSelWin.find('.addcart').removeClass('none');
            $goodsSelWin.find('.btn_sellout').addClass('none');
        } else {
            $goodsSelWin.find('.addcart').addClass('none');
            $goodsSelWin.find('.btn_sellout').removeClass('none');
        }
    },
    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);
        }

    },
    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('swap');

        if ($curSize.length <= 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);
            } else {
                capi.addcart({
                    productSku: sku,
                    buyNumber: $num.val(),
                    promotionId: promotionId
                });
            }
        }
    }
};

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

$payWapper./* 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', '.addcart', GoodsWinAction.add2Cart);


module.exports = GoodsWinAction;