bundle.js 2.9 KB
/*
 * @Author: Targaryen
 * @Date: 2017-04-06 16:51:52
 * @Last Modified by: Targaryen
 * @Last Modified time: 2017-04-07 13:34:40
 */

const $ = require('yoho-jquery');

let tip = require('plugin/tip');

let bundle = {
    init(handle) {
        let self = this;

        let $bundle = $('.bundle'); // 限定作用域
        let activityId = $bundle.data('activityid');

        self.handle = handle;

        /**
         * 减少套餐数量
         */
        $bundle.on('click', '.bundle-nums .num-opt .btn-opt-minus', function(e) {
            self.decrBundle({
                activity_id: activityId
            }, e);
        });

        /**
         * 增加套餐数量
         */
        $bundle.on('click', '.bundle-nums .num-opt .btn-opt-plus', function(e) {
            self.incrBundle({
                activity_id: activityId
            }, e);
        });

        $bundle.on('click', '.bundle-title .opt .select', function(e) {
            self.bundleCheckHandle(e);
        });
    },

    /**
     * 增加套餐数量
     * @param {*} params
     */
    incrBundle(params, e) {
        let self = this;

        $.post('/cart/index/new/incrbundle', params, function(result) {
            let bundleNum = $(e.delegateTarget).find('.good-num');
            let defaultValue = parseInt(bundleNum.val(), 10);

            if (result.code === 200) {
                bundleNum.val(defaultValue + 1);
                self.handle.refreshPage();
            } else {
                tip.show(result.message);
            }
        });
    },

     /**
     * 减少套餐数量
     * @param {*} params
     */
    decrBundle(params, e) {
        let self = this;

        $.post('/cart/index/new/decrbundle', params, function(result) {
            let bundleNum = $(e.delegateTarget).find('.good-num');
            let defaultValue = parseInt(bundleNum.val(), 10);

            if (result.code === 200) {
                bundleNum.val(defaultValue - 1);
                self.handle.refreshPage();
            } else {
                tip.show(result.message);
            }
        });
    },

    /**
     * 套餐选中
     * @param {*} e
     */
    bundleCheckHandle(e) {
        $(e.delegateTarget).find('.select').toggleClass('checked');
        let goodItem = $(e.delegateTarget).find('.good-item');
        let skuData = [];

        goodItem.each(function(index, element) {
            skuData.push({
                product_sku: $(element).data('id'),
                buy_number: $(element).data('mnum'),
                activity_id: $(e.delegateTarget).data('activityid'),
                selected: 'Y'
            });
        });

        $.ajax({
            type: 'post',
            url: '/cart/index/new/select',
            data: {
                skuList: JSON.stringify(skuData)
            },
            success: function(result) {
                console.log(result);
            }
        });
    }
};

module.exports = bundle;