bundle.js 4.87 KB
/*
 * @Author: Targaryen
 * @Date: 2017-04-06 16:51:52
 * @Last Modified by: Targaryen
 * @Last Modified time: 2017-04-14 18:32:07
 */

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

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

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

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

        self.handle = handle;

        /**
         * 减少套餐数量
         */
        $bundle.on('click', '.bundle-nums .num-opt .btn-opt-minus', function(e) {
            let skuIdsArray = [];

            $(e.delegateTarget).find('.good-item').each(function() {
                skuIdsArray.push($(this).data('id'));
            });

            self.decrBundle({
                activity_id: $(e.delegateTarget).data('activityid'),
                batch_no: $(e.delegateTarget).data('poolbatchno'),
                sku_ids: skuIdsArray.join(',')
            }, e);
        });

        /**
         * 增加套餐数量
         */
        $bundle.on('click', '.bundle-nums .num-opt .btn-opt-plus', function(e) {
            let skuIdsArray = [];

            $(e.delegateTarget).find('.good-item').each(function() {
                skuIdsArray.push($(this).data('id'));
            });

            if ($(e.delegateTarget).data('buynum') >= $(e.delegateTarget).data('maxnum')) {
                tip.show('库存不足');
                return false;
            }

            self.incrBundle({
                activity_id: $(e.delegateTarget).data('activityid'),
                batch_no: $(e.delegateTarget).data('poolbatchno'),
                sku_ids: skuIdsArray.join(',')
            }, 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 self = this;
        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'),
                batch_no: $(e.delegateTarget).data('poolbatchno'),
                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');
                    self.handle.refreshPage(result.data);
                }
                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;