select-coupons.js 6.51 KB
import $ from 'yoho-jquery';
import qs from 'yoho-qs';
import Page from 'js/yoho-page';
import tip from 'js/plugin/tip';
import cookie from 'yoho-cookie';
import cookieOption from './cookie-option';

class SelectCouponController extends Page {
    constructor(params) {
        super();

        this.linkUrl = document.referrer;
        this.orderInfo = params.orderInfo;
        this.couponListUrl = params.couponListUrl;
        this.useCouponCodeUrl = params.useCouponCodeUrl;
        this.selectCouponCodes = [];
        this.isBuyNowPage = params.isBuyNowPage || false;
        this.closeCouponRuleTipbyUser = cookie.get('close_coupon_rule_tip_by_user') || 'N';

        if (this.orderInfo('coupon_code')) {
            this.selectCouponCodes = this.orderInfo('coupon_code').split(',');
        }

        this.page = $('.select-coupons-page');
        this.renderPage();
    }

    /**
     * 渲染页面
     */
    renderPage() {
        let couponCodeStr = this.selectCouponCodes.length > 0 ? this.selectCouponCodes.join(',') : '';
        let postData = {
            delivery_way: this.orderInfo('deliveryId'),
            coupon_code: couponCodeStr
        };

        if (this.isBuyNowPage) {
            postData.product_sku = qs.product_sku;
            postData.buy_number = qs.buy_number;
        }

        this.ajax({
            type: 'POST',
            url: this.couponListUrl,
            data: postData
        }).then(result => {
            this.orderInfo('coupon_code', couponCodeStr);

            let resultHtml = $(result);

            this.bindEvents(resultHtml);
            this.page.html(resultHtml);
        });
    }

    /**
     * 事件绑定处理
     */
    bindEvents(resultHtml) {
        this.view = {
            filterBtn: resultHtml.find('.filter-btn'),
            coupon: resultHtml.find('.coupon'),
            checkbox: resultHtml.find('.checkbox'),
            tipBox: resultHtml.find('.tip-box'),
            tipClose: resultHtml.find('.tip-close'),
            usableCouponList: resultHtml.find('.usable'),
            unusableCouponList: resultHtml.find('.unusable'),
            useNowBtn: resultHtml.find('#useNowBtn'),
            exchangeCouponBtn: resultHtml.find('#exchangeCouponBtn'),
            couponCodeInput: resultHtml.find('input[name=couponCodeInput]'),
            exchangeBox: resultHtml.find('.exchange-box'),
            useNowBox: resultHtml.find('#useNowBox')
        };

        this.view.filterBtn.on('click', this.tabChange.bind(this));
        this.view.coupon.on('click', '.checkbox', this.check.bind(this));
        this.view.tipClose.on('click', this.closeTip.bind(this));
        this.view.useNowBtn.on('click', this.useCoupon.bind(this));
        this.view.exchangeCouponBtn.on('click', this.exchangeCoupon.bind(this));
        this.view.couponCodeInput.on('input', this.changeExchangeBtnStatus.bind(this));

        if (this.closeCouponRuleTipbyUser === 'Y') {
            this.view.tipBox.addClass('hide');
        }
    }

    /**
     * 改变兑换按钮状态
     */
    changeExchangeBtnStatus() {
        if (this.view.couponCodeInput.val().length > 0) {
            this.view.exchangeCouponBtn.addClass('active');
        } else {
            this.view.exchangeCouponBtn.removeClass('active');
        }
    }

    /**
     * 使用优惠券码
     */
    exchangeCoupon() {
        let couponCode = this.view.couponCodeInput.val();

        if (!couponCode) {
            tip.show('请输入优惠券码');
            return false;
        }

        this.ajax({
            method: 'POST',
            url: this.useCouponCodeUrl,
            data: {
                couponCode: couponCode
            }
        }).then(res => {
            if (res.message) {
                tip.show(res.message);
            }
            if (res.code === 200) {
                this.selectCouponCodes.push(couponCode);
                setTimeout(() => {
                    location.reload();
                }, 500);
            }
        });
    }

    /**
     * 使用优惠券
     */
    useCoupon() {
        if (this.linkUrl) {
            window.location.href = this.linkUrl;
        } else {
            history.go(-1);
        }
    }

    /**
     * tab 切换
     */
    tabChange(event) {
        let itemClicked = $(event.currentTarget);

        // Tab 按钮状态
        if (!itemClicked.hasClass('active')) {
            this.view.filterBtn.removeClass('active');
            itemClicked.addClass('active');
        }

        // 优惠券列表切换,兑换模块、立即使用模块显示控制
        if (itemClicked.hasClass('valid')) {
            this.view.unusableCouponList.addClass('hide');
            this.view.usableCouponList.removeClass('hide');
            this.view.exchangeBox.removeClass('hide');

            if (itemClicked.data('num') === 0) {
                this.view.useNowBox.addClass('hide');
            } else {
                this.view.useNowBox.removeClass('hide');
            }
        } else {
            this.view.usableCouponList.addClass('hide');
            this.view.unusableCouponList.removeClass('hide');
            this.view.exchangeBox.addClass('hide');
            this.view.useNowBox.addClass('hide');
        }

        // Tip 控制
        if (this.closeCouponRuleTipbyUser === 'Y' || itemClicked.hasClass('invalid')) {
            this.view.tipBox.addClass('hide');
        } else {
            if (itemClicked.data('num') === 0) {
                this.view.tipBox.addClass('hide');
            } else {
                this.view.tipBox.removeClass('hide');
            }
        }
    }

    /**
     * 优惠券勾选处理
     */
    check(e) {
        let theCouponCheck = $(e.currentTarget);
        let theCoupon = $(e.delegateTarget);
        let theCouponData = theCoupon.data();

        if (!this.orderInfo('user_check_coupon')) {
            this.orderInfo('user_check_coupon', 'Y');
        }

        if (theCouponCheck.hasClass('icon-cb-radio')) {
            this.selectCouponCodes.splice($.inArray(theCouponData.code, this.selectCouponCodes), 1);
            theCouponCheck.removeClass('icon-cb-radio').addClass('icon-radio');
        } else {
            this.selectCouponCodes.push(theCouponData.code);
            theCouponCheck.removeClass('icon-radio').addClass('icon-cb-radio');
        }

        this.renderPage();
    }

    /**
     * 关闭提示框
     */
    closeTip() {
        this.closeCouponRuleTipbyUser = 'Y';
        cookie.set('close_coupon_rule_tip_by_user', 'Y', cookieOption);
        this.view.tipBox.hide();
    }
}

export default SelectCouponController;