brand-coupon.js 3.21 KB
'use strict';

/**
 *  商品详情: 品牌券
 */
var tip = require('plugin/tip');
var $ = require('yoho-jquery');
var $body = $(document.body);

var brandCoupon = {
    skn: null,
    brandId: null,
    $entry: null,
    $couponDrawer: null,

    template: require('product/detail/coupon-list.hbs'),

    init: function(skn, brandId) {
        var self = this;

        this.skn = skn;
        this.brandId = brandId;

        if (!(skn && brandId)) {
            return;
        }

        setTimeout(() => {
            this.fetchCoupons(this.skn, this.brandId)
                .done(function(data) {
                    if (data.length) {
                        self.render(data);
                        self.domInit();
                        self.bindEvents();

                        self.$entry.removeClass('hide');
                    }

                    window.rePosFooter && window.rePosFooter();
                })
                .fail();
        }, 200);
    },

    domInit: function() {
        this.$entry = $('.brand-coupon').removeClass('hide');
    },

    bindEvents: function() {
        var self = this;

        this.$entry.on('click', function() {
            self.toggleDrawer(true);
        });

        this.$couponDrawer
            .on('click', '.coupon-drawer-mask', $.proxy(this.toggleDrawer, this, false))
            .on('click', '.coupon-btn-valid', $.proxy(this.saveCouponHandler, this));
    },

    render: function(data) {
        this.$couponDrawer = $(this.template({
            coupons: data
        }));
        this.$couponDrawer.appendTo('.good-detail-page');

        return this;
    },

    // 获取 品牌券
    fetchCoupons: function(skn, brandId) {
        return $.get('/product/detail/coupon.json', {
            skn: skn,
            brandId: brandId
        });
    },

    saveCoupon: function(couponId, callback) {
        $.post('/product/detail/coupon/save.json', {
            couponId: couponId
        }).done(function(res) {
            tip.show(res.message);

            if (res.code === 200) {
                callback(); // eslint-disable-line
            } else {
                tip.show(
                    res.message || '抱歉,您不符合领用条件'
                );

                if (res.redirect) {
                    setTimeout(function() {
                        location.href = res.redirect;
                    }, 1000);
                }
            }
        }).fail(function() {
            tip.show('网络异常,请稍后再试');
        });
    },

    // 收藏 品牌券
    saveCouponHandler: function(event) {
        var $btn = $(event.target);
        var couponId = $btn.closest('.coupon').data('coupon');

        this.saveCoupon(couponId, function() {
            $btn.prop('disabled', true)
                .removeClass('coupon-btn-valid')
                .text('已领取');
        });

        event.stopPropagation();
    },

    toggleDrawer: function(bool) {
        this.$couponDrawer.toggleClass('open', bool);
        $body.toggleClass('coupon-drawer-open', bool);
    }
};

$(function() {
    if ($('#product-coupon-switch').val() === 'true') {
        brandCoupon.init(
            $('#productSkn').val(),
            $('#brand-id').val()
        );
    }
});