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

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

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

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

    init: function(skn, brandId) {
        let 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() {
        let 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 if (res.code === 9999992) {
                tip.show(res.message);
            } else {
                tip.show(
                    res.message || '抱歉,您不符合领用条件'
                );

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

    // 收藏 品牌券
    saveCouponHandler: function(event) {
        let $btn = $(event.target);
        let 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()
        );
    }
});