bundle.js 3.97 KB
/*
 * @Author: Targaryen
 * @Date: 2017-04-06 16:51:52
 * @Last Modified by: Targaryen
 * @Last Modified time: 2017-04-10 11:33:06
 */

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

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

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);
        });

        /**
         * 编辑状态下套餐选中/取消
         */
        $bundle.on('click', '.bundle-title .opt .edit', function(e) {
            self.bundleEditCheckHandle(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) {
        let selectCheck = $(e.delegateTarget).find('.select');
        let goodItem = $(e.delegateTarget).find('.good-item');
        let skuData = [];
        let selected = 'Y';

        if (selectCheck.hasClass('checked')) {
            selected = 'N';
        }

        goodItem.each(function(index, element) {
            skuData.push({
                product_sku: $(element).data('id'),
                buy_number: $(e.delegateTarget).data('buynum'),
                activity_id: $(e.delegateTarget).data('activityid'),
                selected: selected
            });
        });
        loading.showLoading();
        $.ajax({
            type: 'post',
            url: '/cart/index/new/select',
            data: {
                skuList: JSON.stringify(skuData)
            },
            success: function(result) {
                if (result.code === 200) {
                    selectCheck.toggleClass('checked');
                }
                loading.hideLoading();
            }
        });
    },

    /**
     * 编辑状态下套餐选中/取消
     * @param {*} e
     */
    bundleEditCheckHandle(e) {
        let editCheck = $(e.delegateTarget).find('.edit');
        let goodItem = $(e.delegateTarget).find('.good-item');

        if (editCheck.hasClass('checked')) {
            editCheck.removeClass('checked');
            goodItem.find('.opt .edit').removeClass('checked');
        } else {
            editCheck.addClass('checked');
            goodItem.find('.opt .edit').addClass('checked');
        }
    },
};

module.exports = bundle;